Portage complet PHP/Bash vers Node.js (Fastify + worker_threads)
This commit is contained in:
56
cron/tmdbExports.js
Normal file
56
cron/tmdbExports.js
Normal file
@@ -0,0 +1,56 @@
|
||||
import { createWriteStream } from 'node:fs';
|
||||
import { rename } from 'node:fs/promises';
|
||||
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() {
|
||||
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);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user