Optimizing scan torrent files.js
This commit is contained in:
parent
bb086e0231
commit
9f2220233c
|
@ -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.`);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue