This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
505 lines (419 loc) · 12 KB
/
index.ts
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
import axios from 'axios'
import * as cheerio from 'cheerio'
import { URL, parse, Url } from 'url'
interface IsearchResults {
filetype: string
href: string
title: string
size: string
seeders: number
leechers: number
magnet: string
hash: string
}
interface IsearchResponse {
searchResults: IsearchResults[]
search: string
pageSize: number
total: number
}
interface IshowEpisodes {
episodeNumber: string
episodeTitle: string
dataHref: string
}
interface IshowSeasons {
season: string
date: string
episodes: IshowEpisodes[]
}
interface IshowResponse {
title: string
from: string
to: string
summary: string
imdb: string
imdbId: string
seasons: IshowSeasons[]
}
interface Iresponse {
type: string
searchResponse?: IsearchResponse
showResponse?: IshowResponse
movieResponse?: ImovieResponse
torrentResponse?: Itorrent
}
interface Idata {
title: string
metaUrl: string
sound: string
language: string
quality: string
magnet: string
hash: string
size: string
seeders: number
leechers: number
}
interface ImovieResults {
title: string
torrentHref: string
sound: string
language: string
quality: string
size: string
seeders: number
leechers: number
}
interface ImovieResponse {
title: string
summary: string
imdb: string
imdbId: string
release: string
results: ImovieResults[]
}
interface Itorrent {
filetype: string
title: string
imdb?: string
imdbId: string
source?: string
sourceUrl?: string
magnet: string
hash: string
size: string
date: string
}
interface Iload {
$: CheerioStatic
url: string
}
interface Isort {
seeders: string
date: string
size: string
}
interface IsortType {
descending: string
ascending: string
}
interface IassignUrl {
(url: string): string
}
class Enums {
public SORT: Isort = {
seeders: 's=ns',
date: 's=dt',
size: 's=sz'
}
public SORT_TYPE: IsortType = {
descending: 'sd=d',
ascending: 'sd=a'
}
}
class Common {
public static async load (url: string): Promise<Iload> {
// console.log(url)
const result = await axios.get(url)
return {
$: cheerio.load(result.data),
url: result.request.path
}
}
public static magnetToHash (magnet: string) {
return magnet.match(/:([\w\d]{40})/)[1]
}
public static assignUrl (endpoint: string, source: string) {
const url = new URL(endpoint)
const href = parse(source)
url.pathname = href.pathname
url.search = href.search
return url.href
}
public static parseSeeders (seedersStr: string) {
if (seedersStr) {
const seedersMatch = seedersStr.match(/\d+/g)
if (seedersMatch) {
return seedersMatch.map(x => Number(x))
}
}
return [0, 0]
}
public static iconToType (icon: Cheerio): string {
switch (true) {
case icon.hasClass('zqf-movies'):
return 'movie'
case icon.hasClass('zqf-tv'):
return 'show'
case icon.hasClass('zqf-anime'):
return 'anime'
case icon.hasClass('zqf-game'):
return 'game'
case icon.hasClass('zqf-app'):
return 'app'
case icon.hasClass('zqf-music'):
return 'music'
case icon.hasClass('zqf-book'):
return 'book'
case icon.hasClass('zqf-files'):
return 'other'
default:
return 'unknown'
}
}
}
class Parser {
public static parseSearch ($: CheerioStatic) {
let [, search, pageSize, total]: string[] | number[] = $('.panel.zq-panel.zq-small .panel-heading')
.text().trim().match(/"(.+)"\n{2}.+-\n(\d+)\nof (\d+[,]?\d+)/)
pageSize = parseInt(pageSize, 10)
total = parseInt(total.replace(',', ''), 10)
const htmlResults = $('td.text-trunc.text-nowrap a')
const searchResults: IsearchResults[] = []
htmlResults.each((i) => {
const e = htmlResults.eq(i)
const progress = e.parent().parent().find('.progress')
const magnet = e.parent().parent().find('.spr.dl-magnet')
.first().parent().attr('href')
const size = progress.eq(0).text()
const seedersStr = progress.eq(1).attr('title')
const [seeders, leechers] = Common.parseSeeders(seedersStr)
const iconElement = $('.zqf.text-muted2.zqf-small.pad-r2').eq(i)
const filetype = Common.iconToType(iconElement)
searchResults.push({
filetype,
href: e.attr('href'),
title: e.text(),
size,
seeders,
leechers,
magnet,
hash: Common.magnetToHash(magnet)
})
})
const searchResponse: IsearchResponse = {
searchResults,
search,
pageSize,
total
}
const response: Iresponse = {
type: 'search',
searchResponse
}
return response
}
public static parseShow ($: CheerioStatic, loadData?: boolean) {
const title = $('td.h4.sh1').text()
const [from, to] = $('.sh2 i').text().split('→').map(x => x.trim())
const summary = $('td.small.text-muted.sh2').text()
const imdb = $('.imdb_stars').attr('href')
const [imdbId] = imdb.match(/\btt\d{7}\b/) || [null]
let seasons: IshowSeasons[] = []
const seasonsElement = $('.panel.panel-default.eplist')
seasonsElement.each(i => {
const seasonTitleElement = seasonsElement.eq(i).find('.panel-heading.text-nowrap').children()
const season = seasonTitleElement.eq(0).text()
const date = seasonTitleElement.eq(1).text()
const episodes: IshowEpisodes[] = []
const episodesListElement = seasonsElement.eq(i).find('ul.list-group.eplist').find('.list-group-item')
episodesListElement.each(ei => {
const episodeNumber = episodesListElement.eq(ei).find('span.smaller.text-muted.epnum').text()
const episodeTitle = episodesListElement.eq(ei).find('a.pad-r2').text()
const dataHref = episodesListElement.eq(ei).find('.collapse.epdiv').attr('data-href')
episodes.push({
episodeNumber,
episodeTitle,
dataHref
})
})
seasons.push({
season,
date,
episodes
})
})
const showResponse: IshowResponse = {
title,
from,
to,
summary,
imdb,
imdbId,
seasons
}
const response: Iresponse = {
type: 'show',
showResponse
}
return response
}
public static parseMovie ($: CheerioStatic) {
const moviesElement = $('td.text-nowrap.text-trunc')
const title = $('h4.margin-top-10').text().trim()
const summary = $('h4.margin-top-10').parent().find('p.small.text-muted').text().trim()
const release = $('h4.margin-top-10').parent().find('h5.small.text-muted').text().trim().replace('Released • ', '')
const imdb = $('.imdb_stars').attr('href')
const [imdbId] = imdb.match(/\btt\d{7}\b/) || [null]
let results: ImovieResults[] = []
moviesElement.each(i => {
const title = moviesElement.eq(i).find('a').text()
const torrentHref = moviesElement.eq(i).find('a').attr('href')
const sound = moviesElement.eq(i).find('span').eq(0).text()
const language = moviesElement.eq(i).find('span').eq(1).text()
const [quality] = title.match(/\d{3,4}p/) || ['Str']
const size = moviesElement.eq(i).parent().find('.progress-bar.prog-blue').text()
const seedersStr = moviesElement.eq(i).parent()
.find('.progress-bar.prog-green').parent().attr('title')
const [seeders, leechers] = Common.parseSeeders(seedersStr)
results.push({
title,
torrentHref,
sound,
language,
quality,
size,
seeders,
leechers
})
})
const movieResponse: ImovieResponse = {
title,
summary,
imdb,
imdbId,
release,
results
}
const response: Iresponse = {
type: 'movie',
movieResponse
}
return response
}
public static parseData ($: CheerioStatic) {
const data: Idata[] = []
const titleLinks = $('td.text-nowrap.text-trunc a')
titleLinks.each(i => {
const title = titleLinks.eq(i).text()
const metaUrl = titleLinks.eq(i).attr('href')
let sound = titleLinks.eq(i).parent().find('div.text-nowrap span').eq(0).text()
let language = titleLinks.eq(i).parent().find('div.text-nowrap span').eq(1).text().trim()
let quality = titleLinks.eq(i).parent().find('div.text-nowrap span').eq(2).text().trim()
if (sound === '') sound = 'Str'
if (language === '') language = 'Str'
if (quality === '') [quality] = title.match(/\d{3,4}p/) || ['Str']
const magnet = $('.spr.dl-magnet').eq(i).parent().attr('href')
const hash = Common.magnetToHash(magnet)
const size = $('.progress-bar.prog-blue.prog-l').eq(i).text()
const seedersStr = $('.progress-bar.smaller.prog-l').eq(i).parent().attr('title')
const [seeders, leechers] = Common.parseSeeders(seedersStr)
data.push({
title,
metaUrl,
sound,
language,
quality,
magnet,
hash,
size,
seeders,
leechers
})
})
return data
}
public static parseTorrent ($: CheerioStatic) {
const title = $('#torname').text().replace(/ /g, '.')
const sourceElement = $(':contains("– Indexed from –")').last().next()
const iconElement = $('.tor-icon')
const filetype = Common.iconToType(iconElement)
const imdb = $('.imdb_stars').attr('href')
const [imdbId] = imdb.match(/\btt\d{7}\b/) || [null]
let source = sourceElement.text().trim()
if (source === '') { source = null }
const sourceUrl = sourceElement.attr('href') || null
const magnet = $('.dl-magnet').parent().attr('href')
const hash = Common.magnetToHash(magnet)
const [size, date] = $('.zqf-files').last().parent()
.contents().toArray().filter((x: any) => x.type === 'text')
.map(x => x.data.trim())
const torrent: Itorrent = {
filetype,
title,
imdb,
imdbId,
source,
sourceUrl,
magnet,
hash,
size,
date
}
return torrent
}
}
export class Zooqle {
constructor () {
this._assignUrl = Common.assignUrl.bind(null, this.endPoint)
}
private _endpoint = new URL('https://zooqle.com')
private _assignUrl: IassignUrl
public enums = new Enums()
public get endPoint () {
return this._endpoint.href
}
public set endPoint (url: string) {
this._endpoint.host = url
}
public async search (query: string, parameters: string[] = []) {
return new Promise<Iresponse>((resolve, reject) => {
const url = this._endpoint
url.pathname = '/search'
url.searchParams.append('q', query)
parameters.forEach(param => {
const [key, val] = param.split('=')
url.searchParams.append(key, val)
})
Common.load(url.href)
.then(res => {
switch (true) {
case /\/tv\//.test(res.url):
return resolve(Parser.parseShow(res.$)) // handle tv
case /\/movie\//.test(res.url):
return resolve(Parser.parseMovie(res.$)) // handle movie
case /\/search/.test(res.url):
return resolve(Parser.parseSearch(res.$)) // handle search
default:
const torrentResponse = Parser.parseTorrent(res.$) // handle direct torrent
const response: Iresponse = {
type: 'torrent',
torrentResponse
}
return resolve(response)
}
})
.catch(reject)
})
}
public async getData (dataHref: string) {
return new Promise<Idata[]>((resolve, reject) => {
const url = this._assignUrl(dataHref)
Common.load(url).then(res => {
resolve(Parser.parseData(res.$))
})
.catch(console.error)
})
}
public async getTorrentData (torrentHref: string) {
return new Promise<Itorrent>((resolve, reject) => {
const url = this._assignUrl(torrentHref)
Common.load(url).then(res => {
resolve(Parser.parseTorrent(res.$))
})
.catch(console.error)
})
}
}
export const zooqle = new Zooqle()