-
Notifications
You must be signed in to change notification settings - Fork 786
/
Copy pathCBC.js
439 lines (416 loc) · 12.7 KB
/
CBC.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
{
"translatorID": "03c4b906-8cb2-4850-a771-697cbd92c2a1",
"label": "CBC",
"creator": "Geoff Banh",
"target": "^https?:\\/\\/www\\.cbc\\.ca/",
"minVersion": "5.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2024-03-14 20:55:10"
}
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2024 Geoff Banh
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) {
let path = new URL(url).pathname;
if (path.includes("/search?") && getSearchResults(doc, true)) {
return 'multiple';
}
else if ((/(news|sports|radio|books|arts|music|life|television|archives)\//.test(path)) && getLD(doc)) {
return "newspaperArticle";
}
else if (path.includes("/player/")) {
return "videoRecording";
}
return false;
}
function getSearchResults(doc, checkOnly) {
var items = {};
var found = false;
// Adjust the CSS Selectors
var rows = doc.querySelectorAll('.card.cardListing');
for (const row of rows) {
var href = row.href;
var title = text(row, 'h3.headline');
if (!href || !title) continue;
if (checkOnly) return true;
found = true;
items[href] = title;
}
return found ? items : false;
}
function getLD(doc) {
let ldScript = text(doc, "script[type='application/ld+json']");
if (ldScript) return JSON.parse(ldScript);
return null;
}
function getMetaContent(doc, attribute, text) {
return attr(doc.head, 'meta[' + attribute + '="' + text + '"]', 'content');
}
async function scrape(doc, url = doc.location.href) {
let translator = Zotero.loadTranslator('web');
// Embedded Metadata
translator.setTranslator('951c027d-74ac-47d4-a107-9c3069ab7b48');
translator.setDocument(doc);
translator.setHandler('itemDone', (_obj, item) => {
item.language = "en-CA";
let ld = getLD(doc);
// only do processing if page has ld
if (ld) {
item.url = getMetaContent(doc, 'property', "og:url");
item.title = ld.headline ? ld.headline : ld.name;
if (item.itemType == "videoRecording") {
item.date = ZU.strToISO(ld.uploadDate);
}
else {
item.date = ZU.strToISO(ld.datePublished);
}
item.abstractNote = ld.description;
item.creators = []; // clear existing authors
// ignore organization authors
if (ld.hasOwnProperty("author") && ld.author[0]['@type'] != "Organization") {
// either single author or multiple comma separated in one entry
if (ld.author.length == 1) {
let authors = ld.author[0].name;
if (authors.includes(',')) {
let authorsList = authors.split(',');
for (const a of authorsList) {
item.creators.push(ZU.cleanAuthor(a, "author"));
}
}
else {
item.creators.push(ZU.cleanAuthor(authors, "author"));
}
}
else {
for (const a of ld.author) {
item.creators.push(ZU.cleanAuthor(a.name, "author"));
}
}
}
let siteName = "CBC";
if (item.itemType != "videoRecording") {
// get department (e.g. News, Sports, Radio)
// remove .ca/ manually, as regex lookbehind doesn't seem to work
let dept = (/\.ca\/\w+(?=\/)/.exec(item.url))[0].replace(".ca/", "");
// capitalize department
dept = dept[0].toUpperCase() + dept.slice(1);
siteName += " " + dept;
}
item.publicationTitle = siteName;
item.libraryCatalog = "CBC.ca";
}
item.complete();
});
let em = await translator.getTranslatorObject();
em.itemType = detectWeb(doc, url);
await em.doWeb(doc, url);
}
async function doWeb(doc, url) {
if (detectWeb(doc, url) == 'multiple') {
let items = await Zotero.selectItems(getSearchResults(doc, false));
if (!items) return;
for (let url of Object.keys(items)) {
await scrape(await requestDocument(url));
}
}
else {
await scrape(doc, url);
}
}
/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "https://www.cbc.ca/news/canada/online-groups-pressuring-youth-self-harm-1.7107885",
"items": [
{
"itemType": "newspaperArticle",
"title": "Violent online groups are pressuring youth into harming themselves, authorities warn",
"creators": [
{
"firstName": "Ioanna",
"lastName": "Roumeliotis",
"creatorType": "author"
},
{
"firstName": "Laurence",
"lastName": "Mathieu-Leger",
"creatorType": "author"
},
{
"firstName": "Andrew",
"lastName": "Culbert",
"creatorType": "author"
}
],
"date": "2024-02-09",
"abstractNote": "Authorities in Canada and the U.S. are warning the public about violent online groups that deliberately target vulnerable minors and pressure them into recording or livestreaming self-harm and producing child sexual abuse material.",
"language": "en-CA",
"libraryCatalog": "CBC.ca",
"publicationTitle": "CBC News",
"url": "https://www.cbc.ca/news/canada/online-groups-pressuring-youth-self-harm-1.7107885",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.cbc.ca/sports/hockey/nhl/elias-pettersson-contract-extension-canucks-nhl-1.7132138",
"items": [
{
"itemType": "newspaperArticle",
"title": "Canucks star forward Elias Pettersson signs 8-year contract extension",
"creators": [
{
"firstName": "Nick",
"lastName": "Wells",
"creatorType": "author"
}
],
"date": "2024-03-02",
"abstractNote": "The Vancouver Canucks and star centre Elias Pettersson have agreed to an eight-year contract extension, the team announced Saturday. He is second in team scoring this season with 75 points on 29 goals and 46 assists.",
"language": "en-CA",
"libraryCatalog": "CBC.ca",
"publicationTitle": "CBC Sports",
"url": "https://www.cbc.ca/sports/hockey/nhl/elias-pettersson-contract-extension-canucks-nhl-1.7132138",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.cbc.ca/player/play/2313671747656",
"items": [
{
"itemType": "videoRecording",
"title": "If you get pulled over by police this month in Regina, expect to take a breathalyzer test",
"creators": [],
"date": "2024-03-02",
"abstractNote": "Everyone who gets pulled over for any reason will get a test. SGI and police are telling people about the plan because not everyone is aware of a 2018 federal law that allows it. CBC's Darla Ponace has more on what you need to know about mandatory roadside alcohol tests.",
"language": "en-CA",
"libraryCatalog": "CBC.ca",
"runningTime": "82.849",
"url": "https://www.cbc.ca/player/play/2313671747656",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.cbc.ca/radio/thecurrent/airport-facial-recognition-biometrics-1.7130000",
"items": [
{
"itemType": "newspaperArticle",
"title": "Airports want to scan your face to make travelling easier. Privacy experts caution it's not ready for takeoff",
"creators": [
{
"firstName": "Jason",
"lastName": "Vermes",
"creatorType": "author"
}
],
"date": "2024-03-03",
"abstractNote": "While airlines and airports say facial recognition can make air travel — an often tedious experience — more efficient and seamless, privacy advocates argue the use of biometric data is fraught and open to abuse.",
"language": "en-CA",
"libraryCatalog": "CBC.ca",
"publicationTitle": "CBC Radio",
"url": "https://www.cbc.ca/radio/thecurrent/airport-facial-recognition-biometrics-1.7130000",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.cbc.ca/radio/frequency",
"detectedItemType": false,
"items": []
},
{
"type": "web",
"url": "https://www.cbc.ca/news/politics/trudeau-meloni-pro-palestinian-protesters-toronto-1.7132378",
"items": [
{
"itemType": "newspaperArticle",
"title": "Trudeau's Toronto event with Italy PM Meloni cancelled due to pro-Palestinian protest",
"creators": [
{
"firstName": "Justin",
"lastName": "Li",
"creatorType": "author"
},
{
"firstName": "Christian",
"lastName": "Paas-Lang",
"creatorType": "author"
}
],
"date": "2024-03-03",
"abstractNote": "A Toronto event where Canadian Prime Minister Justin Trudeau was scheduled to host his Italian counterpart was cancelled on Saturday due to security concerns as hundreds of pro-Palestinian protesters gathered outside the venue, a spokesperson for the Prime Minister's Office said.",
"language": "en-CA",
"libraryCatalog": "CBC.ca",
"publicationTitle": "CBC News",
"url": "https://www.cbc.ca/news/politics/trudeau-meloni-pro-palestinian-protesters-toronto-1.7132378",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.cbc.ca/music/canadian-reggae-songs-record-labels-1.7116717",
"items": [
{
"itemType": "newspaperArticle",
"title": "Canadian reggae's past, present and future",
"creators": [
{
"firstName": "Kelsey",
"lastName": "Adams",
"creatorType": "author"
}
],
"date": "2024-02-23",
"abstractNote": "From the 1st recording on Canadian soil to the newcomers pushing the genre forward today.",
"language": "en-CA",
"libraryCatalog": "CBC.ca",
"publicationTitle": "CBC Music",
"url": "https://www.cbc.ca/music/canadian-reggae-songs-record-labels-1.7116717",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.cbc.ca/life/culture/the-best-card-games-to-play-with-a-standard-deck-1.5836447",
"items": [
{
"itemType": "newspaperArticle",
"title": "The best card games to play with a standard deck",
"creators": [
{
"firstName": "Sebastian",
"lastName": "Yūe",
"creatorType": "author"
}
],
"date": "2020-12-10",
"abstractNote": "This list will have you suggesting card night every chance you get!",
"language": "en-CA",
"libraryCatalog": "CBC.ca",
"publicationTitle": "CBC Life",
"url": "https://www.cbc.ca/life/culture/the-best-card-games-to-play-with-a-standard-deck-1.5836447",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.cbc.ca/television/he-landed-a-sitcom-role-on-his-first-audition-i-was-really-hyped-says-scarborough-teen-1.7101522",
"items": [
{
"itemType": "newspaperArticle",
"title": "He landed a sitcom role on his first audition. 'I was really hyped,' says Scarborough teen",
"creators": [
{
"firstName": "Russ",
"lastName": "Martin",
"creatorType": "author"
}
],
"date": "2024-02-05",
"abstractNote": "The new CBC workplace comedy One More Time follows the manager of a second-hand sporting goods shop, DJ, played by comedian D.J. Demers, and the hijinks of his beloved gang of oddball employees. Among the motley crew is Keeran Devkar, a very green first-time associate played by 15-year-old Seran Sathiyaseelan.",
"language": "en-CA",
"libraryCatalog": "CBC.ca",
"publicationTitle": "CBC Television",
"url": "https://www.cbc.ca/television/he-landed-a-sitcom-role-on-his-first-audition-i-was-really-hyped-says-scarborough-teen-1.7101522",
"attachments": [
{
"title": "Snapshot",
"mimeType": "text/html"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.cbc.ca/search?q=Windows%2011§ion=arts&sortOrder=relevance&media=all",
"items": "multiple"
}
]
/** END TEST CASES **/