-
Notifications
You must be signed in to change notification settings - Fork 0
/
tpb.js
89 lines (80 loc) · 2.22 KB
/
tpb.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
var util = require("util");
var http = require("http");
module.exports = new (require('events').EventEmitter);
module.exports.on('new', function (torrent) {
util.log("[TPB] New " + torrent.id + ": " + torrent.size +
" | " + torrent.name);
});
module.exports.on('debug', function (msg) {
util.log("[TPB] Debug: " + msg);
});
var requestOptions = {
host: "thepiratebay.org",
port: 80,
path: "/recent",
method: 'GET'
};
var torrentRegex = /href="\/torrent\/([^\/]+).*?">(.*?)<\/a>.*?Size (.*?),/g;
var lastId = 0;
function debugEvents (emitter, events) {
for (var i in events) {
var eventname = events[i];
emitter.on(eventname, function () {
util.debug("event: " + eventname);
});
}
};
function poll () {
var req = http.request(requestOptions, function (res) {
res.setEncoding("utf8");
if (res.statusCode != 200) {
module.exports.emit('debug', "non-200 response: " + res.statusCode);
res.destroy();
setTimeout(poll, 60000);
return;
}
var body = "";
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
body = body.replace(/[\n\r\u2028\u2029]+/g, "");
var match;
var newLastId = 0;
while (match = torrentRegex.exec(body)) {
var torrentId = parseInt(match[1]);
//module.exports.emit('debug', "torrentId: " + torrentId);
if (torrentId <= lastId)
continue;
if (torrentId > newLastId)
newLastId = torrentId;
if (lastId)
module.exports.emit('new', {
id: torrentId,
name: match[2],
size: match[3].replace(" ", " ")
});
}
if (newLastId)
lastId = newLastId;
//module.exports.emit('debug', "DBG lastId: " + lastId);
setTimeout(poll, 30000);
});
});
req.once('error', function (e) {
setTimeout(poll, 500);
});
req.on('error', function (e) {
module.exports.emit('debug', "Error: " + e.message);
});
req.end();
setTimeout(function () {
try {
req.destroy();
req.emit('error', {message: "Timeout"});
module.exports.emit('debug', "Timeout");
} catch (e) {}
}, 5000);
};
poll();
// vim:set ts=2 sw=2 smartindent et: