53 lines
1.6 KiB
JavaScript
53 lines
1.6 KiB
JavaScript
// Port of tmdbintegral/tmdb2imdb.php
|
|
// Builds bidirectional TMDb <-> IMDb id mappings from cached detail files.
|
|
|
|
import { createReadStream, existsSync, readFileSync } from 'node:fs';
|
|
import { writeFile } from 'node:fs/promises';
|
|
import { createInterface } from 'node:readline';
|
|
import { join } from 'node:path';
|
|
import { TMDBINTEGRAL_DIR } from '../config.js';
|
|
import { entryPath } from '../lib/paths.js';
|
|
|
|
export async function buildMapping(type) {
|
|
const inputFile = join(TMDBINTEGRAL_DIR, `${type}.json`);
|
|
const out1 = join(TMDBINTEGRAL_DIR, `${type}2imdb.json`);
|
|
const out2 = join(TMDBINTEGRAL_DIR, `imdb2${type}.json`);
|
|
|
|
const data1 = {};
|
|
const data2 = {};
|
|
|
|
const stream = createReadStream(inputFile, { encoding: 'utf8' });
|
|
const rl = createInterface({ input: stream, crlfDelay: Infinity });
|
|
|
|
for await (const line of rl) {
|
|
if (!line) continue;
|
|
let obj;
|
|
try { obj = JSON.parse(line); } catch { continue; }
|
|
const tmdb = obj.id;
|
|
const path = entryPath(type, tmdb);
|
|
if (!existsSync(path)) continue;
|
|
let detail;
|
|
try { detail = JSON.parse(readFileSync(path, 'utf8')); } catch { continue; }
|
|
const imdb = detail?.external_ids?.imdb_id;
|
|
if (imdb) {
|
|
data1[tmdb] = imdb;
|
|
data2[imdb] = tmdb;
|
|
}
|
|
}
|
|
|
|
await writeFile(out1, JSON.stringify(data1));
|
|
await writeFile(out2, JSON.stringify(data2));
|
|
}
|
|
|
|
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
const type = process.argv[2];
|
|
if (type !== 'movie' && type !== 'tv') {
|
|
console.error('Usage: node cron/tmdb2imdb.js movie|tv');
|
|
process.exit(1);
|
|
}
|
|
buildMapping(type).catch((err) => {
|
|
console.error(err);
|
|
process.exit(1);
|
|
});
|
|
}
|