Spaces:
Paused
Paused
Upload 6 files
Browse files- .env +19 -22
- 2index.js +265 -0
- cache/catalog_cache.json +0 -0
- cache/meta_cache.json +0 -0
.env
CHANGED
|
@@ -1,33 +1,30 @@
|
|
| 1 |
# TMDB API Key
|
| 2 |
TMDB_API_KEY=f051e7366c6105ad4f9aafe4733d9dae
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
META_CACHE_FILE=cache/meta_cache.json
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
END_YEAR=2024
|
| 17 |
-
MIN_SCORE=5
|
| 18 |
-
NUM_PAGES=25
|
| 19 |
|
| 20 |
-
# Genres
|
| 21 |
-
|
| 22 |
|
| 23 |
-
# Genres
|
| 24 |
-
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
|
|
|
| 1 |
# TMDB API Key
|
| 2 |
TMDB_API_KEY=f051e7366c6105ad4f9aafe4733d9dae
|
| 3 |
|
| 4 |
+
# Year range for filtering movies
|
| 5 |
+
YEAR_START=2015
|
| 6 |
+
YEAR_END=2024
|
| 7 |
|
| 8 |
+
# Minimum average rating (score) for movies
|
| 9 |
+
MIN_SCORE=5
|
| 10 |
|
| 11 |
+
# Maximum number of pages to fetch from TMDB
|
| 12 |
+
MAX_PAGES=25
|
|
|
|
| 13 |
|
| 14 |
+
# Number of results per page
|
| 15 |
+
PAGE_SIZE=25
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Genres for filtering movies (AND condition)
|
| 18 |
+
GENRES=28,14 # Action and Adventure combined (AND condition)
|
| 19 |
|
| 20 |
+
# OR Genres for filtering movies
|
| 21 |
+
GENRES_OR=28|14 # Action or Adventure (OR condition)
|
| 22 |
|
| 23 |
+
# Ex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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
require('dotenv').config();
|
| 2 |
+
|
| 3 |
+
const { addonBuilder, serveHTTP } = require('stremio-addon-sdk');
|
| 4 |
+
const axios = require('axios');
|
| 5 |
+
const fs = require('fs').promises;
|
| 6 |
+
const path = require('path');
|
| 7 |
+
const winston = require('winston');
|
| 8 |
+
const schedule = require('node-schedule');
|
| 9 |
+
|
| 10 |
+
// Load environment variables
|
| 11 |
+
const TMDB_API_KEY = process.env.TMDB_API_KEY;
|
| 12 |
+
const YEAR_START = process.env.YEAR_START || 2015;
|
| 13 |
+
const YEAR_END = process.env.YEAR_END || 2024;
|
| 14 |
+
const MIN_SCORE = process.env.MIN_SCORE || 4;
|
| 15 |
+
const MAX_PAGES = process.env.MAX_PAGES || 10;
|
| 16 |
+
const PAGE_SIZE = process.env.PAGE_SIZE || 25;
|
| 17 |
+
|
| 18 |
+
// Winston logger configuration
|
| 19 |
+
const logger = winston.createLogger({
|
| 20 |
+
level: 'debug',
|
| 21 |
+
format: winston.format.combine(
|
| 22 |
+
winston.format.timestamp(),
|
| 23 |
+
winston.format.printf(({ level, message, timestamp }) => {
|
| 24 |
+
return `${timestamp} ${level}: ${message}`;
|
| 25 |
+
})
|
| 26 |
+
),
|
| 27 |
+
transports: [
|
| 28 |
+
new winston.transports.Console(),
|
| 29 |
+
new winston.transports.File({ filename: 'addon.log' })
|
| 30 |
+
]
|
| 31 |
+
});
|
| 32 |
+
|
| 33 |
+
const TMDB_BASE_URL = 'https://api.themoviedb.org/3';
|
| 34 |
+
const CACHE_DIR = path.join(__dirname, 'cache');
|
| 35 |
+
const CATALOG_CACHE_FILE = path.join(CACHE_DIR, 'catalog_cache.json');
|
| 36 |
+
const META_CACHE_FILE = path.join(CACHE_DIR, 'meta_cache.json');
|
| 37 |
+
|
| 38 |
+
let catalogCache = {};
|
| 39 |
+
let metaCache = {};
|
| 40 |
+
|
| 41 |
+
const manifest = {
|
| 42 |
+
id: 'org.stremio.action-adventure-comedy-drama-documentary',
|
| 43 |
+
version: '1.0.0',
|
| 44 |
+
name: 'Action/Adventure/Comedy/Drama/Documentary Movies',
|
| 45 |
+
description: 'Stremio Add-on showcasing selected genres with filtering and sorting',
|
| 46 |
+
catalogs: [
|
| 47 |
+
{
|
| 48 |
+
id: 'action-adventure-comedy-drama-documentary-catalog',
|
| 49 |
+
name: 'Action/Adventure/Comedy/Drama/Documentary Movies',
|
| 50 |
+
type: 'movie',
|
| 51 |
+
pageSize: PAGE_SIZE,
|
| 52 |
+
extra: [{ name: 'search', isRequired: false }]
|
| 53 |
+
}
|
| 54 |
+
],
|
| 55 |
+
resources: ['catalog', 'meta'],
|
| 56 |
+
types: ['movie'],
|
| 57 |
+
idPrefixes: ['tmdb']
|
| 58 |
+
};
|
| 59 |
+
|
| 60 |
+
const builder = new addonBuilder(manifest);
|
| 61 |
+
|
| 62 |
+
async function ensureCacheDirectory() {
|
| 63 |
+
try {
|
| 64 |
+
await fs.mkdir(CACHE_DIR, { recursive: true });
|
| 65 |
+
logger.debug('Cache directory ensured');
|
| 66 |
+
} catch (error) {
|
| 67 |
+
logger.error('Error creating cache directory:', error);
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
async function loadCache() {
|
| 72 |
+
try {
|
| 73 |
+
const catalogData = await fs.readFile(CATALOG_CACHE_FILE, 'utf8');
|
| 74 |
+
catalogCache = JSON.parse(catalogData);
|
| 75 |
+
logger.debug(`Catalog cache loaded. Items: ${catalogCache.data ? catalogCache.data.length : 0}`);
|
| 76 |
+
} catch (error) {
|
| 77 |
+
logger.info('No existing catalog cache found. Will create a new one.');
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
try {
|
| 81 |
+
const metaData = await fs.readFile(META_CACHE_FILE, 'utf8');
|
| 82 |
+
metaCache = JSON.parse(metaData);
|
| 83 |
+
logger.debug(`Meta cache loaded. Items: ${Object.keys(metaCache).length}`);
|
| 84 |
+
} catch (error) {
|
| 85 |
+
logger.info('No existing meta cache found. Will create a new one.');
|
| 86 |
+
}
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
async function saveCache() {
|
| 90 |
+
try {
|
| 91 |
+
await fs.writeFile(CATALOG_CACHE_FILE, JSON.stringify(catalogCache));
|
| 92 |
+
await fs.writeFile(META_CACHE_FILE, JSON.stringify(metaCache));
|
| 93 |
+
logger.debug('Cache saved successfully');
|
| 94 |
+
} catch (error) {
|
| 95 |
+
logger.error('Error saving cache:', error);
|
| 96 |
+
}
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
async function fetchMoviesWithNewQuery() {
|
| 100 |
+
let page = 1;
|
| 101 |
+
let totalPages = 1;
|
| 102 |
+
const allResults = [];
|
| 103 |
+
|
| 104 |
+
try {
|
| 105 |
+
do {
|
| 106 |
+
const url = `${TMDB_BASE_URL}/discover/movie?api_key=${TMDB_API_KEY}&include_adult=false&include_video=false&language=en&sort_by=primary_release_date.desc&with_genres=28,12,35,18,99&with_original_language=en&without_genres=16,10402&page=${page}&primary_release_date.gte=${YEAR_START}-01-01&primary_release_date.lte=${YEAR_END}-12-31&vote_average.gte=${MIN_SCORE}`;
|
| 107 |
+
logger.debug(`Requesting TMDB: ${url}`);
|
| 108 |
+
|
| 109 |
+
const response = await axios.get(url);
|
| 110 |
+
const { results, total_pages } = response.data;
|
| 111 |
+
totalPages = Math.min(total_pages, MAX_PAGES); // Limit to MAX_PAGES
|
| 112 |
+
|
| 113 |
+
logger.debug(`Page ${page}/${totalPages} fetched. Results: ${results.length}`);
|
| 114 |
+
|
| 115 |
+
allResults.push(...results);
|
| 116 |
+
|
| 117 |
+
page++;
|
| 118 |
+
} while (page <= totalPages);
|
| 119 |
+
|
| 120 |
+
allResults.sort((a, b) => new Date(b.release_date) - new Date(a.release_date));
|
| 121 |
+
logger.info(`Total movies fetched after filtering: ${allResults.length}`);
|
| 122 |
+
return allResults;
|
| 123 |
+
} catch (error) {
|
| 124 |
+
logger.error('Error fetching movies:', error);
|
| 125 |
+
return [];
|
| 126 |
+
}
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
async function fetchMovieMetadata(movieId) {
|
| 130 |
+
try {
|
| 131 |
+
const url = `${TMDB_BASE_URL}/movie/${movieId}?api_key=${TMDB_API_KEY}`;
|
| 132 |
+
const response = await axios.get(url);
|
| 133 |
+
return response.data;
|
| 134 |
+
} catch (error) {
|
| 135 |
+
logger.error(`Error fetching metadata for movie ID ${movieId}:`, error.message);
|
| 136 |
+
return { error: 'Failed to fetch movie data' };
|
| 137 |
+
}
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
async function updateCatalogAndMetaCache() {
|
| 141 |
+
logger.info('Updating catalog and meta cache...');
|
| 142 |
+
try {
|
| 143 |
+
const results = await fetchMoviesWithNewQuery();
|
| 144 |
+
catalogCache = {
|
| 145 |
+
timestamp: Date.now(),
|
| 146 |
+
data: results
|
| 147 |
+
};
|
| 148 |
+
|
| 149 |
+
// Fetch meta information for all movies
|
| 150 |
+
for (const movie of results) {
|
| 151 |
+
const movieId = movie.id;
|
| 152 |
+
logger.debug(`Fetching meta for movie ID: ${movieId}`);
|
| 153 |
+
const movieData = await fetchMovieMetadata(movieId);
|
| 154 |
+
metaCache[movieId] = {
|
| 155 |
+
timestamp: Date.now(),
|
| 156 |
+
data: movieData
|
| 157 |
+
};
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
await saveCache();
|
| 161 |
+
logger.info(`Catalog and meta cache updated. Items: ${results.length}`);
|
| 162 |
+
} catch (error) {
|
| 163 |
+
logger.error('Error updating catalog and meta cache:', error);
|
| 164 |
+
}
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
builder.defineCatalogHandler(async (args) => {
|
| 168 |
+
const searchQuery = args.extra && args.extra.search ? args.extra.search : '';
|
| 169 |
+
logger.debug(`Catalog request received. Query: "${searchQuery}"`);
|
| 170 |
+
|
| 171 |
+
let results = catalogCache.data || [];
|
| 172 |
+
logger.debug(`Catalog cache has ${results.length} items`);
|
| 173 |
+
|
| 174 |
+
if (searchQuery) {
|
| 175 |
+
results = results.filter(movie =>
|
| 176 |
+
movie.title.toLowerCase().includes(searchQuery.toLowerCase())
|
| 177 |
+
);
|
| 178 |
+
logger.debug(`Filtered results for "${searchQuery}": ${results.length}`);
|
| 179 |
+
}
|
| 180 |
+
|
| 181 |
+
const metas = results.map(item => ({
|
| 182 |
+
id: `tmdb-${item.id}`,
|
| 183 |
+
type: 'movie',
|
| 184 |
+
name: item.title,
|
| 185 |
+
poster: item.poster_path ? `https://image.tmdb.org/t/p/w500${item.poster_path}` : null,
|
| 186 |
+
description: item.overview || 'No description available',
|
| 187 |
+
releaseInfo: item.release_date ? item.release_date.split('-')[0] : 'Unknown'
|
| 188 |
+
}));
|
| 189 |
+
|
| 190 |
+
logger.info(`Catalog request processed. Query: "${searchQuery}", Results: ${metas.length}`);
|
| 191 |
+
return { metas };
|
| 192 |
+
});
|
| 193 |
+
|
| 194 |
+
builder.defineMetaHandler(async (args) => {
|
| 195 |
+
const id = args.id.split('-')[1];
|
| 196 |
+
logger.debug(`Meta request received for movie ID: ${id}`);
|
| 197 |
+
|
| 198 |
+
if (!metaCache[id]) {
|
| 199 |
+
logger.warn(`Meta not found for movie ID: ${id}`);
|
| 200 |
+
return { meta: null };
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
const movieData = metaCache[id].data;
|
| 204 |
+
|
| 205 |
+
if (movieData.error) {
|
| 206 |
+
logger.warn(`Meta request failed for movie ID: ${id}`);
|
| 207 |
+
return { meta: null };
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
const {
|
| 211 |
+
title,
|
| 212 |
+
overview,
|
| 213 |
+
poster_path,
|
| 214 |
+
release_date,
|
| 215 |
+
genres,
|
| 216 |
+
runtime,
|
| 217 |
+
vote_average,
|
| 218 |
+
backdrop_path
|
| 219 |
+
} = movieData;
|
| 220 |
+
|
| 221 |
+
logger.info(`Meta request processed for movie ID: ${id}`);
|
| 222 |
+
return {
|
| 223 |
+
meta: {
|
| 224 |
+
id: args.id,
|
| 225 |
+
type: 'movie',
|
| 226 |
+
name: title || 'Unknown Title',
|
| 227 |
+
poster: poster_path ? `https://image.tmdb.org/t/p/w500${poster_path}` : null,
|
| 228 |
+
background: backdrop_path ? `https://image.tmdb.org/t/p/w1280${backdrop_path}` : null,
|
| 229 |
+
description: overview || 'No description available',
|
| 230 |
+
releaseInfo: release_date ? release_date.split('-')[0] : 'Unknown',
|
| 231 |
+
genre: genres && Array.isArray(genres) ? genres.map(g => g.name) : [],
|
| 232 |
+
runtime: runtime ? `${runtime} min` : 'Unknown',
|
| 233 |
+
rating: vote_average ? vote_average.toFixed(1) : 'N/A',
|
| 234 |
+
}
|
| 235 |
+
};
|
| 236 |
+
});
|
| 237 |
+
|
| 238 |
+
async function startServer() {
|
| 239 |
+
await ensureCacheDirectory();
|
| 240 |
+
await loadCache();
|
| 241 |
+
|
| 242 |
+
async function refreshCache() {
|
| 243 |
+
logger.info('Scheduled update: Refreshing catalog and meta cache');
|
| 244 |
+
await updateCatalogAndMetaCache();
|
| 245 |
+
}
|
| 246 |
+
|
| 247 |
+
// Schedule cache refresh every day at midnight
|
| 248 |
+
schedule.scheduleJob('0 0 * * *', refreshCache);
|
| 249 |
+
|
| 250 |
+
// Initial cache refresh if needed
|
| 251 |
+
if (!catalogCache.timestamp || Date.now() - catalogCache.timestamp > 24 * 60 * 60 * 1000) {
|
| 252 |
+
logger.info('Initial cache refresh needed');
|
| 253 |
+
await refreshCache();
|
| 254 |
+
} else {
|
| 255 |
+
logger.info('Cache is up to date. Next refresh scheduled for midnight');
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
const interface = builder.getInterface();
|
| 259 |
+
const port = process.env.PORT || 8050;
|
| 260 |
+
|
| 261 |
+
logger.info(`Starting Stremio Add-on server on port ${port}`);
|
| 262 |
+
serveHTTP(interface, { port });
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
startServer();
|
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
|
|
|