import { readFile } from 'fs/promises'; import { getArg } from './lib/args.js'; import { getMany } from './lib/dl.js'; import { error, log } from './lib/log.js'; const ctx = 'downloadDb.js'; /** * Downloads all media possible for the users stored in db.json at the specified `--path`. * Useful for first run or for augmenting existing media * if it may be only partially archived in an uncertain state. */ const downloadDb = async () => { log(ctx, 'Grabbing db'); let directory = '', threadMax = 1, db; try { directory = getArg('path'); } catch (err) { error(ctx, err); return; } try { threadMax = getArg('threads'); log(ctx, `Using ${threadMax} threads`); } catch (err) { log(ctx, 'Using 1 thread'); } try { let file = await readFile(`${directory}/db.json`, { encoding: 'utf8' }); db = JSON.parse(file); } catch (err) { error(ctx, err); return; } let processes = db.map(entry => ({ ...entry, logs: [], })); log(ctx, `Building db using /media for ${processes.length} users`); await getMany(processes, threadMax, directory); log(ctx, 'Building db using /search'); } downloadDb();