mikmc commited on
Commit
923f9ed
·
verified ·
1 Parent(s): 91136ee

Upload 6 files

Browse files
Files changed (6) hide show
  1. .env +33 -0
  2. cache/catalog_cache.json +0 -0
  3. cache/meta_cache.json +0 -0
  4. index.js +60 -40
  5. package-lock.json +19 -2
  6. package.json +8 -6
.env ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # TMDB API Key
2
+ TMDB_API_KEY=f051e7366c6105ad4f9aafe4733d9dae
3
+
4
+ # TMDB Base URL
5
+ TMDB_BASE_URL=https://api.themoviedb.org/3
6
+
7
+ # Cache Directory
8
+ CACHE_DIR=cache
9
+
10
+ # Cache Files
11
+ CATALOG_CACHE_FILE=cache/catalog_cache.json
12
+ META_CACHE_FILE=cache/meta_cache.json
13
+
14
+ # Filtering Configuration
15
+ START_YEAR=2015
16
+ END_YEAR=2024
17
+ MIN_SCORE=5
18
+ NUM_PAGES=25
19
+
20
+ # Genres to exclude (comma-separated)
21
+ EXCLUDED_GENRES=16,10402
22
+
23
+ # Genres to include (comma-separated)
24
+ INCLUDED_GENRES=28,12,35,14
25
+
26
+ # Genres to include with OR combinations (pipe-separated)
27
+ INCLUDED_GENRES_OR=28|12|35|14
28
+
29
+ # Language
30
+ LANGUAGE=en
31
+
32
+ # Genre Combinations (pipe-separated)
33
+ GENRE_COMBINATIONS=28,12|28,35|28,14|14,12|14,35
cache/catalog_cache.json CHANGED
The diff for this file is too large to render. See raw diff
 
cache/meta_cache.json CHANGED
The diff for this file is too large to render. See raw diff
 
index.js CHANGED
@@ -4,15 +4,14 @@ const fs = require('fs').promises;
4
  const path = require('path');
5
  const winston = require('winston');
6
  const schedule = require('node-schedule');
 
7
 
