summaryrefslogtreecommitdiff
path: root/Time.com.js
blob: a14ebd4e46957b343fd5edff3ebdb4e27f306c23 (plain)
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
{
	"translatorID": "d9be934c-edb9-490c-a88d-34e2ee106cd7",
	"label": "Time.com",
	"creator": "Michael Berkowitz",
	"target": "^https?://([^/]*\\.)?time.com/",
	"minVersion": "3.0",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcsv",
	"lastUpdated": "2014-10-25 07:16:36"
}

function detectWeb(doc, url) {
	if (url.indexOf('results.html') != -1) {
		return "multiple";
	} else if (url.search(/\/article\/|\d{4}\/\d{2}\/\d{2}\/./) != -1
		|| ZU.xpath(doc, '//section[@class="article-body"]/div[@class="issue-date"]').length
		|| doc.getElementsByClassName('active').length
	) {
		return "magazineArticle";
	}
	// TODO: detect new content on scroll, beacause we should not detect on
	// ads and ToC content
}

function handleAuthors(authors) {
	if (authors && (authors = authors.trim())) {
		var matches = authors.match(/^\s*([^\/]+?)\s*\/\s*(.+?)\s*$/);
		if (matches) {
			if (matches[1] == 'AP' || matches[1] == 'Fortune') {
				authors = matches[2];
			} else {
				authors = matches[1];
			}
		}
		
		//x, y and z
		authors = authors.replace(/^By\s+|\sBy\s+/, "").split(/\s*,\s*|\s+and\s+/i);
		var authArr = [];
		for (var i = 0, n = authors.length; i < n; i++) {
			authArr.push(ZU.cleanAuthor(ZU.capitalizeTitle(authors[i].replace(/\s+@.+/, "")), 'author'));
		}
		if (authArr.length) return authArr;
	}
	return [];
}

function handleKeywords(keywords) {
	if (keywords && (keywords = keywords.trim())) {
		return keywords.split(', ');
	}
	return [];
}

function scrape(doc, url) {
	var article = ZU.xpath(doc, '//section/div[@class="wrapper"]/article[contains(@class, "active")]')[0],
		metaUrl = ZU.xpathText(doc, '/html/head/meta[@property="og:url"]/@content');

	if (article && metaUrl && doc.location.href.indexOf(metaUrl) == -1) {
		var item = new Zotero.Item("magazineArticle");
		item.title = ZU.trimInternal(article.getElementsByClassName('article-title')[0].textContent);
		item.publicationTitle = "Time";
		item.url = url;
		item.ISSN = "0040-718X";
		
		var authors = article.getElementsByClassName('byline');
		if (authors.length) {
			item.creators = handleAuthors(authors
				.map(function(a) { return ZU.trimInternal(a.textContent) })
				.join(', ')
			);
		}

		var keywords = ZU.xpathText(article, 'header//a[@class="topic-tag" or @class="section-tag"]');
		if (keywords) item.tags = handleKeywords(keywords);

		item.abstractNote = ZU.xpathText(doc, '//h2[@class="article-excerpt"]');
		item.date = ZU.xpathText(article, 'header//time[@class="publish-date"]/@datetime');
		
		item.complete();
	} else {
		var translator = Zotero.loadTranslator('web');
		translator.setTranslator('951c027d-74ac-47d4-a107-9c3069ab7b48');
		translator.setDocument(doc);
		
		translator.setHandler('itemDone', function(obj, item) {
			item.itemType = "magazineArticle";
			item.publicationTitle = "Time";
			item.url = url;
			item.ISSN = "0040-718X";
			
			var authors = ZU.xpathText(doc, '//meta[@name="byline"]/@content')
				|| ZU.xpathText(doc, '//span[@class="author vcard"]/a', null, ' and ')
				|| ZU.xpathText(doc, '//span[@class="entry-byline"]')
				|| ZU.xpathText(doc, '//header[@class="article-header"]\
					//ul[@class="article-authors"]//span[@class="byline"]/a');
			if (authors) item.creators = handleAuthors(authors);

			var title = ZU.xpathText(doc, '//h1[@class="entry-title"]');
			if (title) item.title = title;
			
			var keywords = ZU.xpathText(doc, '/html/head/meta[@name="keywords"]/@content')
				|| ZU.xpathText(doc, 'header//a[@class="topic-tag" or @class="section-tag"]');
			if (keywords) item.tags = handleKeywords(keywords);
			
			if (!item.abstractNote) item.abstractNote = ZU.xpathText(doc, '//h2[@class="article-excerpt"]');
			if (!item.date) item.date = ZU.xpathText(doc, '//time[@class="publish-date"]/@datetime');
			
			item.complete();
		});
		
		translator.getTranslatorObject(function(em) {
			em.addCustomFields({
				'date': 'date'
			});
		});

		translator.translate();
	}
}


