mikmc5 commited on
Commit
1235c01
·
verified ·
1 Parent(s): 6e05c63

Update addon.js

Browse files
Files changed (1) hide show
  1. addon.js +121 -89
addon.js CHANGED
@@ -27,112 +27,100 @@ const documentaryGenres = [
27
  "Ancient Civilizations"
28
  ];
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  const manifest = {
31
  id: "hy.community.ytdocumentaries",
32
- version: "0.4.0",
33
  name: "YouTube Documentaries",
34
  description: "Discover documentaries from YouTube with channels, playlists, and genres",
35
  logo: "https://upload.wikimedia.org/wikipedia/commons/thumb/0/09/YouTube_full-color_icon_%282017%29.svg/1280px-YouTube_full-color_icon_%282017%29.svg.png",
36
  resources: ["catalog", "meta", "stream"],
37
- types: ["movie", "series"],
38
  catalogs: [
39
  {
40
- type: "movie",
 
41
  id: "ytdocs",
42
  name: "YouTube Documentaries",
43
  extra: [
44
  {
45
  name: "genre",
46
  isRequired: false,
47
- options: documentaryGenres // This is the key change - explicitly provide genre options
48
  },
49
  { name: "search", isRequired: false },
50
  { name: "skip", isRequired: false }
51
  ],
52
- // Important: Add genres array at catalog level (not just in extra)
53
  genres: documentaryGenres
54
  },
55
  {
56
- type: "movie",
57
- id: "ytdocs-by-tag",
58
- name: "Tagged Docs",
59
- extra: [
60
- { name: "search", isRequired: true },
61
- { name: "skip", isRequired: false }
62
- ]
63
- },
64
- {
65
- // Clearly labeled channel catalog
66
- type: "series",
67
  id: "ytdocs-channels",
68
- name: "CHANNELS: Documentary",
69
  extra: [
70
  {
71
  name: "genre",
72
  isRequired: false,
73
- options: [
74
- "Education",
75
- "History",
76
- "Science",
77
- "Travel",
78
- "Wildlife",
79
- "News",
80
- "Conspiracy",
81
- "Technology",
82
- "Archaeology"
83
- ]
84
  },
85
  { name: "search", isRequired: false },
86
  { name: "skip", isRequired: false }
87
  ],
88
- // Important: Add genres array at catalog level
89
- genres: [
90
- "Education",
91
- "History",
92
- "Science",
93
- "Travel",
94
- "Wildlife",
95
- "News",
96
- "Conspiracy",
97
- "Technology",
98
- "Archaeology"
99
- ]
100
  },
101
  {
102
- // Clearly labeled playlist catalog
103
- type: "series",
104
  id: "ytdocs-playlists",
105
- name: "PLAYLISTS: Documentary",
106
  extra: [
107
  {
108
  name: "genre",
109
  isRequired: false,
110
- options: [
111
- "Educational",
112
- "Series",
113
- "Collections",
114
- "Lectures",
115
- "Top Documentaries",
116
- "Mysteries",
117
- "Historical Events",
118
- "Science Explained",
119
- "Nature & Wildlife"
120
- ]
121
  },
122
  { name: "search", isRequired: false },
123
  { name: "skip", isRequired: false }
124
  ],
125
- // Important: Add genres array at catalog level
126
- genres: [
127
- "Educational",
128
- "Series",
129
- "Collections",
130
- "Lectures",
131
- "Top Documentaries",
132
- "Mysteries",
133
- "Historical Events",
134
- "Science Explained",
135
- "Nature & Wildlife"
136
  ]
137
  }
138
  ],
@@ -149,8 +137,13 @@ builder.defineCatalogHandler(async ({ type, id, extra }) => {
149
  const skip = parseInt(extra.skip || 0);
150
  const pageSize = 50;
151
 
 
 
 
 
 
152
  // Handle different catalog types
153
- if (type === "movie" && (id === "ytdocs" || id === "ytdocs-by-tag")) {
154
  let searchQuery;
155
 
156
  if (id === "ytdocs-by-tag") {
@@ -179,7 +172,7 @@ builder.defineCatalogHandler(async ({ type, id, extra }) => {
179
 
180
  const metas = videos.map(video => ({
181
  id: `yt:${video.id}`,
182
- type: "movie",
183
  name: video.title,
184
  poster: video.thumbnail,
185
  description: video.description,
@@ -191,7 +184,7 @@ builder.defineCatalogHandler(async ({ type, id, extra }) => {
191
  return { metas };
192
  }
193
  // Handle channel search - explicitly using channel search type
194
- else if (type === "series" && id === "ytdocs-channels") {
195
  // Use genre if provided, otherwise use default or search
196
  const searchTerm = extra.genre ? extra.genre : "documentary";
197
  const searchQuery = extra.search ? `${extra.search} ${searchTerm}` : searchTerm;
@@ -206,24 +199,29 @@ builder.defineCatalogHandler(async ({ type, id, extra }) => {
206
  query: searchQuery,
207
  maxResults: pageSize,
208
  skip,
209
- type: 'channel' // This is the key - must use 'channel'
210
  });
211
 
212
  const metas = channels.map(channel => ({
213
  id: `ytchannel:${channel.id}`,
214
- type: "series",
215
  name: channel.title,
216
  poster: channel.thumbnail,
217
  posterShape: "square",
218
  description: channel.description,
219
- genres: ["Documentary", extra.genre || "YouTube Channel"]
 
 
 
 
 
220
  }));
221
 
222
  cache.set(cacheKey, metas);
223
  return { metas };
224
  }
225
  // Handle playlist search - explicitly using playlist search type
226
- else if (type === "series" && id === "ytdocs-playlists") {
227
  // Use genre if provided, otherwise use default or search
228
  const searchTerm = extra.genre ? extra.genre : "documentary";
229
  const searchQuery = extra.search ? `${extra.search} ${searchTerm}` : searchTerm;
@@ -238,17 +236,22 @@ builder.defineCatalogHandler(async ({ type, id, extra }) => {
238
  query: searchQuery,
239
  maxResults: pageSize,
240
  skip,
241
- type: 'playlist' // This is the key - must use 'playlist'
242
  });
243
 
244
  const metas = playlists.map(playlist => ({
245
  id: `ytplaylist:${playlist.id}`,
246
- type: "series",
247
  name: playlist.title,
248
  poster: playlist.thumbnail,
249
  posterShape: "landscape",
250
  description: playlist.description,
251
- genres: ["Documentary", extra.genre || "YouTube Playlist"]
 
 
 
 
 
252
  }));
253
 
254
  cache.set(cacheKey, metas);
@@ -261,14 +264,19 @@ builder.defineCatalogHandler(async ({ type, id, extra }) => {
261
  builder.defineMetaHandler(async ({ type, id }) => {
262
  console.log(`[META] Request for ${type} with id ${id}`);
263
 
 
 
 
 
 
264
  // Handle single video meta
265
- if (type === "movie" && id.startsWith("yt:")) {
266
  const videoId = id.split(":")[1];
267
  const meta = await fetchVideoMeta(videoId);
268
  return { meta };
269
  }
270
  // Handle channel meta with videos as episodes
271
- else if (type === "series" && id.startsWith("ytchannel:")) {
272
  const channelId = id.split(":")[1];
273
  const cacheKey = `meta_channel_${channelId}`;
274
  const cached = cache.get(cacheKey);
@@ -298,21 +306,23 @@ builder.defineMetaHandler(async ({ type, id }) => {
298
 
299
  const meta = {
300
  id: `ytchannel:${channelId}`,
301
- type: "series",
302
  name: channelInfo.title,
303
  poster: channelInfo.thumbnail,
304
  posterShape: "square",
305
  background: channelInfo.banner || channelInfo.thumbnail,
306
  description: channelInfo.description,
307
- genres: ["Documentary", "YouTube Channel"],
308
- videos: videos
 
 
309
  };
310
 
311
  cache.set(cacheKey, meta);
312
  return { meta };
313
  }
314
  // Handle playlist meta with videos as episodes
315
- else if (type === "series" && id.startsWith("ytplaylist:")) {
316
  const playlistId = id.split(":")[1];
317
  const cacheKey = `meta_playlist_${playlistId}`;
318
  const cached = cache.get(cacheKey);
@@ -345,14 +355,16 @@ builder.defineMetaHandler(async ({ type, id }) => {
345
 
346
  const meta = {
347
  id: `ytplaylist:${playlistId}`,
348
- type: "series",
349
  name: playlistInfo.title,
350
  poster: playlistInfo.thumbnail,
351
  posterShape: "landscape",
352
  background: playlistInfo.thumbnail,
353
  description: playlistInfo.description,
354
- genres: ["Documentary", "YouTube Playlist"],
355
- videos: videos
 
 
356
  };
357
 
358
  cache.set(cacheKey, meta);
@@ -365,9 +377,16 @@ builder.defineMetaHandler(async ({ type, id }) => {
365
  // STREAM HANDLER - DIRECT YOUTUBE INTEGRATION
366
  builder.defineStreamHandler(({ type, id }) => {
367
  console.log(`[STREAM] Request for ${id}`);
 
 
 
 
 
 
368
  if (!id.startsWith("yt:")) {
369
  return Promise.resolve({ streams: [] });
370
  }
 
371
  const videoId = id.split(":")[1];
372
  console.log(`[STREAM] Found YouTube video ID: ${videoId}`);
373
 
@@ -483,14 +502,16 @@ async function fetchVideoMeta(videoId) {
483
 
484
  const meta = {
485
  id: `yt:${videoId}`,
486
- type: "movie",
487
  name: item.snippet.title,
488
  description: item.snippet.description,
489
  poster: item.snippet.thumbnails.high?.url || item.snippet.thumbnails.default?.url,
490
  posterShape: "landscape",
491
- background: item.snippet.thumbnails.high?.url || item.snippet.thumbnails.default?.url,
 
 
492
  released: item.snippet.publishedAt,
493
- genres: genres,
494
  releaseInfo: item.snippet.publishedAt ? new Date(item.snippet.publishedAt).getFullYear().toString() : "",
495
  runtime: item.contentDetails?.duration ? convertDuration(item.contentDetails.duration) : null,
496
  // Add watch count
@@ -545,7 +566,8 @@ async function fetchChannelInfo(channelId) {
545
  title: item.snippet.title,
546
  description: `${item.snippet.description}\n\nSubscribers: ${formatSubscriberCount(item.statistics?.subscriberCount)}`,
547
  thumbnail: item.snippet.thumbnails.high?.url || item.snippet.thumbnails.default?.url,
548
- banner: bannerUrl
 
549
  };
550
  } catch (error) {
551
  console.error("Error fetching channel info:", error);
@@ -565,6 +587,16 @@ function formatSubscriberCount(count) {
565
  return subscribers.toString();
566
  }
567
 
 
 
 
 
 
 
 
 
 
 
568
  async function fetchChannelVideos(channelId, maxResults = 50) {
569
  try {
570
  const youtube = google.youtube({ version: "v3", auth: "AIzaSyCRFgNu3-oNfGA1IZQ88Qc4k0Pq_jG6IpY" });
@@ -618,7 +650,7 @@ async function fetchPlaylistInfo(playlistId) {
618
  title: item.snippet.title,
619
  description: `${item.snippet.description}\n\nVideos: ${item.contentDetails.itemCount || 'Unknown'} (${privacy})`,
620
  thumbnail: item.snippet.thumbnails.high?.url || item.snippet.thumbnails.medium?.url || item.snippet.thumbnails.default?.url,
621
- videoCount: item.contentDetails.itemCount
622
  };
623
  } catch (error) {
624
  console.error("Error fetching playlist info:", error);
 
27
  "Ancient Civilizations"
28
  ];
29
 
30
+ // Channel-specific genres
31
+ const channelGenres = [
32
+ "Education",
33
+ "History",
34
+ "Science",
35
+ "Travel",
36
+ "Wildlife",
37
+ "News",
38
+ "Conspiracy",
39
+ "Technology",
40
+ "Archaeology",
41
+ "Space",
42
+ "Anthropology",
43
+ "Culture"
44
+ ];
45
+
46
+ // Playlist-specific genres
47
+ const playlistGenres = [
48
+ "Educational",
49
+ "Series",
50
+ "Collections",
51
+ "Lectures",
52
+ "Top Documentaries",
53
+ "Mysteries",
54
+ "Historical Events",
55
+ "Science Explained",
56
+ "Nature & Wildlife"
57
+ ];
58
+
59
  const manifest = {
60
  id: "hy.community.ytdocumentaries",
61
+ version: "0.5.0",
62
  name: "YouTube Documentaries",
63
  description: "Discover documentaries from YouTube with channels, playlists, and genres",
64
  logo: "https://upload.wikimedia.org/wikipedia/commons/thumb/0/09/YouTube_full-color_icon_%282017%29.svg/1280px-YouTube_full-color_icon_%282017%29.svg.png",
65
  resources: ["catalog", "meta", "stream"],
66
+ types: ["documentary"], // Single coherent type for better organization
67
  catalogs: [
68
  {
69
+ // Main catalog for individual documentaries
70
+ type: "documentary",
71
  id: "ytdocs",
72
  name: "YouTube Documentaries",
73
  extra: [
74
  {
75
  name: "genre",
76
  isRequired: false,
77
+ options: documentaryGenres
78
  },
79
  { name: "search", isRequired: false },
80
  { name: "skip", isRequired: false }
81
  ],
 
82
  genres: documentaryGenres
83
  },
84
  {
85
+ // Channel catalog
86
+ type: "documentary",
 
 
 
 
 
 
 
 
 
87
  id: "ytdocs-channels",
88
+ name: "Documentary Channels",
89
  extra: [
90
  {
91
  name: "genre",
92
  isRequired: false,
93
+ options: channelGenres
 
 
 
 
 
 
 
 
 
 
94
  },
95
  { name: "search", isRequired: false },
96
  { name: "skip", isRequired: false }
97
  ],
98
+ genres: channelGenres
 
 
 
 
 
 
 
 
 
 
 
99
  },
100
  {
101
+ // Playlist catalog
102
+ type: "documentary",
103
  id: "ytdocs-playlists",
104
+ name: "Documentary Playlists",
105
  extra: [
106
  {
107
  name: "genre",
108
  isRequired: false,
109
+ options: playlistGenres
 
 
 
 
 
 
 
 
 
 
110
  },
111
  { name: "search", isRequired: false },
112
  { name: "skip", isRequired: false }
113
  ],
114
+ genres: playlistGenres
115
+ },
116
+ {
117
+ // Tag-based search catalog
118
+ type: "documentary",
119
+ id: "ytdocs-by-tag",
120
+ name: "Tagged Documentaries",
121
+ extra: [
122
+ { name: "search", isRequired: true },
123
+ { name: "skip", isRequired: false }
 
124
  ]
125
  }
126
  ],
 
137
  const skip = parseInt(extra.skip || 0);
138
  const pageSize = 50;
139
 
140
+ // Only respond to documentary type requests
141
+ if (type !== "documentary") {
142
+ return { metas: [] };
143
+ }
144
+
145
  // Handle different catalog types
146
+ if (id === "ytdocs" || id === "ytdocs-by-tag") {
147
  let searchQuery;
148
 
149
  if (id === "ytdocs-by-tag") {
 
172
 
173
  const metas = videos.map(video => ({
174
  id: `yt:${video.id}`,
175
+ type: "documentary",
176
  name: video.title,
177
  poster: video.thumbnail,
178
  description: video.description,
 
184
  return { metas };
185
  }
186
  // Handle channel search - explicitly using channel search type
187
+ else if (id === "ytdocs-channels") {
188
  // Use genre if provided, otherwise use default or search
189
  const searchTerm = extra.genre ? extra.genre : "documentary";
190
  const searchQuery = extra.search ? `${extra.search} ${searchTerm}` : searchTerm;
 
199
  query: searchQuery,
200
  maxResults: pageSize,
201
  skip,
202
+ type: 'channel'
203
  });
204
 
205
  const metas = channels.map(channel => ({
206
  id: `ytchannel:${channel.id}`,
207
+ type: "documentary",
208
  name: channel.title,
209
  poster: channel.thumbnail,
210
  posterShape: "square",
211
  description: channel.description,
212
+ genres: ["Documentary Channel", extra.genre || "Educational"],
213
+ videoCount: "Channel", // Use this field to identify as a channel in UI
214
+ behaviorHints: {
215
+ defaultVideoId: null, // Signal this is not a single video
216
+ hasCollections: true // Signal this item has collections (videos)
217
+ }
218
  }));
219
 
220
  cache.set(cacheKey, metas);
221
  return { metas };
222
  }
223
  // Handle playlist search - explicitly using playlist search type
224
+ else if (id === "ytdocs-playlists") {
225
  // Use genre if provided, otherwise use default or search
226
  const searchTerm = extra.genre ? extra.genre : "documentary";
227
  const searchQuery = extra.search ? `${extra.search} ${searchTerm}` : searchTerm;
 
236
  query: searchQuery,
237
  maxResults: pageSize,
238
  skip,
239
+ type: 'playlist'
240
  });
241
 
242
  const metas = playlists.map(playlist => ({
243
  id: `ytplaylist:${playlist.id}`,
244
+ type: "documentary",
245
  name: playlist.title,
246
  poster: playlist.thumbnail,
247
  posterShape: "landscape",
248
  description: playlist.description,
249
+ genres: ["Documentary Playlist", extra.genre || "Collection"],
250
+ videoCount: "Playlist", // Use this field to identify as a playlist in UI
251
+ behaviorHints: {
252
+ defaultVideoId: null, // Signal this is not a single video
253
+ hasCollections: true // Signal this item has collections (videos)
254
+ }
255
  }));
256
 
257
  cache.set(cacheKey, metas);
 
264
  builder.defineMetaHandler(async ({ type, id }) => {
265
  console.log(`[META] Request for ${type} with id ${id}`);
266
 
267
+ // Only respond to documentary type requests
268
+ if (type !== "documentary") {
269
+ return { meta: null };
270
+ }
271
+
272
  // Handle single video meta
273
+ if (id.startsWith("yt:")) {
274
  const videoId = id.split(":")[1];
275
  const meta = await fetchVideoMeta(videoId);
276
  return { meta };
277
  }
278
  // Handle channel meta with videos as episodes
279
+ else if (id.startsWith("ytchannel:")) {
280
  const channelId = id.split(":")[1];
281
  const cacheKey = `meta_channel_${channelId}`;
282
  const cached = cache.get(cacheKey);
 
306
 
307
  const meta = {
308
  id: `ytchannel:${channelId}`,
309
+ type: "documentary",
310
  name: channelInfo.title,
311
  poster: channelInfo.thumbnail,
312
  posterShape: "square",
313
  background: channelInfo.banner || channelInfo.thumbnail,
314
  description: channelInfo.description,
315
+ genres: ["Documentary Channel", "Educational"],
316
+ videos: videos,
317
+ videoCount: channelInfo.videoCount || "Unknown",
318
+ releaseInfo: `${videos.length} documentaries`
319
  };
320
 
321
  cache.set(cacheKey, meta);
322
  return { meta };
323
  }
324
  // Handle playlist meta with videos as episodes
325
+ else if (id.startsWith("ytplaylist:")) {
326
  const playlistId = id.split(":")[1];
327
  const cacheKey = `meta_playlist_${playlistId}`;
328
  const cached = cache.get(cacheKey);
 
355
 
356
  const meta = {
357
  id: `ytplaylist:${playlistId}`,
358
+ type: "documentary",
359
  name: playlistInfo.title,
360
  poster: playlistInfo.thumbnail,
361
  posterShape: "landscape",
362
  background: playlistInfo.thumbnail,
363
  description: playlistInfo.description,
364
+ genres: ["Documentary Playlist", "Collection"],
365
+ videos: videos,
366
+ videoCount: playlistInfo.videoCount || "Unknown",
367
+ releaseInfo: `${videos.length} documentaries`
368
  };
369
 
370
  cache.set(cacheKey, meta);
 
377
  // STREAM HANDLER - DIRECT YOUTUBE INTEGRATION
378
  builder.defineStreamHandler(({ type, id }) => {
379
  console.log(`[STREAM] Request for ${id}`);
380
+
381
+ // Only respond to documentary type requests or handle YouTube IDs
382
+ if (type !== "documentary" && !id.startsWith("yt:")) {
383
+ return Promise.resolve({ streams: [] });
384
+ }
385
+
386
  if (!id.startsWith("yt:")) {
387
  return Promise.resolve({ streams: [] });
388
  }
389
+
390
  const videoId = id.split(":")[1];
391
  console.log(`[STREAM] Found YouTube video ID: ${videoId}`);
392
 
 
502
 
503
  const meta = {
504
  id: `yt:${videoId}`,
505
+ type: "documentary",
506
  name: item.snippet.title,
507
  description: item.snippet.description,
508
  poster: item.snippet.thumbnails.high?.url || item.snippet.thumbnails.default?.url,
509
  posterShape: "landscape",
510
+ background: item.snippet.thumbnails.maxres?.url ||
511
+ item.snippet.thumbnails.high?.url ||
512
+ item.snippet.thumbnails.default?.url,
513
  released: item.snippet.publishedAt,
514
+ genres: genres.length > 0 ? genres : ["Documentary"],
515
  releaseInfo: item.snippet.publishedAt ? new Date(item.snippet.publishedAt).getFullYear().toString() : "",
516
  runtime: item.contentDetails?.duration ? convertDuration(item.contentDetails.duration) : null,
517
  // Add watch count
 
566
  title: item.snippet.title,
567
  description: `${item.snippet.description}\n\nSubscribers: ${formatSubscriberCount(item.statistics?.subscriberCount)}`,
568
  thumbnail: item.snippet.thumbnails.high?.url || item.snippet.thumbnails.default?.url,
569
+ banner: bannerUrl,
570
+ videoCount: formatVideoCount(item.statistics?.videoCount)
571
  };
572
  } catch (error) {
573
  console.error("Error fetching channel info:", error);
 
587
  return subscribers.toString();
588
  }
589
 
590
+ function formatVideoCount(count) {
591
+ if (!count) return "Unknown";
592
+
593
+ const videos = parseInt(count);
594
+ if (videos > 1000) {
595
+ return `${(videos / 1000).toFixed(1)}K videos`;
596
+ }
597
+ return `${videos} videos`;
598
+ }
599
+
600
  async function fetchChannelVideos(channelId, maxResults = 50) {
601
  try {
602
  const youtube = google.youtube({ version: "v3", auth: "AIzaSyCRFgNu3-oNfGA1IZQ88Qc4k0Pq_jG6IpY" });
 
650
  title: item.snippet.title,
651
  description: `${item.snippet.description}\n\nVideos: ${item.contentDetails.itemCount || 'Unknown'} (${privacy})`,
652
  thumbnail: item.snippet.thumbnails.high?.url || item.snippet.thumbnails.medium?.url || item.snippet.thumbnails.default?.url,
653
+ videoCount: `${item.contentDetails.itemCount || 'Unknown'} videos`
654
  };
655
  } catch (error) {
656
  console.error("Error fetching playlist info:", error);