-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex-request.js
101 lines (93 loc) · 2.9 KB
/
index-request.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
const requestPromise = require('request-promise');
const cheerio = require('cheerio');
const fs = require('fs');
const request = require('request');
const URLS = [
{
url: 'https://www.imdb.com/title/tt0462538/?ref_=fn_al_tt_2',
id: 'the_simpsons_movie',
},
{
url: 'https://www.imdb.com/title/tt0454876/?ref_=nv_sr_srsg_3',
id: 'the_life_of_pie',
},
];
(async () => {
let moviesData = [];
for (let movie of URLS) {
const response = await requestPromise({
uri: movie.url,
headers: {
Host: 'www.imdb.com',
'User-Agent': 'Super-Scraper',
Accept:
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
Connection: 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Cache-Control': 'max-age=0',
TE: 'Trailers',
},
gzip: true,
});
let $ = cheerio.load(response);
let title = $('div[class="title_wrapper"] > h1').text().trim();
let rating = $('div[class="ratingValue"] > strong > span').text();
let posterURL = $('div[class="poster"] > a > img').attr('src');
let ratingCount = $(
'div[class="imdbRating"] > a > span[itemprop="ratingCount"]'
).text();
let releaseDate = $('a[title="See more release dates"]').text().trim();
let genres = [];
$(
'div[class="title_wrapper"] > div[class="subtext"] > a[href^="/search/"]'
).each((i, elm) => {
let genre = $(elm).text();
genres.push(genre);
});
moviesData.push({
title,
rating,
posterURL,
ratingCount,
releaseDate,
genres,
});
let file = fs.createWriteStream(`${movie.id}.jpg`);
await new Promise((resolve, reject) => {
let stream = request({
uri: posterURL,
headers: {
'User-Agent': 'Super-Scraper',
Accept:
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
Connection: 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Cache-Control': 'max-age=0',
TE: 'Trailers',
},
gzip: true,
})
.pipe(file)
.on('finish', () => {
console.log(`${movie.id} has finished downloading the image.`);
resolve();
})
.on('error', (error) => {
reject(error);
});
}).catch((error) => {
console.log(`${movie.id} has an error on download. ${error}`);
});
}
// fs.writeFileSync('./data.json', JSON.stringify(moviesData), 'utf-8');
})();