-
Notifications
You must be signed in to change notification settings - Fork 0
/
duokan.js
62 lines (46 loc) · 2.25 KB
/
duokan.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
var request = require('request'),
cheerio = require('cheerio'),
duokan_home = 'http://www.duokan.com';
module.exports = {
today_free: function(cb) {
/** 10. 查找今日限免的书 */
request(duokan_home, function(error, response, body) {
if (error) throw error;
if (response.statusCode != 200) throw new Error('Request duokan home failed');
var $ = cheerio.load(body),
free_url = $('img[alt="限时免费"]').parent().attr('href');
if (free_url.indexOf(duokan_home) < 0) {
free_url = duokan_home + free_url;
}
// console.log('loading ' + free_url);
// free_url = 'http://www.duokan.com/book/53260';
/** 20. 在多看获取本书信息 */
request(free_url, function(error, response, body) {
if (error) throw error;
if (response.statusCode != 200) throw new Error('Request duokan free book failed');
var $ = cheerio.load(body),
book = {
title: $('.m-bookdata h3').text(),
url: response.request.uri.href,
duokan_rating_value: $('.m-bookdata em[itemprop="ratingValue"]').text(),
duokan_rating_count: $('.g-mnc span[itemprop="reviewCount"]').text(),
isbn: $('.g-mnc span[itemprop="isbn"]').text()
};
// console.log(book);
var douban_url = 'http://book.douban.com/isbn/' + book.isbn;
/** 30. 在豆瓣获取更多信息 */
request(douban_url, function(error, response, body) {
if (error) throw error;
if (response.statusCode == 200) {
var $ = cheerio.load(body);
book.douban_url = response.request.uri.href;
book.douban_rating_value = $('strong[property="v:average"]').text().trim();
book.douban_rating_count = $('span[property="v:votes"]').text().trim();
// console.log(book);
}
cb(book);
});
});
});
}
};