-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (33 loc) · 1.35 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
const request = require('request')
const cheerio = require('cheerio')
module.exports = function(id, opts = {}) {
opts = {
sort: opts.sort || 'helpfulnessScore',
ratingFilter: opts.ratingFilter || '0',
spoilers: opts.spoilers || 'show',
paginationKey: opts.paginationKey || ''
}
let url = `https://www.imdb.com/title/${id}/reviews/_ajax?spoiler=${opts.spoilers}&sort=${opts.sort}&ratingFilter=${opts.ratingFilter}&paginationKey=${opts.paginationKey}`
return new Promise((resolve, reject) => {
request(url, (err, res, body) => {
if (err) { reject(err); return }
let $ = cheerio.load(body)
if (!$('.lister-list').length) { reject(null); return }
let reviews = {
data: [],
nextPageKey: $('.load-more-data').length ? $('.load-more-data').attr('data-key') : ''
}
$('.imdb-user-review').each(function() {
reviews.data.push({
author: $(this).find('.display-name-link').text(),
date: $(this).find('.review-date').text(),
title: $(this).find('.title').text().trim(),
rating: $(this).find('.rating-other-user-rating').text().trim(),
body: $(this).find('.text').html(),
spoilers: !!$(this).find('.spoiler-warning').length,
})
})
resolve(reviews)
})
})
}