-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchFilm.js
More file actions
99 lines (86 loc) · 2.78 KB
/
Copy pathSearchFilm.js
File metadata and controls
99 lines (86 loc) · 2.78 KB
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
const axios = require("axios");
const cheerio = require("cheerio");
class SearchFilm {
/**
* @constructor
*/
constructor() {
this.data = [];
}
/**
* IMDB sitesine istek atar.
*
* @param data
* @param search {String}
* @returns {Promise<any>}
*/
async getRequest(search) {
// Kelimeyi query uygun çevirme.
search = new URLSearchParams(search).toString();
// İmdb sitesine istek atma.
const { data } = await axios.get(`https://www.imdb.com/find?q=${search}&s=tt&ref_=fn_al_tt_mr`);
// html şeklinde sonuçları dönme.
return data;
}
/**
* IMDB sitesine aramak istediğiniz kelimeyi gönderir.
*
* @param search {String}
* @returns {Array}
*/
async search(search) {
// Split etmek için cheerio kullanıyoruz.
const $ = cheerio.load(await this.getRequest(search));
// verilerimiz bu standartta olacak.
var dataObject = {
image: null,
title: null,
year: null,
season: null,
episode: null
};
// Dönen liste bir tablo içinde döngüye alıyoruz.
$(".findList > tbody > tr > td").each((index, element) => {
var text = $(element).text();
// Split işlemleri
var titleSpit = text.split("(");
var yearSpit = text.match(/\(\d*?\)/g);
var seasonAndEpisodeSpit = text.match( '-(.*)-' );
if(titleSpit && titleSpit.length > 0 && titleSpit[0].trim()) {
// Başlıktaki başında ve sonunda boşluklar varsa sil.
dataObject.title = titleSpit[0].replace(/^[ ]+|[ ]+$/g,"");
}
if(yearSpit && yearSpit.length > 0) {
// Yıldaki () silip int çevir.
dataObject.year = parseInt(yearSpit[0].replace(/[()]/g,""));
}
// Sezon ve bölüm sayılarını alma.
if(seasonAndEpisodeSpit && seasonAndEpisodeSpit.length > 0 && seasonAndEpisodeSpit[1].includes("Season")) {
var test = seasonAndEpisodeSpit[1].split("|");
dataObject.season = parseInt(test[0].match(/\d+/g)[0]);
dataObject.episode = parseInt(test[1].match(/\d+/g)[0]);
}
// Image alma.
if($(element).find("img")[0] && $(element).find("img")[0].attribs) {
var imageUrl = $(element).find("img")[0].attribs.src;
dataObject.image = imageUrl;
}
// Bug olmaması için resim ve title zorunlu alanlar dolu ise pushla. Aksi halde title boş sadece resim pushlanabiliyor.
if(dataObject.image && dataObject.title) {
this.data.push(dataObject);
}
// Her 2 döngüde bir verileri sıfırlama
if(index === 1 || index % 2 !== 0) {
dataObject = {
image: null,
title: null,
year: null,
season: null,
episode: null
};
}
});
return this.data;
}
}
module.exports = SearchFilm;