-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (78 loc) · 2.68 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
const fs = require('fs');
const request = require('request');
const cheerio = require('cheerio');
const requestPromise = require('request-promise');
const Json2csvParser = require('json2csv').Parser;
const URLS = [
'https://www.imdb.com/title/tt6565702/',
'https://www.imdb.com/title/tt5114356/',
'https://www.imdb.com/title/tt0111161/',
'https://www.imdb.com/title/tt0068646/',
'https://www.imdb.com/title/tt0071562/',
'https://www.imdb.com/title/tt0108052/',
];
const HEADER = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-CN;q=0.6,la;q=0.5',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
};
(async () => {
let moviesData = [];
for (let movie of URLS) {
const response = await requestPromise({
uri: movie,
headers: { ...HEADER, 'Host': 'www.imdb.com'},
gzip: true
});
// Data Parsing
let $ = cheerio.load(response);
let title = $('div[class="title_wrapper"] > h1').text().trim();
let rating = $('div[class="ratingValue"] > strong > span').text();
let poster = $('div[class="poster"] > a > img').attr('src');
let totalRatings = $('div[class="imdbRating"] > a').text();
let releaseDate = $('a[title="See more release dates"]').text().trim();
let genres = [];
$('div[class="title_wrapper"] a[href^="/search/title"]').each((i, ele) => {
let genre = $(ele).text();
genres.push(genre);
});
moviesData.push({
title,
rating,
poster,
totalRatings,
releaseDate,
genres
});
// Download Images
let file = fs.createWriteStream(`./images/Poster - ${title}.jpg`);
await new Promise((resolve, reject) => {
let stream = request({
uri: poster,
headers: HEADER,
gzip: true
})
.pipe(file)
.on('finish', () => {
console.log(`${title} has finished Downloading the image.`);
resolve();
})
.on('error', (error) => {
reject(error);
})
})
.catch(error => {
console.log(`${title} has an error on download. ${error}`);
});
// Write date to files
const json2csvParser = new Json2csvParser();
const csv = json2csvParser.parse(moviesData);
fs.writeFileSync('./data/data.csv', csv, 'utf-8');
fs.writeFileSync('./data/data.json', JSON.stringify(moviesData), 'utf-8');
debugger;
}
})();