var fs = require('fs'), path = require('path'), readTorrent = require('read-torrent'), argv = require('minimist')(process.argv.slice(2)); var torrentFiles = {}; var torrentCsvHashes = new Set(); var jsonFile = '../torrent_files.json'; var torrentsCsvFile = '../torrents.csv'; console.log(`Scanning torrent files from ${argv.dir} into ${jsonFile} ...`); main(); async function main() { await fillTorrentFiles(); await fillTorrentCsvHashes(); await scanFolder(); writeFile(); } async function fillTorrentFiles() { if (fs.existsSync(jsonFile)) { var fileContents = await fs.promises.readFile(jsonFile, 'utf8'); torrentFiles = JSON.parse(fileContents); } } async function fillTorrentCsvHashes() { var fileContents = await fs.promises.readFile(torrentsCsvFile, 'utf8'); var lines = fileContents.split('\n'); for (const line of lines) { var hash = line.split(';')[0]; torrentCsvHashes.add(hash); } torrentCsvHashes.delete('infohash'); } async function scanFolder() { console.log('Scanning dir: ' + argv.dir + '...'); var files = fs.readdirSync(argv.dir).filter(f => { var f = f.split('.'); var ext = f[1]; var hash = f[0]; // It must be a torrent file, NOT already be in the files json, // and be an infohash in the csv file. return (ext == 'torrent' && !Object.keys(torrentFiles).includes(hash)); }); for (const file of files) { var fullPath = argv.dir + '/' + file; console.log(`Scanning File ${fullPath}`); var torrent = await read(fullPath).catch(e => console.log('Read error')); torrentFiles = { ...torrentFiles, ...torrent }; // concat them }; console.log('Done scanning.') } function writeFile() { torrentFiles = Object.keys(torrentFiles) .sort() .filter(hash => torrentCsvHashes.has(hash)) .reduce((r, k) => (r[k] = torrentFiles[k], r), {}); fs.writeFileSync(jsonFile, JSON.stringify(torrentFiles)); console.log(`${jsonFile} written.`); } function read(uri, options) { return new Promise((resolve, reject) => { readTorrent(uri, (err, info) => { if (!err) { // Removing some extra fields from files if (info.files) { info.files.forEach((f, i) => { f.i = i; f.p = f.path; f.l = f.length; delete f.name; delete f.offset; delete f.path; delete f.length; }); } resolve({ [info.infoHash]: info.files }); } else { console.error('Error in read-torrent: ' + err.message + ' for torrent uri: ' + uri); reject(err); } }); }); }