-
Notifications
You must be signed in to change notification settings - Fork 758
/
Douban.js
316 lines (286 loc) · 8.34 KB
/
Douban.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
{
"translatorID": "fc353b26-8911-4c34-9196-f6f567c93901",
"label": "Douban",
"creator": "Ace Strong<acestrong@gmail.com>",
"target": "^https?://(www|book)\\.douban\\.com/(subject|doulist|people/[a-zA-Z0-9._]*/(do|wish|collect)|.*?status=(do|wish|collect)|group/[0-9]*?/collection|tag)",
"minVersion": "2.0rc1",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2024-08-07 08:46:18"
}
/*
Douban Translator
Copyright (C) 2009-2010 TAO Cheng, acestrong@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// #######################
// ##### Sample URLs #####
// #######################
/*
* The starting point for an search is the URL below.
* In testing, I tried the following:
*
* - A search listing of books
* - A book page
* - A doulist page
* - A do page
* - A wish page
* - A collect page
*/
// http://book.douban.com/
// #################################
// #### Local utility functions ####
// #################################
function trimTags(text) {
return text.replace(/(<.*?>)/g, "");
}
// #############################
// ##### Scraper functions #####
// #############################
function scrapeAndParse(doc, url) {
// Z.debug({ url })
Zotero.Utilities.HTTP.doGet(url, function (page) {
// Z.debug(page)
var pattern;
// 类型 & URL
var itemType = "book";
var newItem = new Zotero.Item(itemType);
// Zotero.debug(itemType);
newItem.url = url;
// 标题
pattern = /<h1>([\s\S]*?)<\/h1>/;
if (pattern.test(page)) {
var title = pattern.exec(page)[1];
newItem.title = Zotero.Utilities.trim(trimTags(title));
// Zotero.debug("title: "+title);
}
// 又名
pattern = /<span [^>]*?>又名:(.*?)<\/span>/;
if (pattern.test(page)) {
var shortTitle = pattern.exec(page)[1];
newItem.shortTitle = Zotero.Utilities.trim(shortTitle);
// Zotero.debug("shortTitle: "+shortTitle);
}
// 作者
page = page.replace(/\n/g, "");
// Z.debug(page)
pattern = /<span>\s*<span[^>]*?>\s*作者<\/span>:(.*?)<\/span>/;
if (pattern.test(page)) {
var authorNames = trimTags(pattern.exec(page)[1]);
pattern = /(\[.*?\]|\(.*?\)|(.*?))/g;
authorNames = authorNames.replace(pattern, "").split("/");
// Zotero.debug(authorNames);
for (let i = 0; i < authorNames.length; i++) {
let useComma = true;
pattern = /[A-Za-z]/;
if (pattern.test(authorNames[i])) {
// 外文名
pattern = /,/;
if (!pattern.test(authorNames[i])) {
useComma = false;
}
}
newItem.creators.push(Zotero.Utilities.cleanAuthor(
Zotero.Utilities.trim(authorNames[i]),
"author", useComma));
}
}
// 译者
pattern = /<span>\s*<span [^>]*?>\s*译者<\/span>:(.*?)<\/span>/;
if (pattern.test(page)) {
var translatorNames = trimTags(pattern.exec(page)[1]);
pattern = /(\[.*?\])/g;
translatorNames = translatorNames.replace(pattern, "").split("/");
// Zotero.debug(translatorNames);
for (let i = 0; i < translatorNames.length; i++) {
let useComma = true;
pattern = /[A-Za-z]/;
if (pattern.test(translatorNames[i])) {
// 外文名
useComma = false;
}
newItem.creators.push(Zotero.Utilities.cleanAuthor(
Zotero.Utilities.trim(translatorNames[i]),
"translator", useComma));
}
}
// ISBN
pattern = /<span [^>]*?>ISBN:<\/span>(.*?)<br\/>/;
if (pattern.test(page)) {
var isbn = pattern.exec(page)[1];
newItem.ISBN = Zotero.Utilities.trim(isbn);
// Zotero.debug("isbn: "+isbn);
}
// 页数
pattern = /<span [^>]*?>页数:<\/span>(.*?)<br\/>/;
if (pattern.test(page)) {
var numPages = pattern.exec(page)[1];
newItem.numPages = Zotero.Utilities.trim(numPages);
// Zotero.debug("numPages: "+numPages);
}
// 出版社
pattern = /<span [^>]*?>出版社:<\/span>(.*?)<br\/>/;
if (pattern.test(page)) {
var publisher = pattern.exec(page)[1];
newItem.publisher = Zotero.Utilities.trim(publisher);
// Zotero.debug("publisher: "+publisher);
}
// 丛书
pattern = /<span [^>]*?>丛书:<\/span>(.*?)<br\/>/;
if (pattern.test(page)) {
var series = trimTags(pattern.exec(page)[1]);
newItem.series = Zotero.Utilities.trim(series);
// Zotero.debug("series: "+series);
}
// 出版年
pattern = /<span [^>]*?>出版年:<\/span>(.*?)<br\/>/;
if (pattern.test(page)) {
var date = pattern.exec(page)[1];
newItem.date = Zotero.Utilities.trim(date);
// Zotero.debug("date: "+date);
}
// 简介
var tags = ZU.xpath(doc, '//div[@id="db-tags-section"]/div//a');
for (let i in tags) {
newItem.tags.push(tags[i].textContent);
}
newItem.abstractNote = ZU.xpathText(doc, '//span[@class="short"]/div[@class="intro"]/p');
newItem.complete();
});
}
// #########################
// ##### API functions #####
// #########################
function detectWeb(doc, url) {
var pattern = /subject_search|doulist|people\/[a-zA-Z0-9._]*?\/(?:do|wish|collect)|.*?status=(?:do|wish|collect)|group\/[0-9]*?\/collection|tag/;
if (pattern.test(url)) {
return "multiple";
}
else {
return "book";
}
}
function detectTitles(doc, url) {
var pattern = /\.douban\.com\/tag\//;
if (pattern.test(url)) {
return ZU.xpath(doc, '//div[@class="info"]/h2/a');
} else {
return ZU.xpath(doc, '//div[@class="title"]/a');
}
}
function doWeb(doc, url) {
var articles = [];
let r = /douban.com\/url\//;
if (detectWeb(doc, url) == "multiple") {
// also searches but they don't work as test cases in Scaffold
// e.g. https://book.douban.com/subject_search?search_text=Murakami&cat=1001
var items = {};
// var titles = ZU.xpath(doc, '//div[@class="title"]/a');
var titles = detectTitles(doc, url);
var title;
for (let i = 0; i < titles.length; i++) {
title = titles[i];
// Zotero.debug({ href: title.href, title: title.textContent });
if (r.test(title.href)) { // Ignore links
continue;
}
items[title.href] = title.textContent;
}
Zotero.selectItems(items, function (items) {
if (!items) {
return;
}
for (var i in items) {
articles.push(i);
}
Zotero.Utilities.processDocuments(articles, scrapeAndParse);
});
}
else {
scrapeAndParse(doc, url);
}
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "https://book.douban.com/subject/1355643/",
"items": [
{
"itemType": "book",
"title": "Norwegian Wood",
"creators": [
{
"firstName": "Haruki",
"lastName": "Murakami",
"creatorType": "author"
},
{
"firstName": "Jay",
"lastName": "Rubin",
"creatorType": "translator"
}
],
"date": "2003",
"ISBN": "9780099448822",
"abstractNote": "When he hears her favourite Beatles song, Toru Watanabe recalls his first love Naoko, the girlfriend of his best friend Kizuki. Immediately he is transported back almost twenty years to his student days in Tokyo, adrift in a world of uneasy friendships, casual sex, passion, loss and desire - to a time when an impetuous young woman called Midori marches into his life and he has ..., (展开全部)",
"libraryCatalog": "Douban",
"numPages": "389",
"publisher": "Vintage",
"url": "https://book.douban.com/subject/1355643/",
"attachments": [],
"tags": [
{
"tag": "HarukiMurakami"
},
{
"tag": "小说"
},
{
"tag": "挪威森林英文版"
},
{
"tag": "日本"
},
{
"tag": "日本文学"
},
{
"tag": "村上春树"
},
{
"tag": "英文原版"
},
{
"tag": "英文版"
}
],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.douban.com/doulist/120664512/",
"items": "multiple"
},
{
"type": "web",
"url": "https://book.douban.com/tag/认知心理学?type=S",
"items": "multiple"
}
]
/** END TEST CASES **/