Upload 5 files
Browse filesFiles changed (5)
11231 unmodified line56789101112131314151617181 unmodified line20212221232425262728272829303132333435363729303132334234354445363738394849404142514344454641 unmodified lines888990999192939495961019798104105106107108109110111112113114115116991001011021031041051201061221071081241251091101111271281121131301311321331341351141151161171181191201211221231241251261271281291301311321451331341351361 unmodified line13813914015314114214314414514614714814915019 unmodified lines1701711721841731741751762 unmodified lines17918018119318218318418530 unmodified lines21621721823021922022122219 unmodified lines2422432442452462472482492502512 unmodified lines254255256266257258259260require('dotenv').config();const { addonBuilder, serveHTTP } = require('stremio-addon-sdk');const axios = require('axios');const fs = require('fs').promises;1 unmodified lineconst winston = require('winston');const schedule = require('node-schedule');
// Winston logger configurationconst logger = winston.createLogger({ level: 'debug', format: winston.format.combine( winston.format.timestamp(), winston.format.printf(({ level, message, timestamp }) => `${timestamp} ${level}: ${message}`) winston.format.printf(({ level, message, timestamp }) => { return `${timestamp} ${level}: ${message}`; }) ), transports: [ new winston.transports.Console(),1 unmodified line ]});
const TMDB_API_KEY = process.env.TMDB_API_KEY;const TMDB_API_KEY = 'f051e7366c6105ad4f9aafe4733d9dae';const TMDB_BASE_URL = 'https://api.themoviedb.org/3';const CACHE_DIR = path.join(__dirname, 'cache');const CATALOG_CACHE_FILE = path.join(CACHE_DIR, 'catalog_cache.json');const META_CACHE_FILE = path.join(CACHE_DIR, 'meta_cache.json');
const YEAR_START = process.env.YEAR_START || 2000;const YEAR_END = process.env.YEAR_END || new Date().getFullYear();const MIN_SCORE = parseFloat(process.env.MIN_SCORE) || 4;const MAX_PAGES = parseInt(process.env.MAX_PAGES, 10) || 10;const PAGE_SIZE = parseInt(process.env.PAGE_SIZE, 10) || 25;
const GENRES = process.env.GENRES.split(',').map(Number);const GENRES_OR = process.env.GENRES_OR ? process.env.GENRES_OR.split('|').map(Number) : [];const EXCLUDE_GENRES = process.env.EXCLUDE_GENRES.split(',').map(Number);const EXCLUDE_GENRES_OR = process.env.EXCLUDE_GENRES_OR ? process.env.EXCLUDE_GENRES_OR.split('|').map(Number) : [];
let catalogCache = {};let metaCache = {};
// Manifestconst manifest = { id: 'org.stremio.custom-movies', id: 'org.stremio.horror-movies', version: '1.0.0', name: 'Action Fantasy', description: 'Stremio Add-on to showcase movies based on custom search terms', name: 'Horror Movies Since 2015', description: 'Stremio Add-on to showcase Horror movies from the year 2015 onward', catalogs: [ { id: 'custom-movies-catalog', name: 'Action Fantasy', id: 'horror-movie-catalog', name: 'Horror Movies', type: 'Easynews', pageSize: PAGE_SIZE, pageSize: 25, extra: [{ name: 'search', isRequired: false }] } ],41 unmodified lines }}
async function fetchMovies() {async function fetchHorrorMovies() { const pageSize = 20; // Number of results per page const maxPages = 40; // Limit to prevent too many requests const startYear = 2021;
let page = 1; let totalPages = 1; const allResults = [];
const genreQuery = GENRES.length ? `with_genres=${GENRES.join(',')}` : ''; const genreOrQuery = GENRES_OR.length ? `with_genres=${GENRES_OR.join('|')}` : ''; const excludeGenreQuery = EXCLUDE_GENRES.length ? `without_genres=${EXCLUDE_GENRES.join(',')}` : ''; const excludeGenreOrQuery = EXCLUDE_GENRES_OR.length ? `without_genres=${EXCLUDE_GENRES_OR.join('|')}` : ''; const combinedFilters = [genreQuery, genreOrQuery, excludeGenreQuery, excludeGenreOrQuery] .filter(Boolean) .join('&'); try { do { const url = `${TMDB_BASE_URL}/discover/movie?api_key=${TMDB_API_KEY}&include_adult=false&include_video=false&language=en&page=${page}&sort_by=primary_release_date.desc&primary_release_date.gte=${YEAR_START}-01-01&primary_release_date.lte=${YEAR_END}-12-31&vote_average.gte=${MIN_SCORE}&${combinedFilters}`; logger.debug(`Requesting TMDB: ${url}`); while (page <= maxPages) { 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`; logger.debug(`Requesting TMDB: ${url}`);
try { const response = await axios.get(url); const { results, total_pages } = response.data; totalPages = total_pages;
logger.debug(`Page ${page}/${totalPages} fetched. Results: ${results.length}`); if (results.length === 0) break; // Exit if no more results
const validResults = results.filter(movie => movie.release_date && !isNaN(new Date(movie.release_date))); allResults.push(...validResults); logger.debug(`Page ${page} fetched. Results: ${results.length}`); allResults.push(...results);
page++; } while (page <= totalPages && page <= MAX_PAGES); if (page >= total_pages) break; // Exit if we've reached the last page
allResults.sort((a, b) => new Date(b.release_date) - new Date(a.release_date)); logger.info(`Total movies fetched: ${allResults.length}`); return allResults; } catch (error) { logger.error('Error fetching movies:', error); return []; page++; } catch (error) { logger.error(`Error fetching page ${page}: ${error.message}`); break; // Stop fetching if an error occurs } }
allResults.sort((a, b) => new Date(b.release_date) - new Date(a.release_date)); logger.info(`Total Horror movies fetched since ${startYear}: ${allResults.length}`); return allResults;}
async function fetchMovieMetadata(movieId) { try { const url = `${TMDB_BASE_URL}/movie/${movieId}?api_key=${TMDB_API_KEY}`; const response = await axios.get(url); logger.debug(`Metadata fetched for movie ID ${movieId}: ${JSON.stringify(response.data)}`); return response.data; } catch (error) { logger.error(`Error fetching metadata for movie ID ${movieId}:`, error.message); logger.error(`Error fetching metadata for movie ID ${movieId}: ${error.message}`); return { error: 'Failed to fetch movie data' }; }}1 unmodified lineasync function updateCatalogAndMetaCache() { logger.info('Updating catalog and meta cache...'); try { const results = await fetchMovies(); const results = await fetchHorrorMovies(); catalogCache = { timestamp: Date.now(), data: results };
// Fetch meta information for all movies for (const movie of results) { const movieId = movie.id; logger.debug(`Fetching meta for movie ID: ${movieId}`);19 unmodified lines logger.debug(`Catalog cache has ${results.length} items`);
if (searchQuery) { results = results.filter(movie => results = results.filter(movie => movie.title.toLowerCase().includes(searchQuery.toLowerCase()) ); logger.debug(`Filtered results for "${searchQuery}": ${results.length}`);2 unmodified lines const metas = results.map(item => ({ id: `tmdb-${item.id}`, type: 'movie', name: item.title, name: item.title || 'Unknown Title', poster: item.poster_path ? `https://image.tmdb.org/t/p/w500${item.poster_path}` : null, description: item.overview || 'No description available', releaseInfo: item.release_date ? item.release_date.split('-')[0] : 'Unknown'30 unmodified lines backdrop_path } = movieData;
logger.info(`Meta request processed for movie ID: ${id}`); logger.debug(`Metadata for movie ID ${id}: ${JSON.stringify(movieData)}`); return { meta: { id: args.id,19 unmodified lines await updateCatalogAndMetaCache(); }
// Schedule cache refresh every day at midnight schedule.scheduleJob('0 0 * * *', refreshCache);
// Initial cache refresh if needed if (!catalogCache.timestamp || Date.now() - catalogCache.timestamp > 24 * 60 * 60 * 1000) { logger.info('Initial cache refresh needed'); await refreshCache();2 unmodified lines }
const interface = builder.getInterface(); const port = process.env.PORT || 3000; const port = process.env.PORT || 5056;
logger.info(`Starting Stremio Add-on server on port ${port}`); serveHTTP(interface, { port });9 unmodified lines10111213131415326 unmodified lines3423433443463473483493503513523533543553563453463471378 unmodified lines172617271728174117421743174417451729173017319 unmodified lines "license": "MIT", "dependencies": { "axios": "^0.27.2", "dotenv": "^16.4.5", "node-schedule": "^2.1.0", "stremio-addon-sdk": "^1.6.10", "winston": "^3.8.2"326 unmodified lines "npm": "1.2.8000 || >= 1.4.16" } }, "node_modules/dotenv": { "version": "16.4.5", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz", "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==", "engines": { "node": ">=12" }, "funding": { "url": "https://dotenvx.com" } }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",1378 unmodified lines "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz", "integrity": "sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==" }, "dotenv": { "version": "16.4.5", "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz", "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==" }, "ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",14 unmodified lines151617181920181922202122232414 unmodified lines "author": "Your Name", "license": "MIT", "dependencies": { "axios": "^0.27.2", "dotenv": "^16.4.5", "node-schedule": "^2.1.0", "stremio-addon-sdk": "^1.6.10", "axios": "^0.27.2", "winston": "^3.8.2" "winston": "^3.8.2", "node-schedule": "^2.1.0" }, "engines": { "node": ">=14.0.0"cache/catalog_cache.json
cache/meta_cache.json
index.js+44-53
package-lock.json-17
package.json+3-4