forked from zotero/translators
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Bloomsbury Food Library.js
296 lines (275 loc) · 7.45 KB
/
Bloomsbury Food Library.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
{
"translatorID": "0524c89b-2a96-4d81-bb05-ed91ed8b2b47",
"label": "Bloomsbury Food Library",
"creator": "Abe Jellinek",
"target": "^https?://(www\\.)?bloomsburyfoodlibrary\\.com/",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2021-08-03 01:17:12"
}
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2021 Abe Jellinek
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) {
if (doc.querySelector('a[href*="/getris"]')) {
if (url.includes('bloomsburyfoodlibrary.com/encyclopedia-chapter')) {
if (text(doc, '.subfacet').includes('Book chapter')) {
return 'bookSection';
}
else {
return 'encyclopediaArticle';
}
}
else if (url.includes('bloomsburyfoodlibrary.com/audio')) {
// would like to support these, but the RIS isn't useful
return false;
}
else if (url.includes('bloomsburyfoodlibrary.com/museum')) {
return 'artwork';
}
else {
return 'book';
}
}
else if (getSearchResults(doc, true)) {
return "multiple";
}
return false;
}
function getSearchResults(doc, checkOnly) {
var items = {};
var found = false;
var rows = doc.querySelectorAll('a#search-result-link');
for (let row of rows) {
let href = row.href;
let title = ZU.trimInternal(row.textContent);
if (!href || !title) continue;
if (checkOnly) return true;
found = true;
items[href] = title;
}
return found ? items : false;
}
function doWeb(doc, url) {
if (detectWeb(doc, url) == "multiple") {
Zotero.selectItems(getSearchResults(doc, false), function (items) {
if (items) ZU.processDocuments(Object.keys(items), scrape);
});
}
else {
scrape(doc, url);
}
}
function scrape(doc, url) {
var risURL = attr(doc, 'a[href*="/getris"]', 'href');
ZU.doGet(risURL, function (text) {
var translator = Zotero.loadTranslator("import");
translator.setTranslator("32d59d2d-b65a-4da4-b0a3-bdd3cfb979e7");
translator.setString(text);
translator.setHandler("itemDone", function (obj, item) {
let detectedType = detectWeb(doc, url);
if (detectedType == 'encyclopediaArticle' && item.itemType == 'bookSection') {
item.itemType = 'encyclopediaArticle';
item.encyclopediaTitle = item.bookTitle;
delete item.bookTitle;
}
else if (detectedType == 'book' && item.itemType == 'bookSection') {
item.itemType = 'book';
delete item.bookTitle;
// filter out duplicate "editor" names
let names = new Set();
item.creators = item.creators.filter((creator) => {
let fullName = `${creator.lastName}, ${creator.firstName}`;
if (names.has(fullName)) {
return false;
}
names.add(fullName);
return true;
});
}
else if (detectedType == 'artwork') {
item.itemType = 'artwork';
}
for (let prop of ['title', 'bookTitle', 'encyclopediaTitle']) {
if (item[prop]) {
item[prop] = item[prop].replace(/ : /, ': ');
}
}
if (item.publisher) {
item.publisher = item.publisher.replace('©', '');
}
item.archive = '';
// contains full text, if we're on a chapter
item.attachments.push({
title: 'Snapshot',
document: doc
});
item.complete();
});
translator.translate();
});
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "https://www.bloomsburyfoodlibrary.com/encyclopedia-chapter?docid=b-9781474208642&tocid=b-9781474208642-000399&pdfid=9781474208642.0008.pdf",
"defer": true,
"items": [
{
"itemType": "encyclopediaArticle",
"title": "Burkina Faso",
"creators": [
{
"lastName": "Debevec",
"firstName": "Liza",
"creatorType": "author"
},
{
"lastName": "Albala",
"firstName": "Ken",
"creatorType": "editor"
}
],
"date": "2011",
"ISBN": "9781474208642",
"edition": "1",
"encyclopediaTitle": "Food Cultures of the World Encyclopedia: Africa and the Middle East",
"language": "en",
"libraryCatalog": "Bloomsbury Food Library",
"pages": "23-30",
"place": "Santa Barbara",
"publisher": "ABC-Clio Inc",
"url": "https://www.bloomsburyfoodlibrary.com/encyclopedia-chapter?docid=b-9781474208642&tocid=b-9781474208642-000399",
"volume": "1",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.bloomsburyfoodlibrary.com/encyclopedia?docid=b-9781474205283",
"items": [
{
"itemType": "book",
"title": "The Agency of Eating: Mediation, Food and the Body",
"creators": [
{
"lastName": "Abbots",
"firstName": "Emma-Jayne",
"creatorType": "author"
}
],
"date": "2017",
"ISBN": "9781474205283",
"edition": "1",
"language": "en",
"libraryCatalog": "Bloomsbury Food Library",
"place": "London",
"publisher": "Bloomsbury Academic",
"series": "Contemporary Food Studies: Economy, Culture and Politics",
"shortTitle": "The Agency of Eating",
"url": "https://www.bloomsburyfoodlibrary.com/encyclopedia?docid=b-9781474205283",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.bloomsburyfoodlibrary.com/encyclopedia?docid=b-9781474203937",
"items": [
{
"itemType": "book",
"title": "Agri-Food and Rural Development: Sustainable Place-Making",
"creators": [
{
"lastName": "Marsden",
"firstName": "Terry",
"creatorType": "author"
}
],
"date": "2017",
"ISBN": "9781474203937",
"edition": "1",
"language": "en",
"libraryCatalog": "Bloomsbury Food Library",
"place": "London",
"publisher": "Bloomsbury Academic",
"series": "Contemporary Food Studies: Economy, Culture and Politics",
"shortTitle": "Agri-Food and Rural Development",
"url": "https://www.bloomsburyfoodlibrary.com/encyclopedia?docid=b-9781474203937",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.bloomsburyfoodlibrary.com/museumobject?docid=TNA_COPY1_241-95",
"defer": true,
"items": [
{
"itemType": "artwork",
"title": "Advert for Hill Evans's barley malt vinegar",
"creators": [],
"libraryCatalog": "Bloomsbury Food Library",
"url": "https://www.bloomsburyfoodlibrary.com/museum?docid=TNA_COPY1_241-95",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.bloomsburyfoodlibrary.com/search-results?any=corn",
"items": "multiple"
}
]
/** END TEST CASES **/