-
Notifications
You must be signed in to change notification settings - Fork 0
/
imdb.js
59 lines (51 loc) · 1.74 KB
/
imdb.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
const cheerio = require('cheerio');
const fetch = require('node-fetch');
const pLimit = require('p-limit');
const pSettle = require('p-settle');
const {IMDB_NAME_URL, IMDB_URL, P_LIMIT} = require('./constants');
/**
* Get filmography for a given actor
* @param {String} actor - imdb id
* @return {Array}
*/
const getFilmography = async actor => {
const response = await fetch(`${IMDB_NAME_URL}/${actor}`);
const body = await response.text();
const $ = cheerio.load(body);
return $('#filmo-head-actor + .filmo-category-section .filmo-row b a')
.map((i, element) => {
return {
'link': `${IMDB_URL}${$(element).attr('href')}`,
'title': $(element).text()
};
})
.get();
};
const getMovie = async link => {
const response = await fetch(link);
const body = await response.text();
const $ = cheerio.load(body);
return {
link,
'id': $('meta[property="pageId"]').attr('content'),
'metascore': Number($('.metacriticScore span').text()),
'poster': $('.poster img').attr('src'),
'rating': Number($('span[itemprop="ratingValue"]').text()),
'synopsis': $('.summary_text').text().trim(),
'title': $('.title_wrapper h1').text().trim(),
'votes': Number($('span[itemprop="ratingCount"]').text().replace(',', '.')),
'year': Number($('#titleYear a').text())
};
};
module.exports = async actor => {
const limit = pLimit(P_LIMIT);
const filmography = await getFilmography(actor);
const promises = filmography.map(filmo => {
return limit(async () => {
return await getMovie(filmo.link);
});
});
const results = await pSettle(promises);
const isFulfilled = results.filter(result => result.isFulfilled).map(result => result.value);
return [].concat.apply([], isFulfilled);
};