mikmc5 commited on
Commit
7d374c1
·
verified ·
1 Parent(s): 70b07d1

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +55 -59
index.js CHANGED
@@ -30,7 +30,7 @@ const MCU_LIST_ID = '12179';
30
  let catalogCache = {};
31
  let metaCache = {};
32
 
33
- // Updated manifest for MCU collection with Easynews type
34
  const manifest = {
35
  id: 'org.stremio.mcu-catalog',
36
  version: '1.0.0',
@@ -98,78 +98,61 @@ async function fetchMCUCollection() {
98
  try {
99
  let allItems = [];
100
  let page = 1;
101
- let totalPages = 1;
102
 
103
- // Fetch all pages of the list
104
- do {
105
- // Using the TMDB list API endpoint with pagination
106
  const url = `${TMDB_BASE_URL}/list/${MCU_LIST_ID}?api_key=${TMDB_API_KEY}&language=en-GB&page=${page}`;
107
  logger.debug(`Requesting TMDB list page ${page}: ${url}`);
108
 
109
  const response = await axios.get(url);
110
- const { items, total_pages } = response.data;
111
 
112
- // Update total pages on first fetch
113
- if (page === 1) {
114
- totalPages = total_pages || 1;
 
 
 
 
 
 
 
 
 
 
 
 
115
  }
116
 
117
- // Add items to our collection
118
- allItems = [...allItems, ...items];
119
  page++;
120
- } while (page <= totalPages);
121
-
122
- // If we don't have many TV shows, supplement with a search
123
- const tvShows = allItems.filter(item =>
124
- item.media_type === 'tv' ||
125
- (item.first_air_date && !item.release_date)
126
- );
127
-
128
- if (tvShows.length < 10) {
129
- logger.info('Few TV shows found in list, supplementing with Marvel TV search...');
130
 
131
- // Search for Marvel TV shows
132
- const tvSearchUrl = `${TMDB_BASE_URL}/search/tv?api_key=${TMDB_API_KEY}&query=Marvel&include_adult=false`;
133
- const tvResponse = await axios.get(tvSearchUrl);
134
- const additionalTvShows = (tvResponse.data.results || []).map(item => ({
135
- ...item,
136
- media_type: 'tv'
137
- }));
138
-
139
- // Add the TV shows, avoiding duplicates
140
- const existingIds = new Set(allItems.map(item => item.id));
141
- const newTvShows = additionalTvShows.filter(show => !existingIds.has(show.id));
142
-
143
- logger.info(`Found ${newTvShows.length} additional Marvel TV shows`);
144
- allItems = [...allItems, ...newTvShows];
145
- }
146
-
147
- // Ensure all items have media_type property
148
- allItems = allItems.map(item => {
149
- // If media_type is not set, determine from available properties
150
- if (!item.media_type) {
151
- item.media_type = item.first_air_date && !item.release_date ? 'tv' : 'movie';
152
  }
153
- return item;
154
- });
155
 
156
- // Sort items by release date, chronological order (for MCU timeline)
157
  const sortedItems = allItems.sort((a, b) => {
158
  const dateA = a.release_date || a.first_air_date || '';
159
  const dateB = b.release_date || b.first_air_date || '';
160
  return dateA.localeCompare(dateB);
161
  });
162
 
163
- // Log breakdown for debugging
164
- const movies = sortedItems.filter(item => item.media_type === 'movie');
165
- const shows = sortedItems.filter(item => item.media_type === 'tv');
166
 
167
  logger.info(`Total MCU collection items fetched: ${sortedItems.length}`);
168
- logger.info(`Breakdown - Movies: ${movies.length}, TV Shows: ${shows.length}`);
169
 
170
  return sortedItems;
171
  } catch (error) {
172
- logger.error(`Error fetching MCU collection: ${error.message}`);
173
  return [];
174
  }
175
  }
@@ -179,6 +162,7 @@ async function fetchItemMetadata(items) {
179
  const fetchMetadataPromises = items.map(item => {
180
  const mediaType = item.media_type || (item.first_air_date ? 'tv' : 'movie');
181
  const url = `${TMDB_BASE_URL}/${mediaType}/${item.id}?api_key=${TMDB_API_KEY}`;
 
182
  return axios.get(url)
183
  .then(response => ({
184
  ...response.data,
@@ -239,29 +223,41 @@ builder.defineCatalogHandler(async (args) => {
239
  return { metas: [] };
240
  }
241
 
242
- // Filter movies or TV shows based on catalog ID
243
- let results = (catalogId === 'mcu-movies')
244
- ? catalogCache.data.filter(item => item.media_type === 'movie' || (!item.media_type && !item.first_air_date))
245
- : catalogCache.data.filter(item => item.media_type === 'tv' || (!item.media_type && item.first_air_date));
 
 
 
 
 
 
 
 
 
246
 
247
  // Filter by search query if provided
248
  if (searchQuery) {
249
  results = results.filter(item =>
250
- (item.title || item.name).toLowerCase().includes(searchQuery.toLowerCase())
251
  );
252
  }
253
 
254
  // Filter by genre if provided
255
  if (genreFilter) {
256
- results = results.filter(item => item.genre_ids && item.genre_ids.includes(parseInt(genreFilter)));
 
 
 
257
  }
258
 
259
  const metas = results.map(item => {
260
- const isMovie = item.media_type === 'movie' || (!item.media_type && !item.first_air_date);
261
  return {
262
  id: `tmdb-${item.id}`,
263
  type: isMovie ? 'movie' : 'series',
264
- name: item.title || item.name,
265
  poster: item.poster_path ? `https://image.tmdb.org/t/p/w500${item.poster_path}` : null,
266
  background: item.backdrop_path ? `https://image.tmdb.org/t/p/w1280${item.backdrop_path}` : null,
267
  description: item.overview || 'No description available',
@@ -282,7 +278,7 @@ builder.defineMetaHandler(async (args) => {
282
  const id = args.id.split('-')[1];
283
  logger.debug(`Meta request received for item ID: ${id}`);
284
 
285
- // Find item in metaCache
286
  const cacheKey = Object.keys(metaCache).find(key => key.endsWith(`-${id}`));
287
 
288
  if (!cacheKey || !metaCache[cacheKey]) {
 
30
  let catalogCache = {};
31
  let metaCache = {};
32
 
33
+ // Updated manifest for MCU collection
34
  const manifest = {
35
  id: 'org.stremio.mcu-catalog',
36
  version: '1.0.0',
 
98
  try {
99
  let allItems = [];
100
  let page = 1;
101
+ let hasMorePages = true;
102
 
103
+ // Keep fetching pages until we get all items
104
+ while (hasMorePages) {
 
105
  const url = `${TMDB_BASE_URL}/list/${MCU_LIST_ID}?api_key=${TMDB_API_KEY}&language=en-GB&page=${page}`;
106
  logger.debug(`Requesting TMDB list page ${page}: ${url}`);
107
 
108
  const response = await axios.get(url);
109
+ const { items, page: currentPage, total_pages } = response.data;
110
 
111
+ if (items && items.length > 0) {
112
+ // Process items to identify their media type
113
+ const processedItems = items.map(item => {
114
+ // If media_type is missing, determine it based on available properties
115
+ if (!item.media_type) {
116
+ if (item.first_air_date) {
117
+ item.media_type = 'tv';
118
+ } else if (item.release_date || item.title) {
119
+ item.media_type = 'movie';
120
+ }
121
+ }
122
+ return item;
123
+ });
124
+
125
+ allItems = [...allItems, ...processedItems];
126
  }
127
 
128
+ // Check if we need to fetch more pages
129
+ hasMorePages = currentPage < total_pages;
130
  page++;
 
 
 
 
 
 
 
 
 
 
131
 
132
+ // Safety check to prevent infinite loops
133
+ if (page > 20) {
134
+ logger.warn('Reached maximum page limit (20). Stopping pagination.');
135
+ break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  }
137
+ }
 
138
 
139
+ // Sort items by release date
140
  const sortedItems = allItems.sort((a, b) => {
141
  const dateA = a.release_date || a.first_air_date || '';
142
  const dateB = b.release_date || b.first_air_date || '';
143
  return dateA.localeCompare(dateB);
144
  });
145
 
146
+ // Log the results for debugging
147
+ const movies = sortedItems.filter(item => item.media_type === 'movie' || (!item.media_type && !item.first_air_date));
148
+ const tvShows = sortedItems.filter(item => item.media_type === 'tv' || (!item.media_type && item.first_air_date));
149
 
150
  logger.info(`Total MCU collection items fetched: ${sortedItems.length}`);
151
+ logger.info(`Breakdown - Movies: ${movies.length}, TV Shows: ${tvShows.length}`);
152
 
153
  return sortedItems;
154
  } catch (error) {
155
+ logger.error('Error fetching MCU collection:', error);
156
  return [];
157
  }
158
  }
 
162
  const fetchMetadataPromises = items.map(item => {
163
  const mediaType = item.media_type || (item.first_air_date ? 'tv' : 'movie');
164
  const url = `${TMDB_BASE_URL}/${mediaType}/${item.id}?api_key=${TMDB_API_KEY}`;
165
+
166
  return axios.get(url)
167
  .then(response => ({
168
  ...response.data,
 
223
  return { metas: [] };
224
  }
225
 
226
+ // Filter movies or TV shows based on catalog ID with improved media type detection
227
+ let results;
228
+ if (catalogId === 'mcu-movies') {
229
+ results = catalogCache.data.filter(item =>
230
+ item.media_type === 'movie' ||
231
+ (!item.first_air_date && (item.release_date || item.title))
232
+ );
233
+ } else {
234
+ results = catalogCache.data.filter(item =>
235
+ item.media_type === 'tv' ||
236
+ (item.first_air_date || (item.name && !item.title))
237
+ );
238
+ }
239
 
240
  // Filter by search query if provided
241
  if (searchQuery) {
242
  results = results.filter(item =>
243
+ (item.title || item.name || '').toLowerCase().includes(searchQuery.toLowerCase())
244
  );
245
  }
246
 
247
  // Filter by genre if provided
248
  if (genreFilter) {
249
+ results = results.filter(item =>
250
+ item.genre_ids &&
251
+ item.genre_ids.some(id => id.toString() === genreFilter.toString())
252
+ );
253
  }
254
 
255
  const metas = results.map(item => {
256
+ const isMovie = item.media_type === 'movie' || (!item.first_air_date && (item.release_date || item.title));
257
  return {
258
  id: `tmdb-${item.id}`,
259
  type: isMovie ? 'movie' : 'series',
260
+ name: item.title || item.name || 'Unknown Title',
261
  poster: item.poster_path ? `https://image.tmdb.org/t/p/w500${item.poster_path}` : null,
262
  background: item.backdrop_path ? `https://image.tmdb.org/t/p/w1280${item.backdrop_path}` : null,
263
  description: item.overview || 'No description available',
 
278
  const id = args.id.split('-')[1];
279
  logger.debug(`Meta request received for item ID: ${id}`);
280
 
281
+ // Find item in metaCache by checking all possible keys
282
  const cacheKey = Object.keys(metaCache).find(key => key.endsWith(`-${id}`));
283
 
284
  if (!cacheKey || !metaCache[cacheKey]) {