forked from l0o0/translators_CN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wenjin.js
463 lines (420 loc) · 13.5 KB
/
Wenjin.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
{
"translatorID": "f306107f-dabb-41ac-8fa2-f7f858feb11f",
"label": "Wenjin",
"creator": "Xingzhong Lin",
"target": "https?://find.nlc.cn/search",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2020-08-29 14:11:42"
}
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2020 Xingzhong Lin, https://github.com/Zotero-CN/translators_CN
This file is part of Zotero.
Zotero is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zotero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Zotero. If not, see <http://www.gnu.org/licenses/>.
***** END LICENSE BLOCK *****
*/
function processURL(urls) {
//Z.debug(urls);
var url = urls.pop();
//Z.debug(url);
//ZU.doGet(url, function(text) {
ZU.processDocuments(url,function(doc){
//Z.debug(text);
//var parser = new DOMParser();
//var doc = parser.parseFromString(text, "text/html");
//Z.debug(doc);
scrape_multiple(doc, url);
if (urls.length) {
processURL(urls);
}
})
}
function detectWeb(doc, url) {
if (url.includes('/search/showDocDetails')) {
return detectType(doc);
}
else if (url.includes("search/doSearch?query")) {
return 'multiple';
}
return false;
}
function detectType(doc) {
var itemType = {
普通古籍: "book",
善本: "book",
学位论文: "thesis",
特藏古籍: "book",
期刊论文:"journalArticle",
期刊: "journalArticle",
报纸: "newspaperArticle",
专著: "book",
报告: "report"
};
var type = ZU.xpath(doc, "//span[@class='book_val']");
if (type.length) {
Z.debug(type[0].textContent);
return itemType[type[0].textContent];
} else {
return false;
}
}
function getSearchResults(doc, checkOnly) {
var items = {};
var found = false;
var rows = ZU.xpath(doc, "//div[@class='article_item']");
Z.debug(rows.length);
if (checkOnly) {
return rows.length ? 'multiple' : false;
}
for (let i=0; i < rows.length; i++) {
//Z.debug(rows[i])
let title = ZU.xpath(rows[i], ".//div[@class='book_name']/a")[0];
//Z.debug(title.textContent);
let click = title.getAttribute('onclick').split("'");
//Z.debug(click);
let href = "http://find.nlc.cn/search/showDocDetails?docId="
+ click[3]
+ "&dataSource="
+ click[5]
+ "&query="
+ encodeURI(click[7]);
Z.debug(href);
title = ZU.trimInternal(title.textContent);
if (!href || !title) continue;
found = true;
items[href] = (i+1) + " " + title;
//Z.debug(items[href]);
}
return found ? items : false;
}
function doWeb(doc, url) {
if (detectWeb(doc, url) == "multiple") {
Zotero.selectItems(getSearchResults(doc, false), function (items) {
if (items) {
//Z.debug(items);
//items的keys是网址
//items的value是title
//Object.keys返回items对象可枚举属性的字符串数组,传入对象,返回属性名即网址
processURL(Object.keys(items));
}
});
}
else {
scrape(doc, url);
}
}
function scrape(doc, url) {
var type = detectType(doc);
var newItem = new Zotero.Item(type);
var detailA = ZU.xpath(doc, "//div[@class='book_wr']")[0].innerText;
Z.debug(detailA);
//Z.debug(detailA.replace(/\s*/g,""));
var detailB = ZU.xpath(doc, "//div[@id='detail-info']")[0].innerText;
Z.debug(detailB);
var details = (detailA + "\n" + detailB).split("\n");
Z.debug(details);
var title = ZU.trimInternal(details[0]);
//Z.debug(title);
newItem.title = title;
var date = details.filter((ele) => ele.startsWith("出版发行时间:") || ele.startsWith("论文授予时间"));
if (date.length) {
newItem.date = ZU.trimInternal(date[0].split(":")[1]);
}
var tags = details.filter((ele) => ele.startsWith("关键词"));
if (tags.length) {
newItem.tags = ZU.trimInternal(tags[0]).split(": ")[1].split(/[ -;]/);
}
var tagsEN = details.filter((ele) => ele.startsWith("英文关键词"));
if (tagsEN.length) {
newItem.tags = ZU.trimInternal(tagsEN[0]).split(": ")[1].split(/;/);
}
var place = details.filter((ele) => ele.startsWith("出版、发行地"));
if (place.length) {
newItem.place = place[0].split(": ")[1];
}
var pages = details.filter((ele) => ele.startsWith("载体形态") || ele.startsWith("页 :"));
if (pages.length) {
if(type==="book" || type === "thesis")//book和thesis的tiem中页数的关键词是numPages,其他类型是pages
newItem.numPages = pages[0].split(": ")[1].replace("页", "");
else
{
newItem.pages = pages[0].split(": ")[1].replace("页", "");
}
}
var publisher = details.filter((ele) => ele.startsWith("出版、发行者"));
if (publisher.length) {
newItem.publisher = publisher[0].split(": ")[1];
}
var authors = details.filter((ele) => ele.startsWith("所有责任者") || ele.startsWith("作者:"));
Z.debug(authors);
if (authors.length) {
if (authors[0].search(/[A-Za-z]/) !== -1) {
authors = authors[authors.length-1].split(": ")[1].split(/[;|,]/)
} else {
authors = authors[authors.length-1].split(": ")[1].split(/[\s,;]+/) // Special comma
//Z.debug(authors);
}
newItem.creators = handleName(authors);
//Z.debug(authors);
}
var language= details.filter((ele) => ele.startsWith("语种"));
Z.debug(language);
if(language.length){
newItem.language=language[0].split(":")[1];
}
var note=ZU.xpath(doc, "//div[@class='zy_pp_val']");
//Z.debug(note);
if(note.length){
//Z.debug("test");
if(note[0].innerText.length){
newItem.abstractNote=ZU.trimInternal(note[0].innerText);
}
}
var university = details.filter((ele) => ele.startsWith("论文授予机构"));
if (university.length) {
newItem.university = university[0].split(": ")[1];
}
var abstract = details.filter((ele) => ele.startsWith("引文"));
if (abstract.length) {
newItem.abstractNote = abstract[0].split(":")[1];
}
var issue = details.filter((ele) => ele.startsWith("期"));
if (issue.length) {
newItem.issue = issue[0].split(":")[1];
}
var publication = details.filter((ele) => ele.startsWith("来源:"));
if (publication.length) {
var tmp = publication[0].split(": ")[1].split(/,/);
newItem.publication = tmp[0];
tmp.length > 2 ? newItem.journalAbbreviation = tmp[1] : false;
}
var ISSN = details.filter((ele) => ele.startsWith("标识号"));
if (ISSN.length) {
newItem.ISBN = ISSN[0].split(": ")[1];
}
newItem.url = url;
newItem.complete();
}
function scrape_multiple(doc,url){
//因multiple情况下使用doGet返回的doc与single情况下不同,故单独处理
var type = detectType(doc);
var newItem = new Zotero.Item(type);
var detailA = ZU.xpath(doc, "//div[@class='book_wr']")[0].innerText;
//Z.debug(detailA);
//Z.debug(detailA.replace(/\ +/g,""));
var detailB = ZU.xpath(doc, "//div[@id='detail-info']")[0].innerText;
//Z.debug(detailB);
var details = (detailA + "\n" + detailB).split("\n");
//Z.debug(details.filter((ele) => !ele.match(/^[ ]*$/)));
details=details.filter((ele) => !ele.match(/^[ ]*$/));
details=details.filter((ele) => !ele.match(/^[ \t]*$/));
details=details.filter((ele) => !ele.match(/^[ :]*$/));
Z.debug(details);
var title = ZU.trimInternal(details[0]);
//Z.debug(title);
newItem.title = title;
//var date = details.filter((ele) => ele.startsWith("出版发行时间:") || ele.startsWith("论文授予时间"));
var date=details[details.indexOf("出版发行时间:"||"论文授予时间")+1];
//Z.debug(date);
if (date.length) {
//newItem.date = ZU.trimInternal(date[0].split(":")[1]);
newItem.date=ZU.trimInternal(date);
}
var tags=details.filter((ele) => ele.endsWith("关键词"))[0];
if (tags.length) {
tags = details[details.indexOf(tags)+1];
newItem.tags = ZU.trimInternal(tags).split(/[ -|;]/);
}
var tagsEN = details.filter((ele) => ele.startsWith("英文关键词"));
if (tagsEN.length) {
newItem.tags = ZU.trimInternal(tagsEN[0]).split(": ")[1].split(/;/);
}
var place = details.filter((ele) => ele.endsWith("出版、发行地"));
if (place.length) {
place=details[details.indexOf(place[0])+1];
newItem.place = ZU.trimInternal(place);
}
//var pages = details.filter((ele) => ele.startsWith("载体形态") || ele.startsWith("页 :"));
var pages=details.filter((ele) => ele.endsWith("载体形态") || ele.endsWith("页 :"));
Z.debug(pages);
if (pages.length) {
pages=details[details.indexOf(pages[0])+1];
newItem.pages = pages.replace("页", "");
}
var publisher = details.filter((ele) => ele.startsWith("出版、发行者"));
if (publisher.length) {
publisher=details[details.indexOf(publisher[0])+1];
newItem.publisher = ZU.trimInternal(publisher);
}
var authors = details.filter((ele) => ele.endsWith("所有责任者") || ele.startsWith("作者:"));
//Z.debug(authors);
authors=details[details.indexOf(authors[0])+1];
//Z.debug(authors);
if (authors.length) {
if (authors.search(/[A-Za-z]/) !== -1) {
authors = authors.split(/;/)
} else {
authors = authors.split(/[\s,;]+/) // Special comma
//Z.debug(authors);
}
newItem.creators = handleName(authors);
//Z.debug(authors);
}
var language= details.filter((ele) => ele.endsWith("语种"));
if(language.length){
language=details[details.indexOf(language[0])+1];
newItem.language=ZU.trimInternal(language);
}
var note=ZU.xpath(doc, "//div[@class='zy_pp_val']");
//Z.debug(note);
if(note.length){
if(note[0].innerText.length){
newItem.abstractNote=ZU.trimInternal(note[0].innerText);
}
}
var university = details.filter((ele) => ele.startsWith("论文授予机构"));
if (university.length) {
university=details[details.indexOf(university[0])+1];
newItem.university = ZU.trimInternal(university);
}
var abstract = details.filter((ele) => ele.startsWith("引文"));
if (abstract.length) {
newItem.abstractNote = abstract[0].split(":")[1];
}
var issue = details.filter((ele) => ele.startsWith("期"));
if (issue.length) {
newItem.issue = issue[0].split(":")[1];
}
var publication = details.filter((ele) => ele.startsWith("来源:"));
if (publication.length) {
var tmp = publication[0].split(": ")[1].split(/,/);
newItem.publication = tmp[0];
tmp.length > 2 ? newItem.journalAbbreviation = tmp[1] : false;
}
var ISBN = details.filter((ele) => ele.endsWith("标识号"));
//Z.debug(ISBN);
ISBN=details[details.indexOf(ISBN[0])+1];
if (ISBN.length) {
//Z.debug(ISBN);
Z.debug(ISBN.split(':')[1]);
newItem.ISBN = (ISBN.split(':')[1]);
}
newItem.url = url;
newItem.complete();
}
function handleName(authors) {
// 有英文
var creators = [];
for (let author of authors) {
var creator = {};
var lastSpace = author.lastIndexOf(' ');
if (author.search(/[A-Za-z]/) !== -1 && lastSpace !== -1) {
// English
creator.firstName = author.slice(0, lastSpace);
creator.lastName = author.slice(lastSpace+1);
} else {
// Chinese
if (authors.indexOf(author) > -1) {
//if (author.endsWith("等") || author.endsWith("著")) {
// author = author.slice(0, author.length -1);
//作者姓名可能以"等"、"等编"、"编著"、"主编"、"著"这几种形式结尾
if (author.indexOf("等") !==-1) {
author=author.slice(0,author.indexOf("等"));
//Z.debug(author);
// 去除等或著后与其他姓名重名,跳过
if (authors.indexOf(author) > -1) {
continue;
}
}
else if(author.indexOf("主") !==-1){
author=author.slice(0,author.indexOf("主"));
if (authors.indexOf(author) > -1) {
continue;
}
}
else if(author.indexOf("编") !==-1){
author=author.slice(0,author.indexOf("编"));
if (authors.indexOf(author) > -1) {
continue;
}
}
else if(author.indexOf("著") !==-1){
author=author.slice(0,author.indexOf("著"));
if (authors.indexOf(author) > -1) {
continue;
}
}
}
//Z.debug(author);
creator.firstName = author.slice(1);
creator.lastName = author.charAt(0);
if (author.endsWith("指导")) {
creator.creatorType = "contributor";
}
}
creators.push(creator);
}
return creators;
}
// TEST URL
// http://find.nlc.cn/search/showDocDetails?docId=7225006674714026291&dataSource=ucs01,bslw&query=%E4%BF%A1%E7%94%A8
// http://find.nlc.cn/search/showDocDetails?docId=-8373230212045865087&dataSource=cjfd&query=wgcna
// http://find.nlc.cn/search/showDocDetails?docId=6614677564794870987&dataSource=wpqk&query=wgcna
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "http://find.nlc.cn/search/showDocDetails?docId=-4203196484494800823&dataSource=ucs01&query=%E6%B0%B4%E5%90%88%E7%89%A9",
"items": [
{
"itemType": "book",
"title": "天然气水合物气藏开发",
"creators": [
{
"fistName": "平",
"lastName": "郭"
},
{
"fistName": "士鑫",
"lastName": "刘"
},
{
"fistName": "建芬",
"lastName": "杜"
}
],
"date": "2006",
"libraryCatalog": "Wenjin",
"place": "北京",
"publisher": "石油工业出版社",
"url": "http://find.nlc.cn/search/showDocDetails?docId=-4203196484494800823&dataSource=ucs01&query=%E6%B0%B4%E5%90%88%E7%89%A9",
"attachments": [],
"tags": [
{
"tag": "天然气水合物"
},
{
"tag": "气田开发"
}
],
"notes": [],
"seeAlso": []
}
]
}
]
/** END TEST CASES **/