Files
proxy_tmdb/cron/tmdbExports.js

58 lines
2.0 KiB
JavaScript
Raw Normal View History

import { createWriteStream } from 'node:fs';
import { mkdir, rename } from 'node:fs/promises';
import { join } from 'node:path';
import { Readable } from 'node:stream';
import { pipeline } from 'node:stream/promises';
import { createGunzip } from 'node:zlib';
import { TMDB_EXPORTS_BASE, TMDBINTEGRAL_DIR } 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() {
await mkdir(TMDBINTEGRAL_DIR, { recursive: true });
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);
});
}