-
Notifications
You must be signed in to change notification settings - Fork 1
/
kat.js
47 lines (41 loc) · 1.43 KB
/
kat.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
var request = require('request');
var uncompress = require('compress-buffer').uncompress;
var _ = require('underscore');
var url = 'http://kickass.to/json.php?q=';
var headers = {
"accept-charset" : "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
"accept-language" : "en-US,en;q=0.8",
"accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"accept-encoding" : "gzip,deflate",
}
var options = {
headers: headers,
encoding: null
}
exports.search = function(query, callback) {
var searchUrl = url + query;
options.url = searchUrl;
request(options, function(err, response, body) {
if (err) {
return callback(err);
} else if (response.statusCode != 200) {
return callback(new Error('status code not 200 - code is: ' + response.statusCode));
} else {
var encoding = response.headers['content-encoding'];
if (encoding && encoding.indexOf('gzip') >= 0) {
try {
body = uncompress(body);
} catch (compError) {
return callback(compError);
}
}
try {
var resp = JSON.parse(body);
var sorted = _.sortBy(resp.list, function(x) { return -x['votes']; });
return callback(null, sorted);
} catch (parseError) {
return callback(parseError);
}
}
});
}