summaryrefslogtreecommitdiff
path: root/Reuters.js
blob: b699f8785d615690ba252214cc85f58dc6ab0605 (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
{
	"translatorID": "83979786-44af-494a-9ddb-46654e0486ef",
	"label": "Reuters",
	"creator": "Avram Lyon, Michael Berkowitz, Sebastian Karcher",
	"target": "^https?://(www|blogs)?\\.reuters\\.com/",
	"minVersion": "2.1.9",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcsibv",
	"lastUpdated": "2012-07-19 06:10:54"
}

/*
   Reuters Translator
   Copyright (C) 2011 Avram Lyon, ajlyon@gmail.com, Sebastian Karcher

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

function detectWeb(doc, url) {
	if (url.match(/^https?:\/\/(www\.)?reuters\.com\/article/)) {
		return "newspaperArticle";
	} else if (url.match(/^https?:\/\/blogs\.reuters\.com/)) {
	  return "blogPost";
	} else if (url.match(/search\?/)) {
	  return "multiple";
	}
}

function doWeb(doc, url) {
	var articles = new Array();
	if (detectWeb(doc, url) == "multiple") {
		var items = {};
		var titles = doc.evaluate('//li[@class="searchHeadline"]/a', doc, null, XPathResult.ANY_TYPE, null);
		var title;
		while (title = titles.iterateNext()) {
			items[title.href] = title.textContent;
		}

		Zotero.selectItems(items, function (items) {
			if (!items) {
				return true;
			}
			for (var i in items) {
				articles.push(i);
			}
			Zotero.Utilities.processDocuments(articles, scrape, function () {
				Zotero.done();
			});
			Zotero.wait();
		});
	} else {
		scrape(doc, url);
	}
}

function scrape(doc, url) {
	if (detectWeb(doc, url) == "newspaperArticle") {
		var item = new Zotero.Item("newspaperArticle");

		item.date = doc.evaluate('//meta[@name="REVISION_DATE"]', doc, null, XPathResult.ANY_TYPE, null).iterateNext().content;
		var byline = ZU.xpathText(doc, '//div[@id="articleInfo"]//p[@class="byline"]');
		if (byline) {
			var authors = byline.substr(3).split(/and |,/);
			for each(var aut in authors) {
				item.creators.push(authorFix(aut));
			}
		}
		item.publicationTitle = "Reuters";
	}
	if (detectWeb(doc, url) == "blogPost") {
		var item = new Zotero.Item("blogPost");

		item.date = ZU.xpathText(doc, '//div[@id="single"]/div[@class="timestamp"]');
		var byline = ZU.xpathText(doc, '//div[@class="author"]');
		if (byline) {
			var authors = byline.split(/and |,/);
			for each(var aut in authors) {
				item.creators.push(authorFix(aut));
			}
		}

		var blogtitle = ZU.xpathText(doc, '//h1');
		if (blogtitle) item.publicationTitle = "Reuters Blogs - " + blogtitle;
		else item.publicationTitle = "Reuters Blogs";
	}

	//general fields
	if(item) {
		item.title = ZU.xpathText(doc, '//meta[@property="og:title"]/@content');

		item.place = ZU.xpathText(doc, '//div[@id="articleInfo"]//span[@class="location"]');
		if (item.place) {
			if (item.place == item.place.toUpperCase()) item.place = Zotero.Utilities.capitalizeTitle(item.place.toLowerCase(), true);
		}

		item.url = ZU.xpathText(doc, '//link[@rel="canonical"]/@href');
		item.abstractNote = ZU.xpathText(doc, '//meta[@name="description"]/@content');

		var tags = ZU.xpathText(doc, '//meta[@name="keywords"]/@content');
		if(tags)
			item.tags = tags.trim().split(/\s*,\s*/);

		item.attachments.push({title: 'Snapshot', document: doc});

		item.complete();
	}
}

