forked from Taiwan-Ebook-Lover/TaiwanEbookSearch-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbooksCompany.js
More file actions
74 lines (57 loc) · 1.92 KB
/
Copy pathbooksCompany.js
File metadata and controls
74 lines (57 loc) · 1.92 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
66
67
68
69
70
71
72
73
74
const rp = require('request-promise-native');
const cheerio = require('cheerio');
function searchBooks(keywords = '') {
// URL encode
keywords = encodeURIComponent(keywords);
const options = {
uri: `http://search.books.com.tw/search/query/key/${keywords}/cat/EBA`,
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 books company!');
return [];
}
return _getBooks(cheerio.load(response.body));
}).catch(error => {
console.log(error.message);
return [];
});
}
// parse 找書
function _getBooks($) {
const $list = $('#searchlist ul li');
let books = [];
// 找不到就是沒這書
if ($list.length === 0) {
// console.log('Not found in books company!');
return books
}
$list.each((i, elem) => {
// 合併作者成一個陣列
let authors = [];
$(elem).children('a[rel=go_author]').each((i, elem) => {
authors = authors.concat($(elem).prop('title').split('、'));
});
books[i] = {
//
id: $(elem).children('.input_buy').children('input').prop('value'),
thumbnail: $(elem).children('a').children('img').data('original'),
title: $(elem).children('h3').children('a').prop('title'),
link: `http://www.books.com.tw/products/${$(elem).children('.input_buy').children('input').prop('value')}`,
priceCurrency: 'TWD',
price: parseFloat($(elem).children('.price').children('strong').last().children('b').text().replace(/NT\$|,/g, '')),
about: $(elem).children('p').text().replace(/......\ more\n\t\t\t\t\t\t\t\t/g, ' ...'),
publisher: $(elem).children('a[rel=mid_publish]').prop('title'),
};
// 作者群有資料才放
if (authors.length > 0) {
books[i].authors;
}
});
return books;
}
exports.searchBooks = searchBooks;