-
Notifications
You must be signed in to change notification settings - Fork 1
/
BBC.js
301 lines (280 loc) · 8.13 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
{
"translatorID": "f4130157-93f7-4493-8f24-a7c85549013d",
"label": "BBC",
"creator": "Philipp Zumstein",
"target": "^https?://(?:www|news?)\\.bbc\\.(co\\.uk|com)",
"minVersion": "2.1",
"maxVersion": "",
"priority": 100,
"inRepository": true,
"translatorType": 4,
"browserSupport": "gcsibv",
"lastUpdated": "2016-08-20 19:58:56"
}
/*
***** 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) {
if (/\d{8}$/.test(url)) {
var pageNode = doc.getElementById("page");;
if (pageNode) {
//Z.debug(pageNode.className);
if (pageNode.className.indexOf("media-asset-page")>-1 || pageNode.className.indexOf("vxp-headlines")>-1) {
return "videoRecording";
}
}
return "newspaperArticle";
}
if (getSearchResults(doc, true)) {
return "multiple";
}
}
function getSearchResults(doc, checkOnly) {
var items = {};
var found = false;
var rows = ZU.xpath(doc, '//a[h3[@class="title-link__title"]]');
for (var 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 true;
}
var articles = [];
for (var i in items) {
articles.push(i);
}
ZU.processDocuments(articles, scrape);
});
} else {
scrape(doc, url);
}
}
function scrape(doc, url) {
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 three attempts:
// 1. look at the json-ld data
// 2. calculate it from the data-seconds attribute
// 3. extract it from a nonstandard meta field
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');
}
}
//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('...', '');
var authors = authorString.split('&');
for (var i=0; i<authors.length; i++) {
if (webpageTitle.toLowerCase().indexOf(authors[i].trim().toLowerCase())>-1) {
continue;
}
item.creators.push(ZU.cleanAuthor(authors[i], "author"));
}
}
item.language = "en-GB";
item.complete();
});
translator.getTranslatorObject(function(trans) {
trans.itemType = itemType;
trans.doWeb(doc, url);
});
}/** BEGIN TEST CASES **/
var testCases = [
{
"type": "web",
"url": "http://www.bbc.com/news/magazine-15335899",
"items": [
{
"itemType": "newspaperArticle",
"title": "Spain's stolen babies and the families who lived a lie",
"creators": [
{
"firstName": "Katya",
"lastName": "Adler",
"creatorType": "author"
}
],
"date": "2011-10-18T10:31:45+01:00",
"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": "http://www.bbc.com/news/magazine-15335899",
"attachments": [
{
"title": "Snapshot"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "http://www.bbc.com/news/world-africa-37066738",
"items": [
{
"itemType": "videoRecording",
"title": "Drone photography captures South Africa inequality",
"creators": [],
"date": "2016-08-12T23:57:42.000Z",
"abstractNote": "Photographer Johnny Miller has been documenting the disparity between South Africa's rich and poor using a drone.",
"language": "en-GB",
"libraryCatalog": "www.bbc.com",
"url": "http://www.bbc.com/news/world-africa-37066738",
"attachments": [
{
"title": "Snapshot"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "http://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 1:43:21",
"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": "http://www.bbc.com/sport/olympics/37068610",
"attachments": [
{
"title": "Snapshot"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "http://www.bbc.com/news/world/asia/india",
"items": "multiple"
},
{
"type": "web",
"url": "http://www.bbc.com/news/blogs-news-from-elsewhere-37117404",
"items": [
{
"itemType": "newspaperArticle",
"title": "China staff fined for not liking boss's Weibo posts",
"creators": [],
"date": "2016-08-18T12:55:52+01:00",
"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": "http://www.bbc.com/news/blogs-news-from-elsewhere-37117404",
"attachments": [
{
"title": "Snapshot"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
},
{
"type": "web",
"url": "http://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-17T00:49:43+01:00",
"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": "http://www.bbc.com/news/magazine-36287752",
"attachments": [
{
"title": "Snapshot"
}
],
"tags": [],
"notes": [],
"seeAlso": []
}
]
}
]
/** END TEST CASES **/