This repository has been archived by the owner on May 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
122 lines (110 loc) · 4.11 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
import request from 'request';
import cheerio from 'cheerio';
let jar = request.jar();
request.defaults({ followAllRedirects: true });
class Ygg {
constructor(config) {
this.host = config.host;
this.searchhost = config.searchhost;
this.username = config.username;
this.password = config.password;
}
login(callback) {
request(
{
method: 'POST',
url: `${this.host}/user/login`,
headers: {
'user-agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36',
'x-requested-with': 'XMLHttpRequest',
},
formData: {
id: this.username,
pass: this.password,
},
jar,
},
(err, response, body) => {
if (err) return callback(err);
if (response.statusCode / 100 >= 4) {
let error = new Error(`Bad status code while login : ${response.statusCode}. Bad username/password ?`);
error.body = body;
return callback(error);
}
callback(err, body);
}
);
}
getRatio(callback) {
request(
{
method: 'GET',
url: `${this.host}/user/ajax_usermenu`,
headers: {
'user-agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36',
'x-requested-with': 'XMLHttpRequest',
},
jar,
json: true,
},
(err, response, body) => {
if (err) return callback(err);
if (response.statusCode / 100 >= 4) {
let error = new Error(`Bad status code getting ratio : ${response.statusCode}`);
error.body = body;
return callback(error);
}
body = body.html;
// console.log(body);
let $ = cheerio.load(body);
let ratio = body.match(/Ratio : ([0-9\.]+)/);
let results = {
upload: $('.ico_upload').parent().text(),
download: $('.ico_download').parent().text(),
ratio: ratio[1],
};
callback(err, results);
}
);
}
search(name, callback) {
request(
{
method: 'GET',
url: `${this.searchhost}/engine/search`,
qs: {
name,
do: 'search',
},
headers: {
'user-agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36',
},
jar,
},
(err, response, body) => {
if (err) return callback(err);
if (response.statusCode / 100 >= 4) {
let error = new Error(`Bad status code while searching : ${response.statusCode}`);
error.body = body;
return callback(error);
}
let $ = cheerio.load(body);
let results = [];
$('.table-responsive.results tbody tr').each((i, tr) => {
results.push({
// line: $(tr).text(),
url: $(tr).find('#torrent_name').attr('href'),
name: $(tr).find('#torrent_name').text().trim(),
size: $($(tr).find('td')[5]).text(),
downloadurl: `${this.searchhost}/engine/download_torrent?id=${$(tr).find('#get_nfo').attr('target')}`,
});
});
callback(err, results);
}
);
}
}
export default Ygg;