-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
349 lines (324 loc) · 9.63 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Import the necessary modules.
const bytes = require('bytes')
const cheerio = require('cheerio')
const debug = require('debug')
const got = require('got')
const { stringify } = require('querystring')
const { name } = require('./package')
/**
* @typedef {Object} Response
* @property {Date} response_time The response_time of the response.
* @property {number} page The page of the response.
* @property {number} total_results The total_results of the response.
* @property {number} total_pages The total_pages of the response.
* @property {Array<Torrent>} results The results of the response.
*/
/**
* The model of the torrent object
* @typedef {Object} Torrent
* @property {string} title The title of the torrent.
* @property {string} category The category of the torrent.
* @property {string} link The link of the torrent.
* @property {boolean} verified The verified of the torrent.
* @property {number} comments The comments of the torrent.
* @property {string} torrentLink The torrentLink of the torrent.
* @property {string} fileSize The fileSize of the torrent.
* @property {number} size The size of the torrent.
* @property {number} seeds The seeds of the torrent.
* @property {number} leechs The leechs of the torrent.
* @property {number} peers The peers of the torrent.
*/
/**
* A KickassTorrents API wrapper.
* @type {KatApi}
*/
module.exports = class KatApi {
/**
* Create a new instance of the module.
* @param {!Object} config={} - The configuration object for the module.
* @param {!string} baseUrl=https://katcr.co/ - The base url of katcr.
* @type {String}
*/
constructor({baseUrl = 'https://katcr.co/'} = {}) {
/**
* The base url of katcr.
* @type {string}
*/
this._baseUrl = baseUrl
/**
* Show extra output.
* @type {Function}
*/
this._debug = debug(name)
/**
* The available categories to search for.
* @type {Object}
*/
this._category = {
anime_english_translated: 118,
anime_other: 133,
applications_handheld: 144,
applications_windows: 139,
applications_mac: 140,
applications_linux: 142,
applications_other: 131,
books_children: 102,
books_comics: 103,
books_manga: 104,
books_magazines: 105,
books_textbooks: 106,
books_fiction: 107,
books_non_fiction: 108,
books_audio_books: 109,
books_biography: 110,
books_religion: 111,
books_history: 112,
books_computers_technology: 113,
books_educational: 114,
books_cooking: 115,
books_sport: 116,
books_other: 132,
games_windows: 85,
games_linux: 87,
games_xbox: 90,
games_wii: 91,
games_handheld: 92,
games_playstation: 97,
games_other: 130,
movies_3d_movies: 69,
movies_hd: 71,
movies_screener: 74,
movies_ultrahd: 75,
movies_dubbed_movies: 78,
movies_asian_bollywood: 79,
movies_animation: 80,
movies_documentary: 81,
movies_other: 128,
movies_blu_ray_iso: 148,
movies_cam: 149,
movies_dvd_iso: 150,
music_mp3: 22,
music_lossless: 23,
music_radio_shows: 26,
music_aac: 64,
music_transcode: 65,
music_soundtrack: 66,
music_karaoke: 67,
music_videos_concerts: 68,
music_other: 129,
other_subtitles: 134,
other_pictures: 136,
other_other: 138,
other_tutorials: 145,
other_wordpress: 153,
other_dazposer: 154,
tv_dvd_iso: 5,
tv_blu_ray_iso: 6,
tv_hd: 41,
tv_documentary: 7,
tv_sport: 146,
tv_other: 151,
tv_ultra_hd: 152,
xxx_videos: 119,
xxx_hd: 155,
xxx_ultrahd: 121,
xxx_pictures: 122,
xxx_magazines: 123,
xxx_books: 124,
xxx_hentai: 125,
xxx_xxx_games: 126
}
/**
* The available languages to search for.
* @type {Object}
*/
this._lang = {
all: 0,
english: 1,
bengali: 10,
chinese: 11,
dutch: 12,
french: 2,
german: 3,
greek: 13,
hindi: 9,
italian: 4,
japanese: 14,
korean: 15,
russian: 7,
spanish: 6,
tamil: 16,
telegu: 17,
turkish: 18,
unknown: 8
}
/**
* The available ways to sort the results by.
* @type {Object}
*/
this._sort = {
id: 'id',
name: 'name',
comments: 'comments',
size: 'size',
completed: 'times_completed',
seeders: 'seeders',
leechers: 'leechers'
}
/**
* The available ways to order the results by.
* @type {Object}
*/
this._order = {
asc: 'asc',
desc: 'desc'
}
}
/**
* Make a get request to kat.co.
* @param {!string} endpoint - The endpoint to make the request to.
* @param {?Object} query - The query parameters of the HTTP request.
* @returns {Promise<Function, Error>} - The response body wrapped in
* cheerio.
*/
_get(endpoint, query) {
const uri = `${this._baseUrl}${endpoint}`
const opts = {
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'
},
query
}
this._debug(`Making request to: '${uri}?${stringify(query)}'`)
return got.get(uri, opts)
.then(({ body }) => cheerio.load(body))
// .then(({ body }) => body)
}
/**
* Format the result page in the response object.
* @param {!Object} $ - The cheerio loaded body.
* @param {!number} page - The page of the torrent to find.
* @param {!Date} date - The date the query was started.
* @returns {Response} - The response of a query.
*/
_formatPage($, page, date) {
const data = $('a.button.button--gray').last().text()
const hasNext = data.toLowerCase() === 'next'
const result = {
responseTime: date,
page,
hasNext
}
const self = this
result.results = $('table.torrents_table')
.children('tbody')
.find('tr')
.map(function () {
const entry = $(this)
const title = entry.find('a.torrents_table__torrent_title > b').text()
const link = entry.find('a.torrents_table__torrent_title').attr('href')
const category = entry.find('span.torrents_table__upload_info')
.find('a > strong')
.last()
.text()
const verified = entry.find('i.kf__crown').length > 0
const comments = parseInt(
entry.find('a.button.button--small[title="Comments"]').text(), 10
)
const torrentLink = entry
.find('a.button.button--small[title="Torrent magnet link"]')
.attr('href')
const fileSize = entry.find('td.text--nowrap.text--center')
.eq(0)
.text()
const size = bytes(fileSize)
const seeds = parseInt(
entry.find('td.text--nowrap.text--center.text--success').eq(0).text(),
10
)
const leechs = parseInt(
entry.find('td.text--nowrap.text--center.text--error').eq(0).text(),
10
)
const peers = seeds + leechs
return {
title,
category,
link: `${self._baseUrl}/${link}`,
verified,
comments,
torrentLink,
fileSize,
size,
seeds,
leechs,
peers
}
}).get()
return result
}
/**
* Make an advanced search.
* @param {!Object} config - The config of the advanced query object.
* @param {!string} [config.category] - The category of the torrents to find.
* @param {!string} config.query - The keywords to search for.
* @param {?number} [config.page=1] - The page of the torrent to find.
* @param {?number} [config.language] - The language of the torrents to find.
* @param {?string} [config.sortBy='id'] - The way to sort the results by.
* @param {?string} [config.orderBy='desc'] - The way to order the results by.
* @param {!Date} date - The date the query was started.
* @returns {Promise<Resonse, Error>} - The response of an advanced search.
*/
_getData({
category,
query = '',
page = 1,
language,
sortBy = 'id',
orderBy = 'desc'
}, date) {
let err
if (category && !this._category[category]) {
err = new Error(`'${category}' is not a valid value for category`)
return Promise.reject(err)
}
if (language && !this._lang[language]) {
err = new Error(`'${language}' is not a valid value for lang`)
}
if (sortBy && !this._sort[sortBy]) {
err = new Error(`'${sortBy}' is not a valid value for sort`)
}
if (orderBy && !this._order[orderBy]) {
err = new Error(`'${orderBy}' is not a valid value for order`)
}
if (err) {
return Promise.reject(err)
}
const cat = this._category[category]
const lang = this._lang[language]
const sort = this._sort[sortBy]
const order = this._order[orderBy]
return this._get(`katsearch/page/${page}/${query}`, {
cat,
lang,
sort,
order
}).then(res => this._formatPage(res, page, Date.now() - date))
}
/**
* Search for content on katcr.co.
* @param {Object|string} query - Object for advanced search, string for
* simple search.
* @returns {Promise<Response, Error>} - The response object of the query.
*/
search(query) {
this.lastRequestTime = Date.now()
if (typeof (query) === 'string') {
return this._getData({ query }, this.lastRequestTime)
} else if (typeof (query) === 'object') {
return this._getData(query, this.lastRequestTime)
}
const err = new Error('search needs an object or string as a parameter!')
return Promise.reject(err)
}
}