31 lines
1.9 KiB
JavaScript
31 lines
1.9 KiB
JavaScript
#!/usr/bin/env node
|
|
'use strict';
|
|
// Read-only CLI. Never writes the reader's discovery.json or scraper queue.
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const { loadCatalog } = require('../greatest');
|
|
const { generate } = require('./engine');
|
|
const { DiscoveryCache } = require('./cache');
|
|
|
|
if (!process.argv.includes('--dry-run')) {
|
|
console.error('Usage: node discovery/cli.js run --dry-run [--data DIR] [--catalog FILE] [--show]');
|
|
process.exit(2);
|
|
}
|
|
const arg = (name, fallback) => { const i = process.argv.indexOf(name); return i < 0 ? fallback : process.argv[i + 1]; };
|
|
const data = path.resolve(arg('--data', process.env.DATA || 'data'));
|
|
const db = JSON.parse(fs.readFileSync(path.join(data, 'db.json'), 'utf8'));
|
|
let library = [];
|
|
try { library = JSON.parse(fs.readFileSync(path.join(data, 'library-index.json'), 'utf8')).mangas || []; }
|
|
catch (error) { if (error.code !== 'ENOENT') throw error; }
|
|
const greatest = loadCatalog(arg('--catalog', path.join(__dirname, '..', 'greatest', 'catalog.json')));
|
|
// The harvest cache is only read here (get/list, never put); note get/list still drop a corrupt entry file.
|
|
const harvestDir = path.join(data, 'discovery-cache');
|
|
const harvestCache = fs.existsSync(harvestDir) ? new DiscoveryCache(harvestDir) : null;
|
|
const result = generate({ db, library, greatest, cacheDir: path.join(data, 'scraper', 'metadata', 'anilist'), harvestCache });
|
|
const summary = { examined: result.examined, candidates: Object.keys(result.candidates).length,
|
|
optedInUsers: Object.values(db.users || {}).filter(u => u.settings?.discoveryEnabled).length,
|
|
feedSizes: Object.values(result.feeds).filter(f => !f.disabled).map(f => f.items.length) };
|
|
if (process.argv.includes('--show')) summary.titles = Object.values(result.feeds).find(f => !f.disabled)?.items
|
|
.map(item => result.candidates[item.candidateId]?.title) || [];
|
|
console.log(JSON.stringify(summary, null, 2));
|