summaryrefslogtreecommitdiff
path: root/The Telegraph.js
blob: c1357dbb88652bd8d11ec86c14aedc4c8d63eafe (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
{
	"translatorID": "40b9ca22-8df4-4f3b-9cb6-8f9b55486d30",
	"label": "The Telegraph",
	"creator": "Reino Ruusu",
	"target": "^https?://[^/]*telegraph\\.co\\.uk/",
	"minVersion": "1.0.0b4.r5",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcsib",
	"lastUpdated": "2013-12-12 13:59:44"
}

function createExcludes(url, excludeArr) {
	var exclude = new Array();
	for(var i=0, n=excludeArr.length; i<n; i++) {
		if(url.indexOf(excludeArr[i]) == -1) exclude.push(excludeArr[i]);
	}
	return exclude;
}

function getLinks(doc, url) {
	//need to do some SOP checking here
	var origin = doc.location.protocol + '//' + doc.location.host +
				( (doc.location.port) ? ':' + doc.location.port : '' ) + '/';

	//some urls redirect to different subdomains
	var exclude = createExcludes(url, ['/fashion/']);

	var sopCheck = '[starts-with(normalize-space(@href),"' + origin +
		'") or starts-with(normalize-space(@href),"/")]' + 
		( (exclude.length) ? '[not(contains(@href,"' +
		exclude.join('") or contains(@href,"') + '"))]' : '' );

	var links = ZU.xpath(doc, '//div[starts-with(@class,"summaryMedium")]\
				/div[normalize-space(@class)="summary"][./p/a[contains(text(),"|")]]\
				/a' + sopCheck);
	if(!links.length) {
		links = ZU.xpath(doc, '//div[starts-with(@class,"summaryMedium") or starts-with(@class,"summaryBig")]\
				/div[normalize-space(@class)="summary" or @class="summary headlineOnly"]\
				//h3[not(@class)]/a' + sopCheck);
	}

	return links;
}

function detectWeb(doc, url) {
	if (ZU.xpath(doc, '//meta[@name = "tmgads.articleid"]').length) {
		if(ZU.xpathText(doc, '//meta[@name="tmgads.channel"]/@content') == 'blogs'){
			return 'blogPost';
		} else {
			return 'newspaperArticle';
		}
	} else if (url.indexOf('queryText') != -1) {
		if(getLinks(doc, url).length) {
			return 'multiple';
		}
	} else if(getLinks(doc, url).length) {
		return "multiple";
	}
}

function scrape(doc, url) {
	var translator = Zotero.loadTranslator('web');
	translator.setTranslator('951c027d-74ac-47d4-a107-9c3069ab7b48');
	translator.setDocument(doc);

	translator.setHandler('itemDone', function(obj, item) {
		//set proper item type
		item.itemType = detectWeb(doc, url);

		//fix title
		item.title = item.title.replace(/\s*[-–][^-–]*Telegraph[^-]*$/, '');
	
		//fix newlines in abstract
		item.abstractNote = ZU.trimInternal(item.abstractNote);

		//keywords
		var keywords = ZU.xpathText(doc, '//meta[@name="keywords"]/@content');
		if(keywords && keywords.trim()) {
			item.tags = keywords.split(/,\s*/);
		}
		item.creators = [];
		//authors
		var authors	 = ZU.xpathText(doc, '//meta[@name="GSAAuthor"]/@content') ||
					ZU.xpathText(doc, '//meta[@name="DCSext.author"]/@content');
		if(authors) {
			item.creators.push(ZU.cleanAuthor(authors, 'author'));
		}

		item.publisher = 'Telegraph Media Group Limited';

		item.complete();
	});

	//some of the links redirect to a different subdomain, so we need
	//to skip those due to mozilla's Same Origin Policy
	//most of them should already be filtered out,
	//we'll throw an error so users can report this
	//e.g. telegraph.co.uk/fashion will redirect to fashion.telegraph.co.uk
	try{
		translator.getTranslatorObject(function(em) {
			em.addCustomFields({
				'title': 'title',
				'description': 'abstractNote',
				'WT.cg_s': 'section',
				'GSAGenre': 'section',
				'DCSext.Category': 'section',
				'DCSext.articleId': 'callNumber',
				'article-id': 'callNumber',
				'tmgads.articleid': 'callNumber',
				'last-modified': 'date',
				'DCSext.articleFirstPublished' : 'date'
			});
	
			em.doWeb(doc, url);
		});
	} catch(e) {
		Zotero.debug('Zotero cannot access page at ' + url + 
					', because it resides on a different domain.');
	}
}

function doWeb(doc, url) {
	if(detectWeb(doc, url) == 'multiple') {
		var links = getLinks(doc, url);

		var items = new Object();
		for(var i=0, n=links.length; i<n; i++) {
			items[links[i].href] = ZU.trimInternal(links[i].textContent);
		}

		Zotero.selectItems(items, function(selectedItems) {
			if(!selectedItems) return true;

			var urls = new Array();
			for(var i in selectedItems) {
				urls.push(i);
			}

			ZU.processDocuments(urls, scrape, function() {
			});
		});
	} else {
		scrape(doc, url);
	}
}
/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://www.telegraph.co.uk/news/worldnews/asia/china/8888909/China-Google-Earth-spots-huge-unidentified-structures-in-Gobi-desert.html",
		"items": [
			{
				"itemType": "newspaperArticle",
				"creators": [
					{
						"firstName": "Malcolm",
						"lastName": "Moore",
						"creatorType": "author"
					}
				],
				"notes": [],
				"tags": [
					"China",
					"Asia",
					"World News",
					"News"
				],
				"seeAlso": [],
				"attachments": [
					{
						"title": "Snapshot"
					}
				],
				"title": "China: Google Earth spots huge, unidentified structures in Gobi desert",
				"publicationTitle": "Telegraph.co.uk",
				"date": "2011-11-14 13:50",
				"url": "http://www.telegraph.co.uk/news/worldnews/asia/china/8888909/China-Google-Earth-spots-huge-unidentified-structures-in-Gobi-desert.html",
				"abstractNote": "Vast, unidentified, structures have been spotted by satellites in the barren Gobi desert, raising questions about what China might be building in a region it uses for its military, space and nuclear programmes.",
				"libraryCatalog": "www.telegraph.co.uk",
				"accessDate": "CURRENT_TIMESTAMP",
				"section": "worldnews",
				"callNumber": "8888909",
				"publisher": "Telegraph Media Group Limited",
				"shortTitle": "China"
			}
		]
	},
	{
		"type": "web",
		"url": "http://blogs.telegraph.co.uk/news/cristinaodone/100141152/putin-wins-the-russian-election-but-it-wont-be-long-before-hes-in-trouble/",
		"items": [
			{
				"itemType": "blogPost",
				"creators": [
					{
						"firstName": "Cristina",
						"lastName": "Odone",
						"creatorType": "author"
					}
				],
				"notes": [],
				"tags": [
					"Russia",
					"Vladimir Putin",
					"Politics",
					"World"
				],
				"seeAlso": [],
				"attachments": [
					{
						"title": "Snapshot"
					}
				],
				"title": "Putin 'wins' the Russian election. But it won't be long before he's in trouble",
				"publicationTitle": "News - Telegraph Blogs",
				"url": "http://blogs.telegraph.co.uk/news/cristinaodone/100141152/putin-wins-the-russian-election-but-it-wont-be-long-before-hes-in-trouble/",
				"abstractNote": "Vladimir Putin looks set to win the Russian elections – no surprise there, then. Few, even in Russia, believe that today's election is anything bu",
				"libraryCatalog": "blogs.telegraph.co.uk",
				"accessDate": "CURRENT_TIMESTAMP",
				"section": "Blogs",
				"callNumber": "100141152",
				"date": "2012-03-04 18:32:08",
				"publisher": "Telegraph Media Group Limited"
			}
		]
	},
	{
		"type": "web",
		"url": "http://www.telegraph.co.uk/search/?queryText=obama&Search=",
		"items": "multiple"
	},
	{
		"type": "web",
		"url": "http://www.telegraph.co.uk/",
		"items": "multiple"
	}
]
/** END TEST CASES **/