-
Notifications
You must be signed in to change notification settings - Fork 785
/
Copy pathThe New Yorker.js
360 lines (342 loc) · 8.27 KB
/
The New Yorker.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
{
"translatorID": "0fba73bf-f113-4d36-810f-2c654fa985fb",
"label": "The New Yorker",
"creator": "Philipp Zumstein and Abe Jellinek",
"target": "^https?://www\\.newyorker\\.com/",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2022-01-20 11:53:09"
}
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2017-2021 Philipp Zumstein and 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('article.article')) {
return "magazineArticle";
}
else if (url.includes('/search/') && getSearchResults(doc, true)) {
return "multiple";
}
return false;
}
function getSearchResults(doc, checkOnly) {
var items = {};
var found = false;
var rows = ZU.xpath(doc, '//li//a[h4]');
for (let row of rows) {
var href = row.href;
var 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) {
return;
}
ZU.processDocuments(Object.keys(items), scrape);
});
}
else {
scrape(doc, url);
}
}
function scrape(doc, url) {
var data = text(doc, 'script[type="application/ld+json"]');
var json = JSON.parse(data);
var translator = Zotero.loadTranslator('web');
// Embedded Metadata
translator.setTranslator('951c027d-74ac-47d4-a107-9c3069ab7b48');
translator.setDocument(doc);
translator.setHandler('itemDone', function (obj, item) {
if (item.creators.length <= 1 && json.author) {
item.creators = [];
// json.author can either be an array, or a object containing an array
if (Array.isArray(json.author)) {
for (let author of json.author) {
item.creators.push(ZU.cleanAuthor(author.name, "author"));
}
}
else if (json.author.name) {
for (let name of json.author.name) {
item.creators.push(ZU.cleanAuthor(name, "author"));
}
}
}
item.publicationTitle = 'The New Yorker';
item.date = ZU.strToISO(json.dateModified || json.datePublished);
item.section = json.articleSection;
item.ISSN = "0028-792X";
item.complete();
});
translator.getTranslatorObject(function (trans) {
trans.itemType = "magazineArticle";
trans.doWeb(doc, url);
});
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "https://www.newyorker.com/magazine/2011/10/31/foreign-campaigns",
"items": [
{
"itemType": "magazineArticle",
"title": "Foreign Campaigns",
"creators": [
{
"firstName": "David",
"lastName": "Remnick",
"creatorType": "author"
}
],
"date": "2011-10-20",
"ISSN": "0028-792X",
"abstractNote": "The Republican professionals know it. The numbers show that more than half the country identifies the economy as the most pressing issue of the campaign; …",
"language": "en-US",
"libraryCatalog": "www.newyorker.com",
"publicationTitle": "The New Yorker",
"url": "https://www.newyorker.com/magazine/2011/10/31/foreign-campaigns",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [
{
"tag": "2012 election"
},
{
"tag": "arab league"
},
{
"tag": "arab spring"
},
{
"tag": "barack obama"
},
{
"tag": "death"
},
{
"tag": "dictators"
},
{
"tag": "elections"
},
{
"tag": "foreign policy"
},
{
"tag": "herman cain"
},
{
"tag": "idi amin"
},
{
"tag": "libya"
},
{
"tag": "middle east"
},
{
"tag": "mitt romney"
},
{
"tag": "muammar qaddafi"
},
{
"tag": "osama bin laden"
},
{
"tag": "politics"
},
{
"tag": "presidential candidates"
},
{
"tag": "republican party"
},
{
"tag": "republicans"
},
{
"tag": "rick perry"
},
{
"tag": "uzbekistan"
}
],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.newyorker.com/news/hendrik-hertzberg/is-that-rick-santorum-on-the-cafeteria-line",
"items": [
{
"itemType": "magazineArticle",
"title": "Is That Rick Santorum on the Cafeteria Line?",
"creators": [
{
"firstName": "Hendrik",
"lastName": "Hertzberg",
"creatorType": "author"
}
],
"date": "2012-02-24",
"ISSN": "0028-792X",
"abstractNote": "I’m a week late with this, but Chris Matthews had a pretty devastating take on Santorum’s “phony theology” attack on Obama’s concern about what …",
"language": "en-US",
"libraryCatalog": "www.newyorker.com",
"publicationTitle": "The New Yorker",
"url": "https://www.newyorker.com/news/hendrik-hertzberg/is-that-rick-santorum-on-the-cafeteria-line",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [
{
"tag": "catholics"
},
{
"tag": "rick santorum"
}
],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.newyorker.com/search/q/labor",
"items": "multiple"
},
{
"type": "web",
"url": "https://www.newyorker.com/magazine/2017/06/19/remembering-the-murder-you-didnt-commit",
"items": [
{
"itemType": "magazineArticle",
"title": "Remembering the Murder You Didn’t Commit",
"creators": [
{
"firstName": "Rachel",
"lastName": "Aviv",
"creatorType": "author"
}
],
"date": "2017-06-12",
"ISSN": "0028-792X",
"abstractNote": "DNA evidence exonerated six convicted killers. So why do some of them recall the crime so clearly?",
"language": "en-US",
"libraryCatalog": "www.newyorker.com",
"publicationTitle": "The New Yorker",
"url": "https://www.newyorker.com/magazine/2017/06/19/remembering-the-murder-you-didnt-commit",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [
{
"tag": "crime"
},
{
"tag": "memory"
},
{
"tag": "murder"
},
{
"tag": "nebraska"
},
{
"tag": "psychology"
}
],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.newyorker.com/culture/culture-desk/what-happens-when-a-bad-tempered-distractible-doofus-runs-an-empire",
"items": [
{
"itemType": "magazineArticle",
"title": "What Happens When a Bad-Tempered, Distractible Doofus Runs an Empire?",
"creators": [
{
"firstName": "Miranda",
"lastName": "Carter",
"creatorType": "author"
}
],
"date": "2018-06-06",
"ISSN": "0028-792X",
"abstractNote": "Donald Trump is reminiscent of Kaiser Wilhelm II, during whose reign the upper echelons of the German government began to unravel into a free-for-all.",
"language": "en-US",
"libraryCatalog": "www.newyorker.com",
"publicationTitle": "The New Yorker",
"url": "https://www.newyorker.com/culture/culture-desk/what-happens-when-a-bad-tempered-distractible-doofus-runs-an-empire",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [
{
"tag": "donald trump"
},
{
"tag": "first world war"
},
{
"tag": "germany"
},
{
"tag": "leadership"
},
{
"tag": "narcissism"
}
],
"notes": [],
"seeAlso": []
}
]
}
]
/** END TEST CASES **/