8
- // Winston logger configuration
9
  const logger = winston.createLogger({
10
  level: 'debug',
11
  format: winston.format.combine(
12
  winston.format.timestamp(),
13
- winston.format.printf(({ level, message, timestamp }) => {
14
- return `${timestamp} ${level}: ${message}`;
15
- })
16
  ),
17
  transports: [
18
  new winston.transports.Console(),
@@ -20,27 +19,43 @@ const logger = winston.createLogger({
20
  ]
21
  });
22
 
23
- const TMDB_API_KEY = 'f051e7366c6105ad4f9aafe4733d9dae';
24
- const TMDB_BASE_URL = 'https://api.themoviedb.org/3';
25
- const CACHE_DIR = path.join(__dirname, 'cache');
26
  const CATALOG_CACHE_FILE = path.join(CACHE_DIR, 'catalog_cache.json');
27
  const META_CACHE_FILE = path.join(CACHE_DIR, 'meta_cache.json');
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  let catalogCache = {};
30
  let metaCache = {};
31
 
32
- // Manifest
33
  const manifest = {
34
- id: 'org.stremio.horror-movies',
35
  version: '1.0.0',
36
- name: 'Horror Movies Since 2015',
37
- description: 'Stremio Add-on to showcase Horror movies from the year 2015 onward',
38
  catalogs: [
39
  {
40
- id: 'horror-movie-catalog',
41
- name: 'Horror Movies',
42
  type: 'Easynews',
43
- pageSize: 25,
 
44
  extra: [{ name: 'search', isRequired: false }]
45
  }
46
  ],
@@ -88,38 +103,45 @@ async function saveCache() {
88
  }
89
  }
90
 
91
- async function fetchHorrorMovies() {
92
- const pageSize = 20; // Number of results per page
93
- const maxPages = 40; // Limit to prevent too many requests
94
- const startYear = 2021;
95
-
96
  let page = 1;
 
97
  const allResults = [];
98
 
99
- while (page <= maxPages) {
100
- const url = `${TMDB_BASE_URL}/discover/movie?api_key=${TMDB_API_KEY}&with_genres=27&sort_by=primary_release_date.desc&page=${page}&include_adult=false&include_video=false&primary_release_date.gte=${startYear}-01-01`;
101
- logger.debug(`Requesting TMDB: ${url}`);
 
102
 
103
- try {
104
  const response = await axios.get(url);
105
  const { results, total_pages } = response.data;
 
106
 
107
- if (results.length === 0) break; // Exit if no more results
108
 
109
- logger.debug(`Page ${page} fetched. Results: ${results.length}`);
110
- allResults.push(...results);
111
-
112
- if (page >= total_pages) break; // Exit if we've reached the last page
113
 
114
  page++;
115
- } catch (error) {
116
- logger.error(`Error fetching page ${page}: ${error.message}`);
117
- break; // Stop fetching if an error occurs
118
- }
 
 
 
 
 
 
 
 
 
 
 
119
  }
120
 
121
  allResults.sort((a, b) => new Date(b.release_date) - new Date(a.release_date));
122
- logger.info(`Total Horror movies fetched since ${startYear}: ${allResults.length}`);
123
  return allResults;
124
  }
125
 
@@ -127,10 +149,9 @@ async function fetchMovieMetadata(movieId) {
127
  try {
128
  const url = `${TMDB_BASE_URL}/movie/${movieId}?api_key=${TMDB_API_KEY}`;
129
  const response = await axios.get(url);
130
- logger.debug(`Metadata fetched for movie ID ${movieId}: ${JSON.stringify(response.data)}`);
131
  return response.data;
132
  } catch (error) {
133
- logger.error(`Error fetching metadata for movie ID ${movieId}: ${error.message}`);
134
  return { error: 'Failed to fetch movie data' };
135
  }
136
  }
@@ -138,13 +159,12 @@ async function fetchMovieMetadata(movieId) {
138
  async function updateCatalogAndMetaCache() {
139
  logger.info('Updating catalog and meta cache...');
140
  try {
141
- const results = await fetchHorrorMovies();
142
  catalogCache = {
143
  timestamp: Date.now(),
144
  data: results
145
  };
146
 
147
- // Fetch meta information for all movies
148
  for (const movie of results) {
149
  const movieId = movie.id;
150
  logger.debug(`Fetching meta for movie ID: ${movieId}`);
@@ -179,7 +199,7 @@ builder.defineCatalogHandler(async (args) => {
179
  const metas = results.map(item => ({
180
  id: `tmdb-${item.id}`,
181
  type: 'movie',
182
- name: item.title || 'Unknown Title',
183
  poster: item.poster_path ? `https://image.tmdb.org/t/p/w500${item.poster_path}` : null,
184
  description: item.overview || 'No description available',
185
  releaseInfo: item.release_date ? item.release_date.split('-')[0] : 'Unknown'
@@ -216,7 +236,7 @@ builder.defineMetaHandler(async (args) => {
216
  backdrop_path
217
  } = movieData;
218
 
219
- logger.debug(`Metadata for movie ID ${id}: ${JSON.stringify(movieData)}`);
220
  return {
221
  meta: {
222
  id: args.id,
@@ -254,7 +274,7 @@ async function startServer() {
254
  }
255
 
256
  const interface = builder.getInterface();
257
- const port = process.env.PORT || 3000;
258
 
259
  logger.info(`Starting Stremio Add-on server on port ${port}`);
260
  serveHTTP(interface, { port });
 
4
  const path = require('path');
5
  const winston = require('winston');
6
  const schedule = require('node-schedule');
7
+ require('dotenv').config(); // Load environment variables
8
 
9
+ // Logger configuration
10
  const logger = winston.createLogger({
11
  level: 'debug',
12
  format: winston.format.combine(
13
  winston.format.timestamp(),
14
+ winston.format.printf(({ level, message, timestamp }) => `${timestamp} ${level}: ${message}`)
 
 
15
  ),
16
  transports: [
17
  new winston.transports.Console(),
 
19
  ]
20
  });
21
 
22
+ const TMDB_API_KEY = process.env.TMDB_API_KEY;
23
+ const TMDB_BASE_URL = process.env.TMDB_BASE_URL;
24
+ const CACHE_DIR = path.join(__dirname, process.env.CACHE_DIR);
25
  const CATALOG_CACHE_FILE = path.join(CACHE_DIR, 'catalog_cache.json');
26
  const META_CACHE_FILE = path.join(CACHE_DIR, 'meta_cache.json');
27
 
28
+ const START_YEAR = process.env.START_YEAR || 2015;
29
+ const END_YEAR = process.env.END_YEAR || 2024;
30
+ const MIN_SCORE = process.env.MIN_SCORE || 4;
31
+ const NUM_PAGES = process.env.NUM_PAGES || 5;
32
+ const EXCLUDED_GENRES = process.env.EXCLUDED_GENRES || '16,10402';
33
+ const INCLUDED_GENRES = process.env.INCLUDED_GENRES || '28,12,35,14'; // Single genres
34
+ const INCLUDED_GENRES_OR = process.env.INCLUDED_GENRES_OR || '28|12|35|14'; // OR combinations
35
+ const LANGUAGE = process.env.LANGUAGE || 'en';
36
+
37
+ // Genre Combinations for querying
38
+ const GENRE_COMBINATIONS = [
39
+ '28,12', // Action, Adventure
40
+ '28,35', // Action, Comedy
41
+ '28,14', // Action, Fantasy
42
+ '14,12', // Fantasy, Adventure
43
+ '14,35' // Fantasy, Comedy
44
+ ];
45
+
46
  let catalogCache = {};
47
  let metaCache = {};
48
 
 
49
  const manifest = {
50
+ id: 'org.stremio.action-addon',
51
  version: '1.0.0',
52
+ name: 'Action Varied',
53
+ description: 'Stremio Add-on to showcase movies with configurable filters',
54
  catalogs: [
55
  {
 
 
56
  type: 'Easynews',
57
+ id: 'action-movies-catalog',
58
+ name: 'Action Varied',
59
  extra: [{ name: 'search', isRequired: false }]
60
  }
61
  ],
 
103
  }
104
  }
105
 
106
+ async function fetchMoviesForCombination(genreCombination) {
 
 
 
 
107
  let page = 1;
108
+ let totalPages = 1;
109
  const allResults = [];
110
 
111
+ try {
112
+ do {
113
+ const url = `${TMDB_BASE_URL}/discover/movie?api_key=${TMDB_API_KEY}&include_adult=false&include_video=false&language=${LANGUAGE}&page=${page}&sort_by=primary_release_date.desc&primary_release_date.gte=${START_YEAR}-01-01&primary_release_date.lte=${END_YEAR}-12-31&vote_average.gte=${MIN_SCORE}&with_genres=${genreCombination}&with_original_language=${LANGUAGE}&without_genres=${EXCLUDED_GENRES}`;
114
+ logger.debug(`Requesting TMDB: ${url}`);
115
 
 
116
  const response = await axios.get(url);
117
  const { results, total_pages } = response.data;
118
+ totalPages = total_pages;
119
 
120
+ logger.debug(`Page ${page}/${totalPages} fetched for genre combination ${genreCombination}. Results: ${results.length}`);
121
 
122
+ const validResults = results.filter(movie => movie.release_date && !isNaN(new Date(movie.release_date)));
123
+ allResults.push(...validResults);
 
 
124
 
125
  page++;
126
+ } while (page <= NUM_PAGES && page <= totalPages);
127
+
128
+ return allResults;
129
+ } catch (error) {
130
+ logger.error(`Error fetching movies for genre combination ${genreCombination}:`, error);
131
+ return [];
132
+ }
133
+ }
134
+
135
+ async function fetchMovies() {
136
+ const allResults = [];
137
+
138
+ for (const combination of GENRE_COMBINATIONS) {
139
+ const results = await fetchMoviesForCombination(combination);
140
+ allResults.push(...results);
141
  }
142
 
143
  allResults.sort((a, b) => new Date(b.release_date) - new Date(a.release_date));
144
+ logger.info(`Total movies fetched: ${allResults.length}`);
145
  return allResults;
146
  }
147
 
 
149
  try {
150
  const url = `${TMDB_BASE_URL}/movie/${movieId}?api_key=${TMDB_API_KEY}`;
151
  const response = await axios.get(url);
 
152
  return response.data;
153
  } catch (error) {
154
+ logger.error(`Error fetching metadata for movie ID ${movieId}:`, error.message);
155
  return { error: 'Failed to fetch movie data' };
156
  }
157
  }
 
159
  async function updateCatalogAndMetaCache() {
160
  logger.info('Updating catalog and meta cache...');
161
  try {
162
+ const results = await fetchMovies();
163
  catalogCache = {
164
  timestamp: Date.now(),
165
  data: results
166
  };
167
 
 
168
  for (const movie of results) {
169
  const movieId = movie.id;
170
  logger.debug(`Fetching meta for movie ID: ${movieId}`);
 
199
  const metas = results.map(item => ({
200
  id: `tmdb-${item.id}`,
201
  type: 'movie',
202
+ name: item.title,
203
  poster: item.poster_path ? `https://image.tmdb.org/t/p/w500${item.poster_path}` : null,
204
  description: item.overview || 'No description available',
205
  releaseInfo: item.release_date ? item.release_date.split('-')[0] : 'Unknown'
 
236
  backdrop_path
237
  } = movieData;
238
 
239
+ logger.info(`Meta request processed for movie ID: ${id}`);
240
  return {
241
  meta: {
242
  id: args.id,
 
274
  }
275
 
276
  const interface = builder.getInterface();
277
+ const port = process.env.PORT || 8082;
278
 
279
  logger.info(`Starting Stremio Add-on server on port ${port}`);
280
  serveHTTP(interface, { port });
package-lock.json CHANGED
@@ -1,15 +1,16 @@
1
  {
2
- "name": "stremio-ufc-addon",
3
  "version": "1.2.0",
4
  "lockfileVersion": 2,
5
  "requires": true,
6
  "packages": {
7
  "": {
8
- "name": "stremio-ufc-addon",
9
  "version": "1.2.0",
10
  "license": "MIT",
11
  "dependencies": {
12
  "axios": "^0.27.2",
 
13
  "node-schedule": "^2.1.0",
14
  "stremio-addon-sdk": "^1.6.10",
15
  "winston": "^3.8.2"
@@ -342,6 +343,17 @@
342
  "npm": "1.2.8000 || >= 1.4.16"
343
  }
344
  },
 
 
 
 
 
 
 
 
 
 
 
345
  "node_modules/ee-first": {
346
  "version": "1.1.1",
347
  "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
@@ -1726,6 +1738,11 @@
1726
  "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz",
1727
  "integrity": "sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg=="
1728
  },
 
 
 
 
 
1729
  "ee-first": {
1730
  "version": "1.1.1",
1731
  "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
 
1
  {
2
+ "name": "stremio-custom-addon",
3
  "version": "1.2.0",
4
  "lockfileVersion": 2,
5
  "requires": true,
6
  "packages": {
7
  "": {
8
+ "name": "stremio-custom-addon",
9
  "version": "1.2.0",
10
  "license": "MIT",
11
  "dependencies": {
12
  "axios": "^0.27.2",
13
+ "dotenv": "^16.0.3",
14
  "node-schedule": "^2.1.0",
15
  "stremio-addon-sdk": "^1.6.10",
16
  "winston": "^3.8.2"
 
343
  "npm": "1.2.8000 || >= 1.4.16"
344
  }
345
  },
346
+ "node_modules/dotenv": {
347
+ "version": "16.4.5",
348
+ "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz",
349
+ "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==",
350
+ "engines": {
351
+ "node": ">=12"
352
+ },
353
+ "funding": {
354
+ "url": "https://dotenvx.com"
355
+ }
356
+ },
357
  "node_modules/ee-first": {
358
  "version": "1.1.1",
359
  "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
 
1738
  "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz",
1739
  "integrity": "sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg=="
1740
  },
1741
+ "dotenv": {
1742
+ "version": "16.4.5",
1743
+ "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz",
1744
+ "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg=="
1745
+ },
1746
  "ee-first": {
1747
  "version": "1.1.1",
1748
  "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
package.json CHANGED
@@ -1,16 +1,17 @@
1
  {
2
- "name": "stremio-ufc-addon",
3
  "version": "1.2.0",
4
- "description": "Stremio Add-on to showcase UFC movies from TMDB with daily updates",
5
- "main": "addon.js",
6
  "scripts": {
7
  "start": "node index.js"
8
  },
9
  "keywords": [
10
  "stremio",
11
  "addon",
12
- "ufc",
13
- "movies"
 
14
  ],
15
  "author": "Your Name",
16
  "license": "MIT",
@@ -18,7 +19,8 @@
18
  "stremio-addon-sdk": "^1.6.10",
19
  "axios": "^0.27.2",
20
  "winston": "^3.8.2",
21
- "node-schedule": "^2.1.0"
 
22
  },
23
  "engines": {
24
  "node": ">=14.0.0"
 
1
  {
2
+ "name": "stremio-custom-addon",
3
  "version": "1.2.0",
4
+ "description": "Stremio Add-on to showcase custom movies based on TMDB with configurable filters",
5
+ "main": "index.js",
6
  "scripts": {
7
  "start": "node index.js"
8
  },
9
  "keywords": [
10
  "stremio",
11
  "addon",
12
+ "movies",
13
+ "tmdb",
14
+ "custom"
15
  ],
16
  "author": "Your Name",
17
  "license": "MIT",
 
19
  "stremio-addon-sdk": "^1.6.10",
20
  "axios": "^0.27.2",
21
  "winston": "^3.8.2",
22
+ "node-schedule": "^2.1.0",
23
+ "dotenv": "^16.0.3"
24
  },
25
  "engines": {
26
  "node": ">=14.0.0"