-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
415 lines (396 loc) · 10.6 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
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
const http = require('http');
const path = require('path');
const phantom = require('phantom');
const sharp = require('sharp');
const torrentNameParser = require("torrent-name-parser");
const ptn = require('./name-parser/index');
const API_HOST = 'www.allocine.fr';
const parsing = require('./parsing');
const uri_action = {
search_series: {
method: 'GET',
path: '/recherche/6/?q=',
parsing: parsing.series.series_list.get,
query_params: true
},
serie_information: {
method: 'GET',
path: '/series/ficheserie-',
parsing: parsing.series.serie_information.get,
query_params: true
},
serie_information_saison: {
method: 'GET',
path: '',
parsing: parsing.series.serie_information_saison.get,
query_params: true
},
serie_information_episode: {
method: 'GET',
path: '',
parsing: parsing.series.serie_information_episode.get,
query_params: true
},
serie_information_episode_casting: {
method: 'GET',
path: '',
parsing: parsing.series.serie_information_episode_casting.get,
query_params: true
},
search_movies: {
method: 'GET',
path: '/recherche/1/?q=',
parsing: parsing.movies.movies_list.get,
query_params: true
},
movies_information: {
method: 'GET',
path: '',
parsing: parsing.movies.movies_information.get,
query_params: true
}
};
function filter_titles(title)
{
var title_filter = '';
var cut_char = '[]{}();';
for (var i = 0; i < title.length; i++)
{
for (var j = 0; j < cut_char.length; j++)
{
if (title[i] == cut_char[j])
return title_filter.trim();
}
title_filter += title[i];
};
var match_comma_str = title_filter.match(new RegExp(/[^0-9][,][^0-9]/g));
if (match_comma_str)
{
match_comma_str.forEach((e) => {
title_filter = title_filter.replace(e, e.replace(',', ' '));
});
}
var match_space_number = title_filter.match(new RegExp(/[0-9][ ][0-9]/g));
if (match_space_number)
{
match_space_number.forEach((e) => {
title_filter = title_filter.replace(e, e.replace(' ', ''));
});
}
return title_filter.trim();
}
function remove_wrong_word(title)
{
var wrong_word = [
'Unrated',
'FRENCH',
'vff',
'Duologie',
'Trilogie',
'Quadrilogie',
'Integrale',
'Intégrale'
];
wrong_word.forEach((e) => {
title = title.replace(e, '');
});
return title.trim();
}
function parse_query_post(query, type)
{
query_obj = torrentNameParser(path.basename(query));
query_obj.title = remove_wrong_word(filter_titles(query_obj.title).replace(/\./g, ' '));
if (type === 'serie' && query_obj.season && query_obj.episode)
return query_obj.title + " S" + ("0" + parseInt(query_obj.season)).slice(-2) + "E" + ("0" + parseInt(query_obj.episode)).slice(-2);
else if (type === 'movie' && query_obj.year)
return query_obj.title + " " + query_obj.year;
else
return query_obj.title;
}
function parse_query_string(query)
{
let name = "";
let file = ptn(remove_wrong_word(query));
name += `${file.title}.`;
if (file.season)
name += `S${("0" + file.season).slice(-2)}.`;
else
name += `S01.`;
if (file.episode)
name += `E${("0" + file.episode).slice(-2)}.`;
else
name += `E01.`;
if (file.year)
name += `${file.year}.`;
return name.slice(0, -1);
}
function Client() {
}
Client.prototype.get = function(action, query, callback)
{
if(uri_action[action])
fetch_url(action, query, uri_action[action].parsing, null, callback);
else
{
console.log(`Wrong action, action: ${action}, query: ${query}`);
if (callback)
callback(null);
else
console.log('No callback');
}
};
Client.prototype.get_series_list = async function(query)
{
return await new Promise((resolve, reject) => {
query = encodeURIComponent(parse_query_post(query));
this.get('search_series', query, function(res) {
if (res.length > 0)
{
res.forEach(e => {
delete e.from;
e.with = e.with.replace(/\n/g, '');
});
resolve(res);
}
else
reject('No result found');
});
});
};
Client.prototype.get_series_sheets_by_name = async function(query)
{
query = query.replace(/saison /gi, 'S').replace(/ /g, '.');
return await new Promise((resolve, reject) => {
var query_filter = encodeURIComponent(parse_query_post(query));
query = parse_query_string(query);
this.get('search_series', query_filter, (result) => {
parsing.series.select_result.get(result, query, (req, result, error) => {
var result_tmp = result;
if (error)
reject(error);
else
{
this.get('serie_information', result.id + '/saisons/', {
callback: ((result, req) => {
if (!result)
reject('No result found');
else
this.get('serie_information_saison', result, {
callback: ((result, req) => {
if (!result)
reject('No result found');
else {
result.name = result_tmp.name.replace(/\n/g, '');
result.img = result_tmp.url_img;
result.result_weigth = result_tmp.result_weigth;
get_base64img_form_url(result, resolve);
/*
this.get('serie_information_episode', 'http://' + API_HOST + result, {
callback: ((result, req) => {
this.get('serie_information_episode_casting', 'http://' + API_HOST + result, {
callback: ((result, req) => {
result.name = result_tmp.name.replace(/\n/g, '');
result.img = result_tmp.url_img;
result.result_weigth = result_tmp.result_weigth;
get_base64img_form_url(result, resolve);
}),
search_req: req
});
}),
search_req: req
});
*/
}
}),
search_req: req
});
}),
search_req: req
});
}
});
});
});
};
Client.prototype.get_movies_list = async function(query)
{
return await new Promise((resolve, reject) => {
query = encodeURIComponent(parse_query_post(query));
this.get('search_movies', query, function(res) {
resolve(res);
});
});
};
Client.prototype.get_movies_sheets_by_name = async function(query)
{
return await new Promise((resolve, reject) => {
var query_filter = encodeURIComponent(parse_query_post(query));
query = parse_query_post(query, 'movie');
this.get('search_movies', query_filter, (result_movie) => {
parsing.movies.select_result.get(result_movie, query, (req, result_movie, error) => {
var result_tmp = result_movie;
if (error)
reject(error);
else
{
this.get('movies_information', result_movie.url, {
callback: ((result_movie, req) => {
result_movie.titles = result_tmp.name.replace(/\n/g, '');
result_movie.from = result_tmp.from;
result_movie.result_weigth = result_tmp.result_weigth;
get_base64img_form_url(result_movie, resolve);
}),
search_req: req
});
}
});
});
});
};
function limit_output_img_size(img_base64, data_res, callback)
{
sharp(new Buffer(img_base64, 'base64'))
.resize(240, 320)
.toBuffer()
.then( data => {
data_res.img = data.toString('base64');
callback(data_res);
})
.catch( err => {
data_res.img = img_base64;
callback(data_res);
});
}
function update_url_image_size(result)
{
var regex = new RegExp(/\/[rc]_[0-9]+_[0-9]+\//g);
var match_path = result.match(regex);
if (match_path && match_path[0])
{
var split_result = match_path[0].replace(/\//g, '').split('_');
var new_path_size = '';
for (var i = 0; i < split_result.length; i++)
{
if (split_result[i] == parseInt(split_result[i]))
{
if (i == 1)
new_path_size += "_480";
else if (i == 2)
new_path_size += "_640";
}
else
new_path_size += split_result[i];
}
new_path_size = "/" + new_path_size + "/";
result = result.replace(match_path[0], new_path_size);
}
return result;
}
function get_base64img_form_url(data, callback)
{
data.img = update_url_image_size(data.img);
var splited_url = data.img.split('/');
var path = '/';
for (var i = 3; i < splited_url.length; i++)
{
path += splited_url[i] + "/";
}
path = path.substr(0, path.length - 1);
var host_name_url = data.img.replace('http://', '').replace('https://', '').replace(path, '');
var headers = {
'Host': host_name_url,
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': 0
};
var options_url = {
hostname: host_name_url,
port: 80,
path: path,
method: 'GET',
headers: headers
};
var data_res = '';
var req = http.request(options_url, (res) => {
res.setEncoding('base64');
res.on('data', (chunk) => {
data_res += chunk;
});
res.on('end', (d) => {
data.img = limit_output_img_size(data_res, data, callback);
});
});
req.on('error', (e) => {
console.error(e);
});
req.end();
}
function fetch_url_phantom(action, query, parsing, auth, callback)
{
(async function(action, query, parsing, auth, callback) {
const instance = await phantom.create();
const page = await instance.createPage();
await page.on('onResourceRequested', function(requestData) {
//console.info('Requesting', requestData.url);
});
page.property('customHeaders', {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:66.0) Gecko/20100101 Firefox/66.0"
});
const status = await page.open(`https://${API_HOST}/${query}`);
const content = await page.property('content');
parsing(query, Buffer.from(content, 'utf-8'), callback);
await instance.exit();
})(action, query, parsing, auth, callback);
}
function phantom_url_is_need(query)
{
var match = query.match(/ficheserie-|saison-|ep-/g);
if (match)
{
var match_string = '';
match.forEach((e) =>
{
match_string += e;
});
if (match_string === 'ficheserie-saison-')
return true;
else if (match_string === 'ficheserie-saison-ep-')
return true;
}
return false;
}
function fetch_url(action, query, parsing, auth, callback)
{
//console.log(action, `${uri_action[action].path + (uri_action[action].query_params ? query : '')}`, phantom_url_is_need(query));
if (phantom_url_is_need(query))
fetch_url_phantom(action, query, parsing, auth, callback);
else
{
var headers = {
hostname: API_HOST,
port: 80,
path: uri_action[action].path + (uri_action[action].query_params ? query : ''),
method: uri_action[action].method,
headers: {
'Host': API_HOST,
'Content-Length': Buffer.byteLength(uri_action[action].query_params ? '' : query, 'utf8'),
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
};
var data_res = [];
var req = http.request(headers, (res) => {
res.on('data', (d) => {
data_res.push(d);
});
res.on('end', (d) => {
var buffer = Buffer.concat(data_res);
parsing(query, buffer, callback);
});
});
req.on('error', (e) => {
console.error(e);
});
req.write(uri_action[action].query_params ? '' : query);
req.end();
}
}
module.exports = Client;