function doWeb(doc, url) {
	var urls = new Array();
	if (detectWeb(doc, url) == 'multiple') {
		var items = ZU.getItemArray(doc, doc.getElementsByTagName("h3"));
		Zotero.selectItems(items, function(selectedItems) {
			if (!selectedItems) return true;
		
			var urls = new Array();
			for (var i in selectedItems) {
				urls.push(i);
			}
			Z.debug(urls)
			ZU.processDocuments(urls, scrape);
		});
	} else {
		scrape(doc, url);
	}
}/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://time.com/3533556/the-war-on-teacher-tenure/",
		"items": [
			{
				"itemType": "magazineArticle",
				"title": "The War on Teacher Tenure",
				"creators": [
					{
						"firstName": "Haley Sweetland",
						"lastName": "Edwards",
						"creatorType": "author"
					}
				],
				"date": "2014-10-23 05:58:37",
				"ISSN": "0040-718X",
				"abstractNote": "It’s really difficult to fire a bad teacher. A group of Silicon Valley investors wants to change that",
				"accessDate": "CURRENT_TIMESTAMP",
				"libraryCatalog": "time.com",
				"publicationTitle": "Time",
				"url": "http://time.com/3533556/the-war-on-teacher-tenure/",
				"attachments": [
					{
						"title": "Snapshot"
					}
				],
				"tags": [
					"cover story",
					"education",
					"nation",
					"silicon valley",
					"teahers",
					"tech"
				],
				"notes": [],
				"seeAlso": []
			}
		]
	},
	{
		"type": "web",
		"url": "http://time.com/3512672/the-new-ebola-protocols",
		"items": [
			{
				"itemType": "magazineArticle",
				"title": "The New Ebola Protocols",
				"creators": [
					{
						"firstName": "David Von",
						"lastName": "Drehle",
						"creatorType": "author"
					}
				],
				"date": "2014-10-16 06:26:47",
				"ISSN": "0040-718X",
				"abstractNote": "New U.S. cases have health experts rethinking the response and turning to doctors and hospitals that were truly prepared",
				"accessDate": "CURRENT_TIMESTAMP",
				"libraryCatalog": "time.com",
				"publicationTitle": "Time",
				"url": "http://time.com/3512672/the-new-ebola-protocols/",
				"attachments": [
					{
						"document": "[object]",
						"title": "Snapshot"
					}
				],
				"tags": [
					"ebola",
					"medicine"
				],
				"notes": [],
				"seeAlso": []
			}
		]
	},
	{
		"type": "web",
		"url": "http://content.time.com/time/nation/article/0,8599,2099187,00.html",
		"items": [
			{
				"itemType": "magazineArticle",
				"title": "How the U.S. Postal Service Fell Apart",
				"creators": [
					{
						"firstName": "Josh",
						"lastName": "Sanburn",
						"creatorType": "author"
					}
				],
				"date": "Thursday, Nov. 17, 2011",
				"ISSN": "0040-718X",
				"abstractNote": "Battling debilitating congressional mandates and competition online, the USPS is closing thousands of post offices and struggling to find a place in the modern world. But there are people behind the scenes trying to save this American institution",
				"accessDate": "CURRENT_TIMESTAMP",
				"libraryCatalog": "content.time.com",
				"publicationTitle": "Time",
				"url": "http://content.time.com/time/nation/article/0,8599,2099187,00.html",
				"attachments": [
					{
						"title": "Snapshot"
					}
				],
				"tags": [
					"Post Offices",
					"Postal Service",
					"USPS",
					"United States Postal Service"
				],
				"notes": [],
				"seeAlso": []
			}
		]
	},
	{
		"type": "web",
		"url": "http://content.time.com/time/nation/article/0,8599,2108263,00.html",
		"items": [
			{
				"itemType": "magazineArticle",
				"title": "On Scene in Indiana and Kentucky: When the Tornadoes Came",
				"creators": [
					{
						"firstName": "Cary",
						"lastName": "Stemle",
						"creatorType": "author"
					}
				],
				"date": "Sunday, Mar. 04, 2012",
				"ISSN": "0040-718X",
				"abstractNote": "The month of March isn't really the heart of the tornado season but they have come fast and with awesome destruction.",
				"accessDate": "CURRENT_TIMESTAMP",
				"libraryCatalog": "content.time.com",
				"publicationTitle": "Time",
				"shortTitle": "On Scene in Indiana and Kentucky",
				"url": "http://content.time.com/time/nation/article/0,8599,2108263,00.html",
				"attachments": [
					{
						"title": "Snapshot"
					}
				],
				"tags": [
					"destruction",
					"henryville",
					"indiana",
					"kentucky",
					"storm",
					"tornado",
					"weather"
				],
				"notes": [],
				"seeAlso": []
			}
		]
	},
	{
		"type": "web",
		"url": "http://swampland.time.com/2012/03/04/obama-courts-aipac-before-netanyahu-meeting/?iid=sl-main-lede",
		"items": [
			{
				"itemType": "magazineArticle",
				"title": "Obama Courts AIPAC Before Netanyahu Meeting",
				"creators": [
					{
						"firstName": "Jay",
						"lastName": "Newton-Small",
						"creatorType": "author"
					}
				],
				"ISSN": "0040-718X",
				"abstractNote": "Obama rejected any notion that his administration has not been in Israel's corner. “Over the last three years, as President of the United States, I have kept my commitments to the state of Israel.\" The President then ticked off the number of ways he has supported Israel in the last year.",
				"accessDate": "CURRENT_TIMESTAMP",
				"libraryCatalog": "swampland.time.com",
				"publicationTitle": "Time",
				"url": "http://swampland.time.com/2012/03/04/obama-courts-aipac-before-netanyahu-meeting/?iid=sl-main-lede",
				"attachments": [
					{
						"title": "Snapshot"
					}
				],
				"tags": [
					"aipac",
					"barack obama",
					"bibi",
					"iran",
					"israel",
					"mahmoud ahamadinejad",
					"netanyahu",
					"obama",
					"speech",
					"washington"
				],
				"notes": [],
				"seeAlso": []
			}
		]
	},
	{
		"type": "web",
		"url": "http://business.time.com/2012/03/02/struggling-to-stay-afloat-number-of-underwater-homeowners-keeps-on-rising/?iid=pf-main-lede/",
		"items": [
			{
				"itemType": "magazineArticle",
				"title": "Struggling to Stay Afloat: Number of Underwater Homeowners Keeps on Rising",
				"creators": [
					{
						"firstName": "Brad",
						"lastName": "Tuttle",
						"creatorType": "author"
					}
				],
				"ISSN": "0040-718X",
				"abstractNote": "Despite signs that some housing markets are improving, the overall trend is for home prices (and values) to keep dropping—and dropping. As values shrink, more and more homeowners find themselves underwater, the unfortunate scenario in which one owes more on the mortgage than the home is worth.",
				"accessDate": "CURRENT_TIMESTAMP",
				"libraryCatalog": "business.time.com",
				"publicationTitle": "Time",
				"shortTitle": "Struggling to Stay Afloat",
				"url": "http://business.time.com/2012/03/02/struggling-to-stay-afloat-number-of-underwater-homeowners-keeps-on-rising/?iid=pf-main-lede/",
				"attachments": [
					{
						"title": "Snapshot"
					}
				],
				"tags": [
					"arizona",
					"baltimore",
					"california",
					"california real estate",
					"dallas",
					"economics & policy",
					"florida",
					"florida real estate",
					"georgia",
					"mortgages",
					"nevada",
					"personal finance",
					"real estate & homes",
					"real estate markets",
					"sunbelt",
					"the economy",
					"underwater",
					"upside-down"
				],
				"notes": [],
				"seeAlso": []
			}
		]
	},
	{
		"type": "web",
		"url": "http://search.time.com/results.html?Ntt=labor&N=0&Nty=1&p=0&cmd=tags",
		"items": "multiple"
	}
]
/** END TEST CASES **/