-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fully functional torrent search (Laggy window issue on high results c…
…ount remains)
- Loading branch information
1 parent
b8e737f
commit c0a77ff
Showing
8 changed files
with
240 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const papa = require('papaparse'); | ||
|
||
process.on('uncaughtException', function (error) { | ||
console.log(error); | ||
process.send(['search-failed', 'general']); //mainWindow.webContents.send('search-failed', 'general'); | ||
}); | ||
|
||
let args = process.argv.slice(2); | ||
let query = args[0]; | ||
let count = parseInt(args[1]); | ||
let smart = (args[2] === 'true'); | ||
count = count > 0 ? count : 100; | ||
|
||
console.log(count); | ||
console.log(query); | ||
console.log(smart); | ||
|
||
let i = 1; | ||
let reg; | ||
let stream; | ||
let result = ''; | ||
|
||
let procData = smart ? smartSearch : regularSearch; | ||
|
||
function regularSearch(results, parser) { | ||
let stop = false; | ||
results.data.forEach(function (record) { | ||
// console.log(record['NAME']); | ||
if (record['NAME'].toUpperCase().indexOf(query.toUpperCase()) > -1) { | ||
if (i > count) { | ||
if (!stop) { | ||
parser.abort(); | ||
stream.close(); | ||
} | ||
stop = true; | ||
} else { | ||
result += '<tr><td>' + record['#ADDED'] + | ||
'</td><td class="d-none">' + record['HASH(B64)'] + | ||
'</td><td>' + record['NAME'] + | ||
'</td><td>' + record['SIZE(BYTES)'] + '</td></tr>'; | ||
// console.log(i + ' ' + record['NAME']); | ||
i++; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
function smartSearch(results, parser) { | ||
let stop = false; | ||
results.data.forEach(function (record) { | ||
// console.log(record['NAME']); | ||
if (record['NAME'].match(reg)) { | ||
if (i > count) { | ||
if (!stop) { | ||
parser.abort(); | ||
stream.close(); | ||
} | ||
stop = true; | ||
} else { | ||
result += '<tr><td>' + record['#ADDED'] + | ||
'</td><td class="d-none">' + record['HASH(B64)'] + | ||
'</td><td>' + record['NAME'] + | ||
'</td><td>' + formatBytes(record['SIZE(BYTES)'], 1) + '</td></tr>'; | ||
// console.log(i + ' ' + formatBytes(record['SIZE(BYTES)'])); | ||
i++; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
let finSearch = function () { | ||
process.send(['search-success', { | ||
resCount: --i, | ||
results: result | ||
}]); //mainWindow.webContents.send('search-failed', 'process'); | ||
console.log(process.uptime()); | ||
process.kill(process.pid); | ||
}; | ||
|
||
|
||
function search() { | ||
|
||
stream = fs.createReadStream(path.join(process.cwd(), 'data', 'processed.csv')) | ||
.once('open', function () { | ||
papa.parse(stream, { | ||
// fastMode: true, | ||
delimiter: ';', | ||
escapeChar: '\\', | ||
header: true, | ||
chunk: procData, | ||
complete: finSearch, | ||
error: function (error, file) { | ||
process.send(['search-failed', 'process']); //mainWindow.webContents.send('search-failed', 'process'); | ||
console.log(error); | ||
} | ||
}); | ||
}) | ||
.on('error', function (err) { | ||
process.send(['search-failed', 'read']); //mainWindow.webContents.send('search-failed', 'read'); | ||
console.log(err); | ||
}); | ||
|
||
} | ||
|
||
|
||
function startSearch(text) { | ||
reg = new RegExp(regexify(text), 'i'); | ||
search(); | ||
} | ||
|
||
function escapeRegExp(text) { | ||
return text.replace(/[-[\]{}()*+?.,\\/^$|#\s]/g, '\\$&'); | ||
} | ||
|
||
function regexify(text) { | ||
text = text.trim().replace(/(\s+)/g, ' '); | ||
let words = text.split(' '); | ||
let final = ''; | ||
words.forEach(function (item) { | ||
final += '(?=.*' + escapeRegExp(item) + ')'; | ||
}); | ||
return final; | ||
} | ||
|
||
function formatBytes(bytes,decimals) { | ||
if(bytes === 0) return '0 Bytes'; | ||
let k = 1024, | ||
dm = decimals || 2, | ||
sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], | ||
i = Math.floor(Math.log(bytes) / Math.log(k)); | ||
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; | ||
} | ||
|
||
startSearch(query); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters