This repository has been archived by the owner on Mar 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtntvillage-api.js
150 lines (120 loc) · 4.04 KB
/
tntvillage-api.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
'use strict';
const got = require('got');
const cheerio = require('cheerio');
const formdata = require('form-data');
module.exports = class TNTVillageAPI {
constructor(debug = false) {
this._baseUrl = 'http://tntvillage.scambioetico.org/src/releaselist.php';
this._timeout = 5 * 1000;
this._debug = debug;
this._category = {
all_categories: 0,
movies: 4,
music: 2,
tv_movies_and_programs: 1,
documentaries: 14,
students_releases: 13,
e_books: 3,
linux: 6,
anime: 7,
cartoons: 8,
macintosh: 9,
windows_software: 10,
pc_game: 11,
playstation: 12,
music_video: 21,
sport: 22,
theater: 23,
wrestling: 24,
various: 25,
xbox: 26,
wallpaper_images: 27,
other_games: 28,
tv_series: 29,
comics: 30,
trash: 31,
nintendo: 32,
a_book: 34,
podcast: 35,
newsstand: 36,
mobile: 37
};
}
_get(query = {}) {
if (this._debug) console.info(`DEBUG: Making request to: '${this._baseUrl}', query: ${JSON.stringify(query)}`);
const form = new formdata();
for (var property in query) {
if (query.hasOwnProperty(property)) {
form.append(property, query[property]);
}
}
return got(this._baseUrl, {
method: 'POST',
body: form,
timeout: this._timeout
}).then(({ body }) => body);
}
_formatPage(res, date) {
if (this._debug) console.info(`DEBUG: Parsing result page`);
let $ = cheerio.load(res);
let results = $('#form_go_to_page > span > b:nth-child(1)').text();
let page = $('#form_go_to_page > span > b:nth-child(2)').text();
let pages = $('#form_go_to_page > span > b:nth-child(3)').text();
const result = {
response_time: parseInt(date, 10),
page: parseInt(page, 10),
total_results: parseInt(results, 10),
total_pages: parseInt(pages, 10),
results: []
};
$('.showrelease_tb > table tr:not(:first-child)').each((index, row) => {
let title = $('td:nth-child(7) > a', row).text().replace(/\s\(\d{4}\)/g, '');
let yearMatches = $('td:nth-child(7) > a', row).text().match(/\((\d{4})\)/);
let year = (yearMatches && yearMatches.length > 0 ? yearMatches[1] : -1);
let quality = '';
let qualityString = $('td:nth-child(7)', row).clone().children().remove().end().text();
if (qualityString.match(/480p/i)) quality = '480p';
if (qualityString.match(/720p/i)) quality = '720p';
if (qualityString.match(/1080p/i)) quality = '1080p';
let link = $('td:nth-child(7) > a', row).attr('href');
let torrent_link = $('td:nth-child(2) > a', row).attr('href');
let seeders = $('td:nth-child(5)', row).text();
let leechers = $('td:nth-child(4)', row).text();
result.results.push({
title: title,
year: parseInt(year, 10),
quality: quality,
link: link,
torrent_link: decodeURIComponent(torrent_link),
seeders: parseInt(seeders, 10),
leechers: parseInt(leechers, 10),
});
});
if (this._debug) console.info(`DEBUG: Returning ${result.results.length} results`);
return result;
}
_getData({ category, search, page = 1 }, date) {
if (category && !this._category[category]) {
throw new Error(`'${category}' is not a valid value for category`);
} else {
category = this._category[category];
}
return this._get({
cat: category,
srcrel: search,
page
}).then(res => this._formatPage(res, Date.now() - date));
}
search(params) {
const date = Date.now();
if (typeof (params) === 'string') {
return this._getData({
category: 'movies',
search: params
}, date);
} else if (typeof (params) === 'object') {
return this._getData(params, date);
}
throw new Error('search needs an object or string as a parameter!');
}
}