-
Notifications
You must be signed in to change notification settings - Fork 788
/
Copy pathMIT Press Books.js
621 lines (592 loc) · 16.2 KB
/
MIT Press Books.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
{
"translatorID": "ac277fbe-000c-46da-b145-fbe799d17eda",
"label": "MIT Press Books",
"creator": "Guy Aglionby",
"target": "https://(www\\.)?mitpress\\.mit\\.edu/(mit-press-open|contributors|search|series|distribution|topics|forthcoming|best-sellers|books)",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2021-06-28 22:21:37"
}
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2019 Guy Aglionby
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 (url.includes('/books/') && !url.includes('/series/') && !url.includes('/distribution/') && !url.includes('/imprint/')) {
return 'book';
}
else if (getSearchResults(doc, true)) {
return 'multiple';
}
return false;
}
function doWeb(doc, url) {
if (detectWeb(doc, url) === 'multiple') {
Zotero.selectItems(getSearchResults(doc, false), function (selected) {
if (selected) {
ZU.processDocuments(Object.keys(selected), scrape);
}
});
}
else {
scrape(doc);
}
}
function scrape(doc, url) {
let item = new Zotero.Item('book');
item.url = url;
item.place = 'Cambridge, MA, USA';
item.language = 'en';
item.date = ZU.xpathText(doc, '(//time[@property = "publishDate"]/@content)[1]');
item.ISBN = ZU.xpathText(doc, '(//span[@property = "isbn"])[1]');
item.numPages = ZU.xpathText(doc, '(//span[@property = "numPages"])[1]');
let abstract = doc.querySelector('div.book__blurb');
if (abstract) {
item.abstractNote = abstract.textContent.trim();
}
let title = ZU.xpathText(doc, '//h1[@class = "book__title"]').trim();
let editionRegex = /, (([\w ]+) edition( [\w ]+)?)/i;
let matchedEdition = title.match(editionRegex);
if (matchedEdition) {
item.edition = cleanEdition(matchedEdition[1]);
title = title.replace(matchedEdition[0], '');
}
let volumeRegex = /, volume (\d+)/i;
let matchedVolume = title.match(volumeRegex);
if (matchedVolume) {
item.volume = matchedVolume[1];
title = title.replace(matchedVolume[0], '');
}
let subtitle = ZU.xpathText(doc, '//h2[@class = "book__subtitle"]');
if (subtitle) {
subtitle = subtitle.trim();
item.title = [title, subtitle].join(': ');
}
else {
item.title = title;
}
const contributorTypes = [['By', 'author'], ['Translated by', 'translator'], ['Edited by', 'editor']];
let allContributors = ZU.xpath(doc, '//span[@class = "book__authors"]/p');
allContributors.forEach(function (contributorLine) {
contributorLine = contributorLine.textContent;
contributorTypes.forEach(function (contributorType) {
if (contributorLine.startsWith(contributorType[0])) {
let contributors = contributorLine.replace(contributorType[0], '').split(/ and |,/);
contributors.forEach(function (contributorName) {
item.creators.push(ZU.cleanAuthor(contributorName, contributorType[1]));
});
}
});
});
let series = ZU.xpathText(doc, '//p[@class = "book__series"]/a[contains(@href, "/series/")]');
if (series) {
item.series = series.trim();
}
let publisher = ZU.xpathText(doc, '//p[@class = "book__series"]/a[contains(@href, "/imprint/") or contains(@href, "/distribution/")]');
if (publisher) {
item.publisher = publisher.trim();
}
else {
item.publisher = 'MIT Press';
}
let openAccessUrl = ZU.xpathText(doc, '//div[contains(@class, "open-access")]/a/@href');
if (openAccessUrl) {
if (openAccessUrl.endsWith('.pdf') || openAccessUrl.endsWith('.pdf?dl=1')) {
item.attachments.push({
url: openAccessUrl,
title: 'Full Text PDF',
mimeType: 'application/pdf'
});
}
else {
item.attachments.push({
url: openAccessUrl,
title: 'Open Access',
mimeType: 'text/html'
});
}
}
let seriesURL = attr(doc, '.book__series a', 'href');
if (seriesURL) {
ZU.processDocuments(seriesURL, function (seriesDoc) {
let seriesEditors = text(seriesDoc, '.series__editors')
.split(/,| and /);
for (let seriesEditor of seriesEditors) {
let creator = ZU.cleanAuthor(seriesEditor, 'seriesEditor');
// sometimes series editors are also editors of individual
// volumes, and it doesn't make sense to include the same name
// twice. not an efficient approach but we're dealing with 4-5
// contributors max.
let duplicate = false;
for (let other of item.creators) {
if (other.firstName == creator.firstName && other.lastName == creator.lastName) {
duplicate = true;
}
}
if (!duplicate) {
item.creators.push(creator);
}
}
item.complete();
});
}
else {
item.complete();
}
}
function cleanEdition(text) {
if (!text) return text;
// from Taylor & Francis eBooks translator, slightly adapted
const ordinals = {
first: "1",
second: "2",
third: "3",
fourth: "4",
fifth: "5",
sixth: "6",
seventh: "7",
eighth: "8",
ninth: "9",
tenth: "10"
};
text = ZU.trimInternal(text).replace(/[[\]]/g, '');
// this somewhat complicated regex tries to isolate the number (spelled out
// or not) and make sure that it isn't followed by any extra info
let matches = text
.match(/^(?:(?:([0-9]+)(?:st|nd|rd|th)?)|(first|second|third|fourth|fifth|sixth|seventh|eighth|ninth|tenth))(?:\s?ed?\.?|\sedition)?$/i);
if (matches) {
let edition = matches[1] || matches[2];
edition = ordinals[edition.toLowerCase()] || edition;
return edition == "1" ? null : edition;
}
else {
return text;
}
}
function getSearchResults(doc, checkOnly) {
let rows = ZU.xpath(doc, '//ul[contains(@class, "results__list")]//a[@property = "name"]');
if (checkOnly) {
return rows.length > 0;
}
let items = {};
for (let i = 0; i < rows.length; i++) {
items[rows[i].href] = rows[i].text.trim();
}
return items;
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "https://mitpress.mit.edu/search?keywords=deep+learning",
"items": "multiple"
},
{
"type": "web",
"url": "https://mitpress.mit.edu/contributors/ian-goodfellow",
"items": "multiple"
},
{
"type": "web",
"url": "https://mitpress.mit.edu/books/series/adaptive-computation-and-machine-learning-series",
"items": "multiple"
},
{
"type": "web",
"url": "https://mitpress.mit.edu/books/imprint/bradford-book",
"items": "multiple"
},
{
"type": "web",
"url": "https://mitpress.mit.edu/books/distribution/urbanomic",
"items": "multiple"
},
{
"type": "web",
"url": "https://mitpress.mit.edu/books/elements-causal-inference",
"items": [
{
"itemType": "book",
"title": "Elements of Causal Inference: Foundations and Learning Algorithms",
"creators": [
{
"firstName": "Jonas",
"lastName": "Peters",
"creatorType": "author"
},
{
"firstName": "Dominik",
"lastName": "Janzing",
"creatorType": "author"
},
{
"firstName": "Bernhard",
"lastName": "Schölkopf",
"creatorType": "author"
},
{
"firstName": "Francis",
"lastName": "Bach",
"creatorType": "seriesEditor"
}
],
"date": "2017-11-29",
"ISBN": "9780262037310",
"abstractNote": "A concise and self-contained introduction to causal inference, increasingly important in data science and machine learning.",
"language": "en",
"libraryCatalog": "MIT Press Books",
"numPages": "288",
"place": "Cambridge, MA, USA",
"publisher": "MIT Press",
"series": "Adaptive Computation and Machine Learning series",
"shortTitle": "Elements of Causal Inference",
"attachments": [
{
"title": "Full Text PDF",
"mimeType": "application/pdf"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://mitpress.mit.edu/books/sciences-artificial-reissue-third-edition-new-introduction-john-laird",
"items": [
{
"itemType": "book",
"title": "The Sciences of the Artificial",
"creators": [
{
"firstName": "Herbert A.",
"lastName": "Simon",
"creatorType": "author"
}
],
"date": "2019-08-13",
"ISBN": "9780262537537",
"abstractNote": "Herbert Simon's classic work on artificial intelligence in the expanded and updated third edition from 1996, with a new introduction by John E. Laird.",
"edition": "Reissue Of The Third Edition With A New Introduction By John Laird",
"language": "en",
"libraryCatalog": "MIT Press Books",
"numPages": "256",
"place": "Cambridge, MA, USA",
"publisher": "MIT Press",
"attachments": [],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://mitpress.mit.edu/books/construction-site-possible-worlds",
"items": [
{
"itemType": "book",
"title": "Construction Site for Possible Worlds",
"creators": [
{
"firstName": "Amanda",
"lastName": "Beech",
"creatorType": "editor"
},
{
"firstName": "Robin",
"lastName": "Mackay",
"creatorType": "editor"
},
{
"firstName": "James",
"lastName": "Wiltgen",
"creatorType": "editor"
}
],
"date": "2020-08-11",
"ISBN": "9781913029579",
"abstractNote": "Perspectives from philosophy, aesthetics, and art on how to envisage the construction site of possible worlds.",
"language": "en",
"libraryCatalog": "MIT Press Books",
"numPages": "276",
"place": "Cambridge, MA, USA",
"publisher": "Urbanomic",
"attachments": [],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://mitpress.mit.edu/books/ribbon-olympias-throat",
"items": [
{
"itemType": "book",
"title": "The Ribbon at Olympia's Throat",
"creators": [
{
"firstName": "Michel",
"lastName": "Leiris",
"creatorType": "author"
},
{
"firstName": "Christine",
"lastName": "Pichini",
"creatorType": "translator"
}
],
"date": "2019-07-02",
"ISBN": "9781635900842",
"abstractNote": "Short fragments and essays that explore how a seemingly irrelevant aesthetic detail may cause the eruption of sublimity within the mundane.",
"language": "en",
"libraryCatalog": "MIT Press Books",
"numPages": "288",
"place": "Cambridge, MA, USA",
"publisher": "Semiotext(e)",
"series": "Semiotext(e) / Native Agents",
"attachments": [],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://mitpress.mit.edu/books/foundations-machine-learning-second-edition",
"items": [
{
"itemType": "book",
"title": "Foundations of Machine Learning",
"creators": [
{
"firstName": "Mehryar",
"lastName": "Mohri",
"creatorType": "author"
},
{
"firstName": "Afshin",
"lastName": "Rostamizadeh",
"creatorType": "author"
},
{
"firstName": "Ameet",
"lastName": "Talwalkar",
"creatorType": "author"
},
{
"firstName": "Francis",
"lastName": "Bach",
"creatorType": "seriesEditor"
}
],
"date": "2018-12-25",
"ISBN": "9780262039406",
"abstractNote": "A new edition of a graduate-level machine learning textbook that focuses on the analysis and theory of algorithms.",
"edition": "2",
"language": "en",
"libraryCatalog": "MIT Press Books",
"numPages": "504",
"place": "Cambridge, MA, USA",
"publisher": "MIT Press",
"series": "Adaptive Computation and Machine Learning series",
"attachments": [
{
"title": "Open Access",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://mitpress.mit.edu/books/collapse-volume-8",
"items": [
{
"itemType": "book",
"title": "Collapse: Casino Real",
"creators": [
{
"firstName": "Robin",
"lastName": "Mackay",
"creatorType": "editor"
}
],
"date": "2018-10-23",
"ISBN": "9780956775023",
"abstractNote": "An assembly of perspectives on risk, contingency, and chance—at the gaming table, in the markets, and in life.",
"language": "en",
"libraryCatalog": "MIT Press Books",
"numPages": "1020",
"place": "Cambridge, MA, USA",
"publisher": "Urbanomic",
"series": "Urbanomic / Collapse",
"shortTitle": "Collapse",
"volume": "8",
"attachments": [],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://mitpress.mit.edu/books/reinforcement-learning-second-edition",
"items": [
{
"itemType": "book",
"title": "Reinforcement Learning: An Introduction",
"creators": [
{
"firstName": "Richard S.",
"lastName": "Sutton",
"creatorType": "author"
},
{
"firstName": "Andrew G.",
"lastName": "Barto",
"creatorType": "author"
},
{
"firstName": "Francis",
"lastName": "Bach",
"creatorType": "seriesEditor"
}
],
"date": "2018-11-13",
"ISBN": "9780262039246",
"abstractNote": "The significantly expanded and updated new edition of a widely used text on reinforcement learning, one of the most active research areas in artificial intelligence.",
"edition": "2",
"language": "en",
"libraryCatalog": "MIT Press Books",
"numPages": "552",
"place": "Cambridge, MA, USA",
"publisher": "A Bradford Book",
"series": "Adaptive Computation and Machine Learning series",
"shortTitle": "Reinforcement Learning",
"attachments": [
{
"title": "Open Access",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://mitpress.mit.edu/books/architecture-and-action",
"items": [
{
"itemType": "book",
"title": "Architecture and Action",
"creators": [
{
"firstName": "J. Meejin",
"lastName": "Yoon",
"creatorType": "editor"
},
{
"firstName": "Irina",
"lastName": "Chernyakova",
"creatorType": "editor"
}
],
"date": "2019-07-02",
"ISBN": "9780998117065",
"abstractNote": "Projects and texts that address architecture's role in taking on complex global challenges including climate change, housing, migration, and social justice.",
"language": "en",
"libraryCatalog": "MIT Press Books",
"numPages": "350",
"place": "Cambridge, MA, USA",
"publisher": "SA+P Press",
"series": "Agendas in Architecture",
"attachments": [],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://mitpress.mit.edu/books/acquired-tastes",
"items": [
{
"itemType": "book",
"title": "Acquired Tastes: Stories about the Origins of Modern Food",
"creators": [
{
"firstName": "Benjamin R.",
"lastName": "Cohen",
"creatorType": "editor"
},
{
"firstName": "Michael S.",
"lastName": "Kideckel",
"creatorType": "editor"
},
{
"firstName": "Anna",
"lastName": "Zeide",
"creatorType": "editor"
},
{
"firstName": "Robert",
"lastName": "Gottlieb",
"creatorType": "seriesEditor"
},
{
"firstName": "Nevin",
"lastName": "Cohen",
"creatorType": "seriesEditor"
}
],
"date": "2021-08-17",
"ISBN": "9780262542913",
"abstractNote": "How modern food helped make modern society between 1870 and 1930: stories of power and food, from bananas and beer to bread and fake meat.",
"language": "en",
"libraryCatalog": "MIT Press Books",
"numPages": "290",
"place": "Cambridge, MA, USA",
"publisher": "MIT Press",
"series": "Food, Health, and the Environment",
"shortTitle": "Acquired Tastes",
"attachments": [],
"tags": [],
"notes": [],
"seeAlso": []
}
]
}
]
/** END TEST CASES **/