From 9f2220233ce5b69da0154c1e7f68ea90d57baef8 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 6 Feb 2019 17:48:49 -0800 Subject: [PATCH] Optimizing scan torrent files.js --- scripts/scan_torrent_files.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/scripts/scan_torrent_files.js b/scripts/scan_torrent_files.js index b867fd4..fe9fa61 100644 --- a/scripts/scan_torrent_files.js +++ b/scripts/scan_torrent_files.js @@ -38,14 +38,21 @@ async function fillTorrentCsvHashes() { async function scanFolder() { console.log('Scanning dir: ' + argv.dir + '...'); + var fileHashes = new Set(Object.keys(torrentFiles)); + 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. + var sp = f.split('.'); + var ext = sp[1]; + var hash = sp[0]; + var fullPath = argv.dir + '/' + f; + // It must be a torrent file, + // NOT in the torrent_files.json + // must be in the CSV file + // must have a file size return (ext == 'torrent' && - !Object.keys(torrentFiles).includes(hash)); + !fileHashes.has(hash) && + torrentCsvHashes.has(hash) && + getFilesizeInBytes(fullPath) > 0); }); for (const file of files) { var fullPath = argv.dir + '/' + file; @@ -56,12 +63,18 @@ async function scanFolder() { console.log('Done scanning.') } +function getFilesizeInBytes(filename) { + var stats = fs.statSync(filename) + var fileSizeInBytes = stats["size"] + return fileSizeInBytes +} + 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)); + fs.writeFileSync(jsonFile, JSON.stringify(torrentFiles, null, 2)); console.log(`${jsonFile} written.`); }