forked from Taiwan-Ebook-Lover/TaiwanEbookSearch-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadmoo.js
More file actions
65 lines (52 loc) · 1.75 KB
/
Copy pathreadmoo.js
File metadata and controls
65 lines (52 loc) · 1.75 KB
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
const rp = require('request-promise-native');
const cheerio = require('cheerio');
function searchBooks(keywords = '') {
// URL encode
keywords = encodeURIComponent(keywords);
const options = {
uri: `https://readmoo.com/search/keyword`,
qs: {
q: keywords,
kw: keywords,
pi: 0,
st: true,
},
resolveWithFullResponse: true,
simple: false,
gzip: true,
timeout: 10000,
};
return rp(options).then(response =>{
if (!(/^2/.test('' + response.statusCode))) {
// console.log('Not found or error in readmoo!');
return [];
}
return _getBooks(cheerio.load(response.body));
}).catch(error => {
console.log(error.message);
return [];
});
}
// parse 找書
function _getBooks($) {
$list = $('#main_items li');
let books = [];
// 找不到就是沒這書
if ($list.length === 0) {
// console.log('Not found in readmoo!');
return books;
}
$list.each((i, elem) => {
books[i] = {
id: $(elem).children('.caption').children('.price-info').children('meta[itemprop=identifier]').prop('content'),
thumbnail: $(elem).children('.thumbnail').children('a').children('img').data('original'),
title: $(elem).children('.caption').children('h4').children('a').text(),
link: $(elem).children('.caption').children('h4').children('a').prop('href'),
priceCurrency: $(elem).children('.caption').children('.price-info').children('meta[itemprop=priceCurrency]').prop('content'),
price: parseFloat($(elem).children('.caption').children('.price-info').children('.our-price').children('strong').text().replace(/NT\$|,/g, '')),
about: $(elem).children('.caption').children('.description').text(),
};
});
return books;
}
exports.searchBooks = searchBooks;