summaryrefslogtreecommitdiff
path: root/Library Catalog (Talis Prism 2).js
blob: 2208a736909ec76603dad9e4c9758eddc1f33248 (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
{
	"translatorID": "dc024bfc-2252-4257-b10e-cb95a0f213aa",
	"label": "Library Catalog (Talis Prism 2)",
	"creator": "Sebastian Karcher",
	"target": "/items(/\\d+|\\?query=)",
	"minVersion": "2.1.9",
	"maxVersion": "",
	"priority": 260,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcsibv",
	"lastUpdated": "2014-08-26 04:11:00"
}

/*
	***** BEGIN LICENSE BLOCK *****
	
	Copyright © 2012 Sebastian Karcher 
	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 (url.match(/\/items\?query=/) && ZU.xpathText(doc, '//div[@id="searchResults"]//h2[@class="title"]/a')!=null ) return "multiple";
	if (url.match(/\/items\/\d+/)) {
		var test = ZU.xpathText(doc, '//link/@type');
		if(test && test.indexOf("application/x-endnote-refer")!=-1)
			return "book";
	}
}
	

function doWeb(doc, url){

	var articles = new Array();
	if(detectWeb(doc, url) == "multiple") { 
		var items = {};
		var titles = doc.evaluate('//div[@id="searchResults"]//h2[@class="title"]/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(referURL(i));
			}
			scrape(articles);
		});
	} else {
		scrape(referURL(url));
	}
}

// help function
function scrape(url){
	Zotero.Utilities.HTTP.doGet(url, function (text) {
	//Z.debug(text)
	//the language text doesn't seem regular Refer, but it's used here
	var language = text.match(/%G.+/)[0];
	//load Refer/BibIX translator - until we can import their JSON or RDF format this looks best.
	var translator = Zotero.loadTranslator("import");
		translator.setTranslator("881f60f2-0802-411a-9228-ce5f47b64c7d");
		translator.setString(text);
		translator.setHandler("itemDone", function(obj, item) {
			if (language){
				item.language= language.replace(/%G\s*/, "");
			}
			item.complete();
		});	
		translator.translate();
	});
}

function referURL(url){
	return url.replace(/\?.+/, "") + ".enw";
}
/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://capitadiscovery.co.uk/cityoflondon/items/169754?outdated=true&query=mahoney&resultsUri=items%3Fquery%3Dmahoney",
		"items": [
			{
				"itemType": "book",
				"creators": [
					{
						"firstName": "John William",
						"lastName": "Mahoney",
						"creatorType": "author"
					}
				],
				"notes": [
					{
						"note": "by Luke Bayard (pseud. i.e. John Mahoney)"
					}
				],
				"tags": [],
				"seeAlso": [],
				"attachments": [],
				"title": "The compact wine guide",
				"place": "(London",
				"publisher": "Wine and Spirit Publications",
				"date": "1969",
				"url": "http://capitadiscovery.co.uk/cityoflondon/items/169754",
				"language": "English",
				"libraryCatalog": "Library Catalog (Talis Prism 2)",
				"accessDate": "CURRENT_TIMESTAMP"
			}
		]
	},
	{
		"type": "web",
		"url": "http://capitadiscovery.co.uk/surrey-ac/items/199580?outdated=true&query=borges&resultsUri=items%3Fquery%3Dborges",
		"items": [
			{
				"itemType": "book",
				"creators": [
					{
						"firstName": "Jorge Luis",
						"lastName": "Borges",
						"creatorType": "author"
					},
					{
						"firstName": "Anthony",
						"lastName": "Kerrigan",
						"creatorType": "author"
					}
				],
				"notes": [
					{
						"note": "Jorge Luis Borges ; edited and with an introduction by Anthony Kerrigan"
					}
				],
				"tags": [
					"Short stories in Spanish Argentinian writers 1910-1945 English texts"
				],
				"seeAlso": [],
				"attachments": [],
				"title": "Fictions",
				"place": "London",
				"publisher": "Calder",
				"date": "1965 1985",
				"ISBN": "9780714540832",
				"url": "http://capitadiscovery.co.uk/surrey-ac/items/199580",
				"language": "English",
				"libraryCatalog": "Library Catalog (Talis Prism 2)"
			}
		]
	},
	{
		"type": "web",
		"url": "http://capitadiscovery.co.uk/surrey-ac/items?query=borges",
		"items": "multiple"
	},
	{
		"type": "web",
		"url": "http://capitadiscovery.co.uk/cityoflondon/items?query=argentina",
		"items": "multiple"
	}
]
/** END TEST CASES **/