function authorFix(author) {
	// Sometimes we have "By Author"
	if (author.substr(0, 3).toLowerCase() == "by ") {
		author = author.substr(3);
	}
	var cleaned = Zotero.Utilities.cleanAuthor(author, "author");
	// If we have only one name, set the author to one-name mode
	if (cleaned.firstName == "") {
		cleaned["fieldMode"] = true;
	} else {
		// We can check for all lower-case and capitalize if necessary
		// All-uppercase is handled by cleanAuthor
		cleaned.firstName = (cleaned.firstName == cleaned.firstName.toLowerCase()) ? Zotero.Utilities.capitalizeTitle(cleaned.firstName, true) : cleaned.firstName;
		cleaned.lastName = (cleaned.lastName == cleaned.lastName.toLowerCase()) ? Zotero.Utilities.capitalizeTitle(cleaned.lastName, true) : cleaned.lastName;
	}
	return cleaned;
}

/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://www.reuters.com/article/2011/11/14/us-eurozone-idUSTRE7AC15K20111114",
		"items": [
			{
				"itemType": "newspaperArticle",
				"creators": [
					{
						"firstName": "James",
						"lastName": "Mackenzie",
						"creatorType": "author"
					},
					{
						"firstName": "Barry",
						"lastName": "Moody",
						"creatorType": "author"
					}
				],
				"notes": [],
				"tags": [
					"Germany",
					"Greece",
					"Italy",
					"Germany",
					"Greece",
					"Italy",
					"Germany",
					"Angela Merkel",
					"Antonis Samaras",
					"George Papandreou",
					"Harry Papachristou",
					"Jens Weidmann",
					"Lucas Papademos",
					"Mario Monti",
					"Olli Rehn",
					"Philip Pullella",
					"Silvio Berlusconi",
					"Angela Merkel",
					"Antonis Samaras",
					"George Papandreou",
					"Giorgio Napolitano",
					"Harry Papachristou",
					"Jack Ablin",
					"Jens Weidmann",
					"Kai Pfaffenbach",
					"Lucas Papademos",
					"Mario Monti",
					"Olli Rehn",
					"Philip Pullella",
					"Silvio Berlusconi",
					"Angela Merkel",
					"Kai Pfaffenbach"
				],
				"seeAlso": [],
				"attachments": [
					{
						"title": "Snapshot"
					}
				],
				"date": "Mon Nov 14 21:16:28 UTC 2011",
				"publicationTitle": "Reuters",
				"title": "Europe could be in worst hour since WW2: Merkel",
				"place": "Rome",
				"url": "http://www.reuters.com/article/2011/11/14/us-eurozone-idUSTRE7AC15K20111114",
				"abstractNote": "ROME (Reuters) - Prime Minister-designate Mario Monti meets the leaders of Italy's biggest two parties on Tuesday to discuss the many sacrifices needed to reverse a collapse in market confidence that is",
				"libraryCatalog": "Reuters",
				"accessDate": "CURRENT_TIMESTAMP",
				"shortTitle": "Europe could be in worst hour since WW2"
			}
		]
	},
	{
		"type": "web",
		"url": "http://blogs.reuters.com/lawrencesummers/2012/03/26/its-too-soon-to-return-to-normal-policies/",
		"items": [
			{
				"itemType": "blogPost",
				"creators": [
					{
						"firstName": "Lawrence",
						"lastName": "Summers",
						"creatorType": "author"
					}
				],
				"notes": [],
				"tags": [
					"deficit",
					"fiscal policy",
					"housing",
					"recovery",
					"unemployment"
				],
				"seeAlso": [],
				"attachments": [
					{
						"title": "Snapshot"
					}
				],
				"title": "It’s too soon to return to normal policies",
				"abstractNote": "After years when the risks to the consensus modest-growth forecast were to the downside, they are now very much two-sided.",
				"url": "http://blogs.reuters.com/lawrencesummers/2012/03/26/its-too-soon-to-return-to-normal-policies/",
				"publicationTitle": "Reuters Blogs - Lawrence Summers",
				"libraryCatalog": "Reuters",
				"accessDate": "CURRENT_TIMESTAMP"
			}
		]
	},
	{
		"type": "web",
		"url": "http://www.reuters.com/search?blob=europe",
		"items": "multiple"
	}
]
/** END TEST CASES **/