This repository has been archived by the owner on Jan 3, 2020. It is now read-only.
forked from zotero/translators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorldCat Discovery Service.js
317 lines (277 loc) · 8.2 KB
/
WorldCat Discovery Service.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
{
"translatorID": "fd8dc5f6-a6dd-42b2-948f-600f5da844ea",
"label": "WorldCat Discovery Service",
"creator": "Sebastian Karcher",
"target": "^https?://[^/]+\\.worldcat\\.org/",
"minVersion": "3.0.9",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsbv",
"lastUpdated": "2015-06-27 03:23:48"
}
/*
***** BEGIN LICENSE BLOCK *****
WorldCat Discovery Service translator; Copyright © 2015 Sebastian Karcher
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 detectWeb(doc, url) {
var results = getSearchResults(doc);
if (results.length) {
return "multiple";
}
//single result
// generate item and return type
var co = getFirstContextObj(doc);
if (!co | url.indexOf("?databaseList") == -1) return false;
return generateItem(doc, co).itemType;
}
/**
* Gets Zotero item from a WorldCat icon src
*/
function getZoteroType(iconSrc) {
// only specify types not specified in COinS
if (iconSrc.indexOf("icon-rec") != -1) {
return "audioRecording";
}
if (iconSrc.indexOf("icon-com") != -1) {
return "computerProgram";
}
if (iconSrc.indexOf("icon-map") != -1) {
return "map";
}
return false;
}
/**
* Generates a Zotero item from a single item WorldCat page,
* or the first item on a multiple item page
*/
function generateItem(doc, co) {
var item = new Zotero.Item();
ZU.parseContextObject(co, item);
// item types not covered by COinS will still need to be covered. Leaving the corresponding code from open Worldcat
// here as a reminder.
/*
var type = ZU.xpathText(doc,
'//img[@class="icn"][contains(@src, "icon-")][1]/@src');
if (type) {
type = getZoteroType(type);
if (type) item.itemType = type;
} */
return item;
}
function getSearchResults(doc) {
var results = ZU.xpath(doc, '//ol[@class="results"]/li[contains(@id, "record")]')
return results;
}
function getTitleNode(searchResult) {
return ZU.xpath(searchResult, './div[@class="title"]/a')[0];
}
function getFirstContextObj(doc) {
return ZU.xpathText(doc, '//span[@class="Z3988"][1]/@title');
}
/**
* Given an item URL, extract OCLC ID
*/
function extractOCLCID(url) {
var id = url.match(/\/oclc\/([^?]+)/);
if (!id) return false;
return id[1];
}
/**
* Given an item URL, extract database ID
*/
function extractDatabaseID(url) {
var id = url.match(/databaseList=([^&#]+)/);
if (!id) return false;
return decodeURIComponent(id[1]);
}
function composeURL(oclcID, databaseID) {
var risURL = "/share/citation.ris?oclcNumber=" + oclcID + "&databaseIds=" + encodeURIComponent(databaseID);
return risURL;
}
/**
* RIS Scraper Function
*
*/
function scrape(risURL) {
ZU.doGet(risURL, function(text) {
//Z.debug(text);
//conference proceedings exported as CONF, but fields match BOOK better
text = text.replace(/TY\s+-\s+CONF\s+[\s\S]+?\n\s*ER\s+-/g, function(m) {
return m.replace(/^TY\s+-\s+CONF\s*$/mg, 'TY - BOOK')
//authors are actually editors
.replace(/^A1\s+-\s+/mg, 'A3 - ');
})
//Zotero.debug("Importing corrected RIS: \n" + text);
var translator = Zotero.loadTranslator("import");
translator.setTranslator("32d59d2d-b65a-4da4-b0a3-bdd3cfb979e7");
translator.setString(text);
translator.setHandler("itemDone", function(obj, item) {
item.extra = undefined;
item.archive = undefined;
//clean up ISBNs
if (item.ISBN) {
var ISBN = item.ISBN.split(/\s/);
var ISBNarray = [];
for (var i = 0; i < ISBN.length; i++) {
if (ZU.cleanISBN(ISBN[i])) {
ISBNarray.push(ZU.cleanISBN(ISBN[i]));
}
}
item.ISBN = ISBNarray.join(" ");
}
//remove space before colon
item.title = item.title.replace(/\s+:/, ":")
//remove trailing colon after place
if (item.place) {
item.place = item.place.replace(/:\s*$/, "");
}
//remove traling period after publication
if (item.publicationTitle) {
item.publicationTitle = item.publicationTitle.replace(/\.\s*$/, "");
}
//remove trailing commar after publisher
if (item.publisher) {
item.publisher = item.publisher.replace(/,\s*$/, "");
}
//correct field mode for corporate authors
for (var i = 0; i < item.creators.length; i++) {
if (!item.creators[i].firstName) {
item.creators[i].fieldMode = 1;
}
}
//number of pages gets mapped to section???
if (item.section) {
//extract possible roman numerals and number of pages without the p
var numPages = item.section.match(/(([lxiv]+,\s*)?\d+)\s*p/);
if (numPages) item.numPages = numPages[1];
}
item.complete();
});
translator.getTranslatorObject(function(trans) {
trans.options.defaultItemType = 'book'; //if not supplied, default to book
trans.options.typeMap = {
'ELEC': 'book'
}; //ebooks should be imported as books
trans.doImport();
});
});
}
function doWeb(doc, url) {
var results = getSearchResults(doc);
if (detectWeb(doc, url) == "multiple") {
var items = {};
var articles = [];
for (var i = 0, n = results.length; i < n; i++) {
var title = ZU.xpathText(results[i], './/div[contains(@class, "title") and a[@class="record-title"]]');
//Z.debug(title)
if (!title) continue;
var oclcID = ZU.xpathText(results[i], './@data-oclcnum');
//Z.debug(oclcID)
var databaseID = ZU.xpathText(results[i], './@data-database-list');
//Z.debug(databaseID)
var risURL = composeURL(oclcID, databaseID);
Z.debug(risURL)
items[risURL] = title.trim();
}
Zotero.selectItems(items, function(items) {
if (!items) return true;
for (var i in items) {
articles.push(i);
}
scrape(articles);
});
} else {
var oclcID = extractOCLCID(url);
var databaseID = extractDatabaseID(url);
if (!oclcID) throw new Error("WorldCat: Failed to extract OCLC ID from URL: " + url);
var risURL = composeURL(oclcID, databaseID);
Z.debug("risURL= " + risURL)
scrape(risURL);
}
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "https://sbts.on.worldcat.org/oclc/795005226?databaseList=239,283,638",
"items": [
{
"itemType": "journalArticle",
"title": "Steven E. Runge. Discourse Grammar of the Greek New Testament",
"creators": [
{
"lastName": "Long",
"firstName": "C.",
"creatorType": "author"
}
],
"date": "2012",
"ISSN": "0360-3032",
"issue": "1",
"libraryCatalog": "WorldCat Discovery Service",
"pages": "129-132",
"publicationTitle": "Trinity journal",
"volume": "33",
"attachments": [],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "http://lpts.on.worldcat.org/search?queryString=au:Mary%20GrandPre%CC%81&databaseList=638",
"items": "multiple"
},
{
"type": "web",
"url": "http://sbts.on.worldcat.org/search?databaseList=&queryString=runge+discourse+grammar",
"items": "multiple"
},
{
"type": "web",
"url": "https://sbts.on.worldcat.org/oclc/667874424?databaseList=239,283,638",
"items": [
{
"itemType": "book",
"title": "Discourse grammar of the Greek New Testament: a practical introduction for teaching and exegesis",
"creators": [
{
"lastName": "Runge",
"firstName": "Steven E.",
"creatorType": "author"
}
],
"date": "2010",
"ISBN": "9781598565836\n1598565834",
"language": "English",
"libraryCatalog": "WorldCat Discovery Service",
"numPages": "xx, 404",
"place": "Peabody, Mass.",
"publisher": "Hendrickson Publishers Marketing",
"series": "Lexham Bible reference series; Lexham Bible reference series.",
"shortTitle": "Discourse grammar of the Greek New Testament",
"attachments": [],
"tags": [],
"notes": [],
"seeAlso": []
}
]
}
]
/** END TEST CASES **/