Phase 1: lock cron, reload chaud, argon2, providers, IMDb lookup, cache LRU, /health, /metrics, rate limit, UI dark, biome
This commit is contained in:
35
lib/imdbMapping.js
Normal file
35
lib/imdbMapping.js
Normal file
@@ -0,0 +1,35 @@
|
||||
// Loads imdb2movie.json / imdb2tv.json into memory and reloads on mtime change.
|
||||
|
||||
import { readFile, stat } from 'node:fs/promises';
|
||||
import { join } from 'node:path';
|
||||
import { TMDBINTEGRAL_DIR } from '../config.js';
|
||||
|
||||
const cache = { movie: null, tv: null };
|
||||
const mtimes = { movie: 0, tv: 0 };
|
||||
|
||||
async function loadOne(type) {
|
||||
const file = join(TMDBINTEGRAL_DIR, `imdb2${type}.json`);
|
||||
const st = await stat(file);
|
||||
if (cache[type] && st.mtimeMs === mtimes[type]) return cache[type];
|
||||
const obj = JSON.parse(await readFile(file, 'utf8'));
|
||||
cache[type] = new Map(Object.entries(obj).map(([k, v]) => [k, Number(v)]));
|
||||
mtimes[type] = st.mtimeMs;
|
||||
return cache[type];
|
||||
}
|
||||
|
||||
export async function lookupImdb(imdbId) {
|
||||
// Try movie first, then tv (matches PHP behaviour of exhaustive lookup).
|
||||
try {
|
||||
const movieMap = await loadOne('movie');
|
||||
if (movieMap.has(imdbId)) return { type: 'movie', tmdb: movieMap.get(imdbId) };
|
||||
} catch {
|
||||
/* file may be missing on a fresh install */
|
||||
}
|
||||
try {
|
||||
const tvMap = await loadOne('tv');
|
||||
if (tvMap.has(imdbId)) return { type: 'tv', tmdb: tvMap.get(imdbId) };
|
||||
} catch {
|
||||
/* same */
|
||||
}
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user