summaryrefslogtreecommitdiff
path: root/taz.de.js
blob: ee75599e53fb85ca7881688c858f407454c812a6 (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
{
	"translatorID": "d84574f1-e4d6-4337-934f-bf9d01173bf0",
	"label": "taz.de",
	"creator": "Martin Meyerhoff",
	"target": "^https?://(?:www\\.)?taz\\.de",
	"minVersion": "1.0",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcsibv",
	"lastUpdated": "2014-06-11 22:07:16"
}

/*
taz.de Translator
Copyright (C) 2011 Martin Meyerhoff

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/>.
*/

/*
This site is rather heterogenous when it comes to where the author is and all that.
Whenever the script doesn't find something it just returns an empty field.
Try on:
http://www.taz.de/
http://www.taz.de/1/archiv/detailsuche/?tx_hptazsearch_pi1[search_term]=Krise&tx_hptazsearch_pi2[submit_button].x=0&tx_hptazsearch_pi2[submit_button].y=0
http://www.taz.de/1/debatte/kolumnen/artikel/1/haengt-sie-hoeher-1/
*/

function detectWeb(doc, url) {

	var taz_ArticleTitle_XPath = ".//div[@class='sectbody']//h1";
	var taz_Multiple_XPath = '//div[contains(@class, "first_page")]//a[h4]';
	var taz_Search_XPath = '//div[contains(@class, "searchresults")]//a[h4]'   	
	if (doc.evaluate(taz_ArticleTitle_XPath, doc, null, XPathResult.ANY_TYPE, null).iterateNext() ){ 
		//Zotero.debug("newspaperArticle");
		return "newspaperArticle";
	} else if (doc.evaluate(taz_Multiple_XPath, doc, null, XPathResult.ANY_TYPE, null).iterateNext() ){ 
		//Zotero.debug("multiple");
		return "multiple";
	}  else if (doc.evaluate(taz_Search_XPath, doc, null, XPathResult.ANY_TYPE, null).iterateNext() ){ 
		//Zotero.debug("multiple");
		return "multiple";
	}
}

function authorCase(author) { // Turns All-Uppercase-Authors to normally cased Authors
	var words = author.split(/\s|-/);
	var authorFixed = '';
	for (var i in words) {
		words[i] = words[i][0].toUpperCase() + words[i].substr(1).toLowerCase();
		authorFixed = authorFixed + words[i] + ' ';
	}
	return(authorFixed);
}

function scrape(doc, url) {

	var newItem = new Zotero.Item("newspaperArticle");
	newItem.url = doc.location.href; 

	
	// This is for the title!
	
	var title_XPath = '//title';
	var title = doc.evaluate(title_XPath, doc, null, XPathResult.ANY_TYPE, null).iterateNext().textContent;
	newItem.title = title.split(" - ")[0];
	
	// Summary
	var description_XPath = '//meta[contains(@name, "description")]';
	var description = doc.evaluate(description_XPath, doc, null, XPathResult.ANY_TYPE, null).iterateNext().content;
	summary = description.replace(/\sVON.*$/g, '');
	newItem.abstractNote = summary.replace(/KOMMENTAR|KOLUMNE.*$/g, '');
	
	// Authors 
	var author_XPath = "//*[contains(@class, 'sectbody')]//a[contains(@class, 'author')]/h4";
	if (doc.evaluate(author_XPath, doc, null, XPathResult.ANY_TYPE, null).iterateNext() ) {
		var author = doc.evaluate(author_XPath, doc, null, XPathResult.ANY_TYPE, null).iterateNext().textContent;
	} else if (description.match(/^(KOMMENTAR)|(KOLUMNE)\sVON/)){
		Zotero.debug(description);
		author = description.replace(/^(KOMMENTAR)|(KOLUMNE)\sVON\s/, '');
	} else {
		var author = "";
	}
	author = author.replace(/^\s*|\s*$/g, '');
	author = author.replace(".", ". "); // in case a space is missing.
	author = author.replace("VON ", '');
	author = author.replace(/\s+/g, ' ');
	author = author.split(/\sund\s|\su\.\s|\,\s|\&/); 
	for (var i in author) {
		if (author[i].match(/\s/)) { // only names that contain a space!
			author[i] = author[i].replace(/^\s*|\s*$/g, '');
			author[i] = authorCase(author[i]);
			newItem.creators.push(Zotero.Utilities.cleanAuthor(author[i], "author"));
		}
	}
	
	
	
	// Section
	var section_XPath = ".//*[contains(@class, 'selected')]/ul/li[contains(@class, 'selected')]";
	if (doc.evaluate(section_XPath, doc, null, XPathResult.ANY_TYPE, null).iterateNext() ) {
		var section= doc.evaluate(section_XPath, doc, null, XPathResult.ANY_TYPE, null).iterateNext().textContent;
		newItem.section = section;
	}
	
	// Date 
	var date_XPath = '//div[contains(@class, "sectbody")]/span[@class="date"]';
	var date = ZU.xpathText(doc, date_XPath);
	if (date) newItem.date = ZU.trimInternal(date);	

	newItem.attachments.push({url:doc.location.href, title:doc.title, mimeType:"text/html"});
	newItem.publicationTitle = "die tageszeitung"

	newItem.complete();
	
}


function doWeb(doc, url) {
	if (detectWeb(doc, url) == "multiple") {
		var items = new Object();
		var taz_Multiple_XPath = '//div[contains(@class, "first_page")]//a[h4]';
		var taz_Search_XPath = '//div[contains(@class, "searchresults")]//a[h4]'   

		 if (doc.evaluate(taz_Multiple_XPath, doc, null, XPathResult.ANY_TYPE, null).iterateNext() ){ 
			var titles = doc.evaluate(taz_Multiple_XPath, doc, null, XPathResult.ANY_TYPE, null);
		}  else if (doc.evaluate(taz_Search_XPath, doc, null, XPathResult.ANY_TYPE, null).iterateNext() ){ 
			var titles = doc.evaluate(taz_Search_XPath, doc, null, XPathResult.ANY_TYPE, null);
		}

		
		var next_title;
		while (next_title = titles.iterateNext()) {
			items[next_title.href] = next_title.innerHTML;
			items[next_title.href] = items[next_title.href].replace(/(\<h4.*?\>.*?\<\/h4\>\<h3.*?\>)(.*)\<\/h3\>.*/, '$2');
		}
		items = Zotero.selectItems(items, function(items) {
			if(!items) return true;

			var articles = new Array();
			for (var i in items) {
				articles.push(i);
			}
			ZU.processDocuments(articles, function(doc) { scrape(doc, doc.location.href) });
		});
	} else {
		scrape(doc, url);
	}
}	
/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://www.taz.de/!67936/",
		"items": [
			{
				"itemType": "newspaperArticle",
				"creators": [
					{
						"firstName": "Josef",
						"lastName": "Winkler",
						"creatorType": "author"
					}
				],
				"notes": [],
				"tags": [],
				"seeAlso": [],
				"attachments": [
					{
						"title": "Wortklauberei: Hängt sie höher! - taz.de",
						"mimeType": "text/html"
					}
				],
				"url": "http://www.taz.de/!67936/",
				"title": "Wortklauberei: Hängt sie höher!",
				"abstractNote": "Der deutsche Wald als Leistungsträger. Oder: zynisch Kranke auf freiem Fuß! Was ist mit der öffentlichen Sicherheit?",
				"section": "Kolumnen",
				"date": "23. 03. 2011",
				"publicationTitle": "die tageszeitung",
				"libraryCatalog": "taz.de",
				"accessDate": "CURRENT_TIMESTAMP",
				"shortTitle": "Wortklauberei"
			}
		]
	},
	{
		"type": "web",
		"url": "http://taz.de/",
		"items": "multiple"
	},
	{
		"type": "web",
		"url": "http://www.taz.de/!s=bleibt/",
		"items": "multiple"
	}
]
/** END TEST CASES **/