forked from dessalines/torrent-tracker-health
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
102 lines (87 loc) · 2.62 KB
/
utils.js
File metadata and controls
102 lines (87 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
var fs = require('fs'),
path = require('path');
var _debug = false;
function debug() {
if (!_debug) return;
console.debug.apply(console, arguments);
}
const defaultTrackers = [
"udp://tracker.coppersurfer.tk:6969/announce",
"udp://tracker.leechers-paradise.org:6969/announce",
"udp://tracker.openbittorrent.com:80/announce",
"udp://9.rarbg.to:2710/announce",
"udp://9.rarbg.me:2710/announce",
"udp://tracker.opentrackr.org:1337/announce",
"udp://tracker.internetwarriors.net:1337/announce",
"udp://exodus.desync.com:6969/announce",
"udp://tracker.tiny-vps.com:6969/announce",
"udp://retracker.lanta-net.ru:2710/announce",
"udp://open.demonii.si:1337/announce",
"udp://torrentclub.tech:6969/announce",
"udp://denis.stalker.upeer.me:6969/announce",
"udp://tracker3.itzmx.com:6961/announce",
"udp://tracker.torrent.eu.org:451/announce",
"udp://open.stealth.si:80/announce",
"udp://tracker.cyberia.is:6969/announce",
"udp://tracker.moeking.me:6969/announce",
"udp://explodie.org:6969/announce",
"udp://ipv4.tracker.harry.lu:80/announce",
];
function rearrange(options) {
if (!options) options = {};
_debug = (!options.debug) ? false : true;
if (!options.trackers) {
options.trackers = defaultTrackers;
}
// Make sure options.trackers is an array
if (!Array.isArray(options.trackers)) {
options.trackers = options.trackers ? [options.trackers] : [];
}
if (!options.batchSize) {
options.batchSize = 50;
}
if (!options.showAllFetches) {
options.showAllFetches = false;
}
return options;
}
function isDir(path) {
try {
var stat = fs.lstatSync(path);
return stat.isDirectory();
} catch (e) {
return false;
}
}
function isFile(path) {
try {
var stat = fs.lstatSync(path);
return stat.isFile();
} catch (e) {
return false;
}
}
function collectUris(uri) {
if (isDir(uri)) {
return torrentFilesInDir(uri);
}
// If its a file that doesn't end in .torrent
else if (isFile(uri) && uri.split('.').pop() !== 'torrent') {
return fs.readFileSync(uri).toString().trim().split('\n').map(f => 'magnet:?xt=urn:btih:' + f);
}
// If its a magnet link or single torrent file
else {
return [uri];
}
}
function torrentFilesInDir(dir) {
return fs.readdirSync(dir).map(file => path.join(dir, file)).filter(f => f.endsWith('.torrent'));
}
function chunk(inputArray, perChunk) {
return inputArray.reduce((all, one, i) => {
const ch = Math.floor(i / perChunk);
all[ch] = [].concat((all[ch] || []), one);
return all
}, []);
}
module.exports = { rearrange, torrentFilesInDir, isDir, isFile, collectUris, chunk, debug, defaultTrackers };