/** * Fetch, parse, validate and store recent Kerala lottery results. * npm run results last 7 days from the portal * npm run results -- --days 5 --force * npm run results -- --pdf-dir ./pdfs parse PDFs you already have (no network) * Exit code: 0 all fine, 2 a draw was held, 3 a fetch failed. */ import { readdirSync, readFileSync } from 'node:fs'; import { join } from 'node:path'; import { loadConfig } from '../src/config'; import { openDb } from '../src/db'; import { runMigrations } from '../src/db/migrate'; import { processPdf, runResultsJob } from '../src/results/job'; import { hasErrors } from '../src/results/parseResult'; import { storeDraw } from '../src/results/store'; function arg(name: string): string | undefined { const i = process.argv.indexOf(`--${name}`); return i >= 0 ? process.argv[i + 1] : undefined; } async function main(): Promise { const config = loadConfig(); const db = openDb(config.dbPath); runMigrations(db); const dir = arg('pdf-dir'); if (dir) { let held = 0; for (const f of readdirSync(dir).filter((n) => n.toLowerCase().endsWith('.pdf')).sort()) { const { draw, issues } = await processPdf(readFileSync(join(dir, f))); if (draw) storeDraw(db, draw, issues); if (draw && !hasErrors(issues)) { console.log(`${f}: ${draw.schemeCode}-${draw.drawNo} ${draw.drawDate} ready`); } else { held++; console.log(`${f}: HELD ${issues.map((i) => i.message).join('; ')}`); } } return held ? 2 : 0; } const days = arg('days'); const today = arg('today'); const reports = await runResultsJob({ db, force: process.argv.includes('--force'), ...(days ? { days: Number(days) } : {}), ...(today ? { today } : {}), }); for (const r of reports) { const detail = r.issues.length ? ' - ' + r.issues.map((i) => `${i.level} ${i.message}`).join('; ') : ''; console.log(`${r.code} ${r.date}: ${r.status}${detail}`); } if (reports.some((r) => r.status === 'fetch_failed')) return 3; return reports.some((r) => r.status === 'held') ? 2 : 0; } main().then( (code) => process.exit(code), (err) => { console.error(err instanceof Error ? err.message : err); process.exit(3); }, );