-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
153 lines (131 loc) · 4.43 KB
/
index.js
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'use strict'
var readTorrent = require('read-torrent'),
Tracker = require('torrent-tracker'),
util = require('util');
var _debug = false;
function debug () {
if (!_debug) return;
console.log.apply(console, arguments);
}
function rearrange (uri, options, debug) {
if (!options) options = {};
// debug
_debug = (debug === true || options.debug) ? true : false;
// Make sure options.uri is there
if (util.isObject(uri)) {
options = uri;
} else {
options.uri = uri;
}
// Make sure options.blacklist is an array
if (!util.isArray(options.blacklist)) {
options.blacklist = options.blacklist ? [options.blacklist] : [];
}
// Make sure options.force is an array
if (!util.isArray(options.force)) {
options.force = options.force ? [options.force] : [];
}
// Set a timeout
if (!options.timeout && options.timeout !== false || options.timeout === true) {
options.timeout = 400;
}
if (!options.uri) {
throw 'No torrent URI specified';
}
return options;
}
function read (uri, options, debug) {
return new Promise(function (resolve, reject) {
options = rearrange(uri, options, debug);
readTorrent(options.uri, function (err, info) {
if (!err) {
var trackers = [];
// Make sure info.announce is an array
if (!util.isArray(info.announce)) {
info.announce = info.announce ? [info.announce] : [];
}
// Remove the "blacklisted" trackers from the list
for (var i = 0; i < info.announce.length; i++) {
if (!options.blacklist.some(function (regex) {
if (util.isString(regex)) {
regex = new RegExp(regex);
}
return regex.test(info.announce[i]);
})) {
trackers.push(info.announce[i]);
}
}
// Add the "forced" trackers to the list
for (var i = 0; i < options.force.length; i++) {
if (trackers.indexOf(options.force[i]) === -1) {
trackers.push(options.force[i]);
}
}
resolve({
hash: info.infoHash,
trackers: trackers,
timeout: options.timeout
});
} else {
debug('Error in read-torrent: ' + err.message);
reject(err);
}
});
});
}
function scrape (req) {
return Promise.all(req.trackers.map(function (trUri) {
return new Promise(function (resolve, reject) {
debug('Obtaining tracker for ' + trUri);
var tracker = new Tracker(trUri);
var begin = Date.now();
tracker.scrape([req.hash], {
timeout: req.timeout
}, function (err, data) {
if (err) {
if (err.message === 'timed out' || err.code === 'ETIMEDOUT') {
debug('Scrape timed out for ' + trUri);
} else {
debug('Error in torrent-tracker: ' + err.message);
}
resolve({
tracker: trUri,
error: err.message
});
} else {
debug('Scrape successful for ' + trUri);
resolve({
tracker: trUri,
response_time: Date.now() - begin,
seeds: data[req.hash].seeders,
peers: data[req.hash].leechers
});
}
});
});
}));
}
function calc (res) {
var totalSeeds = 0,
totalPeers = 0,
total = 0;
for (var i = 0; i < res.length; i++) {
if (!res[i].error) {
totalSeeds += res[i].seeds | 0;
totalPeers += res[i].peers | 0;
total++;
}
};
// Avoid divide-by-zero issues
if (total === 0) total = 1;
return {
seeds: Math.round(totalSeeds / total),
peers: Math.round(totalPeers / total),
results: res
};
}
module.exports = function (uri, options, debug) {
return read(uri, options, debug)
.then(scrape)
.then(calc);
};