2026-04-23 08:37:48 +02:00
|
|
|
import { createWriteStream } from 'node:fs';
|
2026-04-23 10:56:45 +02:00
|
|
|
import { rename, mkdir } from 'node:fs/promises';
|
2026-04-23 08:37:48 +02:00
|
|
|
import { pipeline } from 'node:stream/promises';
|
|
|
|
|
import { createGunzip } from 'node:zlib';
|
|
|
|
|
import { Readable } from 'node:stream';
|
|
|
|
|
import { join } from 'node:path';
|
|
|
|
|
import { TMDBINTEGRAL_DIR, TMDB_EXPORTS_BASE } from '../config.js';
|
|
|
|
|
|
|
|
|
|
function formatMMDDYYYY(date) {
|
|
|
|
|
const mm = String(date.getUTCMonth() + 1).padStart(2, '0');
|
|
|
|
|
const dd = String(date.getUTCDate()).padStart(2, '0');
|
|
|
|
|
const yyyy = date.getUTCFullYear();
|
|
|
|
|
return `${mm}_${dd}_${yyyy}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function tryDownload(url, outPath) {
|
|
|
|
|
console.log(`Downloading: "${url}"`);
|
|
|
|
|
const res = await fetch(url);
|
|
|
|
|
if (res.status === 403 || res.status === 404) {
|
|
|
|
|
console.log(`Not published yet (HTTP ${res.status}): ${url}`);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!res.ok || !res.body) {
|
|
|
|
|
throw new Error(`Failed to fetch ${url}: HTTP ${res.status}`);
|
|
|
|
|
}
|
|
|
|
|
const tmp = `${outPath}.tmp`;
|
|
|
|
|
await pipeline(Readable.fromWeb(res.body), createGunzip(), createWriteStream(tmp));
|
|
|
|
|
await rename(tmp, outPath);
|
|
|
|
|
console.log(`Wrote ${outPath}`);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TMDb publishes the daily export around 08:00 UTC. If we run before that, the
|
|
|
|
|
// current-day file returns 403. Try today, then fall back to yesterday.
|
|
|
|
|
async function downloadExport(prefix, outName) {
|
|
|
|
|
const now = new Date();
|
|
|
|
|
const yesterday = new Date(now.getTime() - 86400 * 1000);
|
|
|
|
|
const out = join(TMDBINTEGRAL_DIR, outName);
|
|
|
|
|
for (const d of [now, yesterday]) {
|
|
|
|
|
const url = `${TMDB_EXPORTS_BASE}/${prefix}_${formatMMDDYYYY(d)}.json.gz`;
|
|
|
|
|
if (await tryDownload(url, out)) return;
|
|
|
|
|
}
|
|
|
|
|
throw new Error(`No TMDb ${prefix} export available for today or yesterday`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function syncExports() {
|
2026-04-23 10:56:45 +02:00
|
|
|
await mkdir(TMDBINTEGRAL_DIR, { recursive: true });
|
2026-04-23 08:37:48 +02:00
|
|
|
await downloadExport('movie_ids', 'movie.json');
|
|
|
|
|
await downloadExport('tv_series_ids', 'tv.json');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
|
|
|
syncExports().catch((err) => {
|
|
|
|
|
console.error(err);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
});
|
|
|
|
|
}
|