forked from zotero/translators
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BBC.js
430 lines (409 loc) · 11.6 KB
/
BBC.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
{
"translatorID": "f4130157-93f7-4493-8f24-a7c85549013d",
"label": "BBC",
"creator": "Philipp Zumstein",
"target": "^https?://(www|news?)\\.bbc\\.(co\\.uk|com)",
"minVersion": "3.0",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2019-06-10 21:51:43"
}
/*
***** BEGIN LICENSE BLOCK *****
Copyright © 2016 Philipp Zumstein
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) {
url = url.replace(/[?#].+/, "");
if (/\d{8}$/.test(url) || /\d{7}\.(stm)$/.test(url)) {
var pageNode = doc.getElementById("page");
if (pageNode) {
// Z.debug(pageNode.className);
if (pageNode.className.includes("media-asset-page") || pageNode.className.includes("vxp-headlines")) {
return "videoRecording";
}
}
return "newspaperArticle";
}
if (url.includes("/newsbeat/article")) {
return "blogPost";
}
if (getSearchResults(doc, true)) {
return "multiple";
}
return false;
}
function getSearchResults(doc, checkOnly) {
var items = {};
var found = false;
var rows = ZU.xpath(doc, '//a[h3]');
// for NewsBeat
if (!rows.length) {
rows = ZU.xpath(doc, '//article/div/h1[@itemprop="headline"]/a');
}
for (let i = 0; i < rows.length; i++) {
var href = rows[i].href;
var title = ZU.trimInternal(rows[i].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;
}
var articles = [];
for (let i in items) {
articles.push(i);
}
ZU.processDocuments(articles, scrape);
});
}
else {
scrape(doc, url);
}
}
function scrape(doc, url) {
url = url.replace(/[?#].+/, "");
var itemType = detectWeb(doc, url);
var translator = Zotero.loadTranslator('web');
// Embedded Metadata
translator.setTranslator('951c027d-74ac-47d4-a107-9c3069ab7b48');
translator.setDocument(doc);
translator.setHandler('itemDone', function (obj, item) {
// add date and time if missing by one of four attempts:
// 1. look at the json-ld data
// 2. calculate it from the data-seconds attribute
// 3. extract it from a nonstandard meta field
// 4. for old pages, get from metadata
var jsonld = ZU.xpathText(doc, '//script[@type="application/ld+json"]');
var data = JSON.parse(jsonld);
// Z.debug(data);
if (data && data.datePublished) {
item.date = data.datePublished;
}
else {
var seconds = ZU.xpathText(doc, '(//div[h1 or h2]//*[contains(@class, "date")]/@data-seconds)[1]');
if (!item.date && seconds) {
// Z.debug(seconds);
var date = new Date(1000 * seconds);
item.date = date.toISOString();
}
else {
item.date = ZU.xpathText(doc, '//meta[@property="rnews:datePublished"]/@content');
if (!item.date) {
item.date = ZU.xpathText(doc, '//p[@class="timestamp"]');
if (!item.date) {
item.date = ZU.xpathText(doc, '//meta[@name="OriginalPublicationDate"]/@content');
}
}
}
}
if (item.date) {
item.date = ZU.strToISO(item.date);
}
// delete wrongly attached creators like
// "firstName": "B. B. C.", "lastName": "News"
item.creators = [];
// add authors from byline__name but only if they
// are real authors and not just part of the webpage title
// like By BBC Trending, By News from Elsewhere... or By Who, What Why
var authorString = ZU.xpathText(doc, '//span[@class="byline__name"]');
var webpageTitle = ZU.xpathText(doc, '//h1');
if (authorString) {
authorString = authorString.replace('By', '').replace('...', '');
let authors = authorString.split('&');
for (let i = 0; i < authors.length; i++) {
if (webpageTitle.toLowerCase().includes(authors[i].trim().toLowerCase())) {
continue;
}
item.creators.push(ZU.cleanAuthor(authors[i], "author"));
}
}
else {
authorString = ZU.xpathText(doc, '//p[@class="byline"]');
var title = ZU.xpathText(doc, '//em[@class="title"]');
if (authorString) {
authorString = authorString.replace(title, '').replace('By', '');
let authors = authorString.split('&');
for (let i = 0; i < authors.length; i++) {
item.creators.push(ZU.cleanAuthor(authors[i], "author"));
}
}
}
if (url.includes("/newsbeat/article")) {
item.blogTitle = "BBC Newsbeat";
}
// description for old BBC pages
if (!item.abstractNote) {
item.abstractNote = ZU.xpathText(doc, '//meta[@name="Description"]/@content');
}
for (let i in item.tags) {
item.tags[i] = item.tags[i].charAt(0).toUpperCase() + item.tags[i].substring(1);
}
if (!item.language || item.language === "en") {
item.language = "en-GB";
}
if (url.substr(-4) == ".stm") {
item.title = ZU.xpathText(doc, '//meta[@name="Headline"]/@content');
}
item.complete();
});
translator.getTranslatorObject(function (trans) {
trans.itemType = itemType;
trans.doWeb(doc, url);
});
}/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "https://www.bbc.com/news/magazine-15335899",
"items": [
{
"itemType": "newspaperArticle",
"title": "Spain's stolen babies",
"creators": [
{
"firstName": "Katya",
"lastName": "Adler",
"creatorType": "author"
}
],
"date": "2011-10-18",
"abstractNote": "Spanish society has been shaken by revelations of the mass trafficking of babies, dating back to the Franco era but continuing until the 1990s involving respected doctors, nuns and priests.",
"language": "en-GB",
"libraryCatalog": "www.bbc.com",
"publicationTitle": "BBC News",
"section": "Magazine",
"url": "https://www.bbc.com/news/magazine-15335899",
"attachments": [
{
"title": "Snapshot"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "http://www.bbc.com/news/world/asia/india",
"items": "multiple"
},
{
"type": "web",
"url": "https://www.bbc.com/news/blogs-news-from-elsewhere-37117404",
"items": [
{
"itemType": "newspaperArticle",
"title": "Fines for not liking boss's Weibo posts",
"creators": [],
"date": "2016-08-18",
"abstractNote": "Company in China punishes employees who don't comment on manager's social media posts.",
"language": "en-GB",
"libraryCatalog": "www.bbc.com",
"publicationTitle": "BBC News",
"section": "News from Elsewhere",
"url": "https://www.bbc.com/news/blogs-news-from-elsewhere-37117404",
"attachments": [
{
"title": "Snapshot"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.bbc.com/news/magazine-36287752",
"items": [
{
"itemType": "newspaperArticle",
"title": "'I found my dad on Facebook'",
"creators": [
{
"firstName": "Abdirahim",
"lastName": "Saeed",
"creatorType": "author"
},
{
"firstName": "Deirdre",
"lastName": "Finnerty",
"creatorType": "author"
}
],
"date": "2016-08-17",
"abstractNote": "How a simple post on social media ended a Russian woman's 40-year search for her father.",
"language": "en-GB",
"libraryCatalog": "www.bbc.com",
"publicationTitle": "BBC News",
"section": "Magazine",
"url": "https://www.bbc.com/news/magazine-36287752",
"attachments": [
{
"title": "Snapshot"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "http://www.bbc.co.uk/search?q=harry+potter",
"items": "multiple"
},
{
"type": "web",
"url": "http://www.bbc.co.uk/newsbeat/article/32129457/will-new-music-streaming-service-tidal-make-the-waves-artists-want",
"items": [
{
"itemType": "blogPost",
"title": "Will new music streaming service Tidal make the waves artists want? - BBC Newsbeat",
"creators": [
{
"firstName": "Chi Chi",
"lastName": "Izundu",
"creatorType": "author"
}
],
"date": "2015-03-31",
"abstractNote": "Exclusive music news, big interviews, entertainment, social media trends and video from the news people at BBC Radio 1 and 1Xtra.",
"blogTitle": "BBC Newsbeat",
"language": "en-GB",
"shortTitle": "Will new music streaming service Tidal make the waves artists want?",
"url": "http://www.bbc.co.uk/newsbeat/article/32129457/will-new-music-streaming-service-tidal-make-the-waves-artists-want",
"attachments": [
{
"title": "Snapshot"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.bbc.com/sport/olympics/37068610",
"items": [
{
"itemType": "newspaperArticle",
"title": "Rio Olympics 2016: Joseph Schooling beats Michael Phelps in 100m butterfly",
"creators": [],
"date": "2016-08-13",
"abstractNote": "Singapore's Joseph Schooling wins his nation's first ever gold medal with victory in the 100m butterfly as Michael Phelps finishes joint second.",
"language": "en-GB",
"libraryCatalog": "www.bbc.com",
"publicationTitle": "BBC Sport",
"section": "Olympics",
"shortTitle": "Rio Olympics 2016",
"url": "https://www.bbc.com/sport/olympics/37068610",
"attachments": [
{
"title": "Snapshot"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "http://news.bbc.co.uk/2/hi/uk_news/politics/2116949.stm",
"items": [
{
"itemType": "newspaperArticle",
"title": "EU must expand, Straw warns",
"creators": [],
"date": "2002-07-08",
"abstractNote": "Debate on reform of the Common Agricultural Policy must not dilute support for EU enlargement, Foreign Secretary Jack Straw will warn.",
"language": "en-GB",
"libraryCatalog": "news.bbc.co.uk",
"url": "http://news.bbc.co.uk/2/hi/uk_news/politics/2116949.stm",
"attachments": [
{
"title": "Snapshot"
}
],
"tags": [
"BBC",
"BBC News",
"British",
"Foreign",
"International",
"News",
"News online",
"Online",
"Service",
"Uk",
"World"
],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "https://www.bbc.com/portuguese/internacional-48562081",
"items": [
{
"itemType": "newspaperArticle",
"title": "Como peixes e até camarões podem ser 'recrutados' como espiões",
"creators": [
{
"firstName": "Emma",
"lastName": "Woollacott",
"creatorType": "author"
}
],
"date": "2019-06-10",
"abstractNote": "Animais são usados há muito tempo para fins militares, mas agora um projeto americano quer saber se as criaturas marinhas também podem agir como sensores.",
"language": "pt",
"libraryCatalog": "www.bbc.com",
"publicationTitle": "BBC News Brasil",
"section": "Internacional",
"url": "https://www.bbc.com/portuguese/internacional-48562081",
"attachments": [
{
"title": "Snapshot"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
}
]
/** END TEST CASES **/