summaryrefslogtreecommitdiff
path: root/Library Catalog (Voyager 7).js
blob: cb033cb18adcc607c2a7478ca8c28660cec461ac (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
{
	"translatorID": "a81243b5-a9fd-4921-8441-3142a518fdb7",
	"label": "Library Catalog (Voyager 7)",
	"creator": "Sean Takats",
	"target": "/vwebv/(holdingsInfo|search)",
	"minVersion": "1.0",
	"maxVersion": "",
	"priority": 250,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcsibv",
	"lastUpdated": "2014-08-26 04:12:22"
}

function detectWeb(doc, url) {
	var bibIdRe = new RegExp("bibId=[0-9]+");
	if (bibIdRe.test(url)) {
		return "book";
	}
	//for single search results such as
	//http://catalog.loc.gov/vwebv/search?searchArg=bynum+holy+feast+holy+fast&searchCode=GKEY^*&searchType=0&recCount=100&sk=en_US
	else if(ZU.xpathText(doc, '//div[@class="bibliographicData"]')) return "book";


	var titles = doc.evaluate('//div[@class="resultListTextCell"]//a', doc, null, XPathResult.ANY_TYPE, null);
	if (titles.iterateNext()) {
		return "multiple";
	}
}

function doWeb(doc, url) {
	var bibIdRe = new RegExp("bibId=([0-9]+)");
	var m = bibIdRe.exec(url);
	//for single search results 
	if (detectWeb(doc, url)== "book" && !m){
		var host = url.match("^(https?://[^/]+)/")[0];
		//we get the URL from the print view of the item, which looks like it exists for all Voyager 7 catalogs
		url = host + "vwebv/" + ZU.xpathText(doc, '//div[@class="actionBox"]//a[contains(@href, "printDialog.do")]/@href')
		var m = bibIdRe.exec(url);
	}
	Z.debug(url)
	var hostRegexp = new RegExp("^(https?://[^/]+)/");
	var hMatch = hostRegexp.exec(url);
	var host = hMatch[1];

	var urlPrefix = url.match("https?://[^/]*(/[^/]*/)?/?vwebv/")[1] ? host + url.match("https?://[^/]*(/[^/]*/)?/?vwebv/")[1] + "/vwebv/exportRecord.do?bibId=" : host + "/vwebv/exportRecord.do?bibId=";
	var newUris = new Array();
	if (m) { //single item
		var newURL = urlPrefix + m[1] + "&format=utf-8"
		scrape(doc, newURL)
	} else { //search results
		var items = new Object();
		var titles = doc.evaluate('//div[@class="resultListTextCell"]//a', doc, null, XPathResult.ANY_TYPE, null);
		var title;

		while (title = titles.iterateNext()) {
			var bibId = title.href.match(/bibId=([0-9]+)/)[1];
			// Chrome ignores the order in which properties are added if they are numbers
			// See http://code.google.com/p/v8/issues/detail?id=164
			items["_"+bibId] = title.textContent;
		}

		Zotero.selectItems(items, function (items) {
			if (!items) {
				return true;
			}
			for (var i in items) {
				newUris.push(urlPrefix + i.substr(1) + "&format=utf-8");
			}

			Zotero.Utilities.HTTP.doGet(newUris, function (text) {
				// load translator for MARC
				var marc = Zotero.loadTranslator("import");
				marc.setTranslator("a6ee60df-1ddc-4aae-bb25-45e0537be973");
				marc.setString(text);

				var domain = url.match(/https?:\/\/([^/]+)/);
				marc.setHandler("itemDone", function (obj, item) {
					item.repository = domain[1] + " Library Catalog";
					item.complete();
				});
				marc.translate();
			}, function () {
				Zotero.done()
			})
			Zotero.wait();
		});
	}
}

function scrape(doc, url) {
	Zotero.Utilities.HTTP.doGet(url, function (text) {
		// load translator for MARC
		var marc = Zotero.loadTranslator("import");
		marc.setTranslator("a6ee60df-1ddc-4aae-bb25-45e0537be973");
		marc.setString(text);

		var domain = url.match(/https?:\/\/([^/]+)/);
		marc.setHandler("itemDone", function (obj, item) {
			item.repository = domain[1] + " Library Catalog";
			item.complete();
		});

		marc.translate();
	}, function () {
		Zotero.done()
	})

	Zotero.wait();
}
/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://groucho.lib.rochester.edu/vwebv/search?searchArg=argentina&searchCode=GKEY%5E*&limitTo=none&recCount=50&searchType=1&page.search.search.button=Search",
		"items": "multiple"
	},
	{
		"type": "web",
		"url": "http://groucho.lib.rochester.edu/vwebv/holdingsInfo?searchId=3544&recCount=50&recPointer=1&bibId=78520",
		"items": [
			{
				"itemType": "book",
				"creators": [
					{
						"firstName": "Mildred Anna",
						"lastName": "Phoebus",
						"creatorType": "author"
					},
					{
						"lastName": "United States",
						"fieldMode": true
					}
				],
				"notes": [
					{
						"note": "Supplement to Commerce reports. Published by the Bureau of foreign and domestic commerce. October 29, 1923"
					}
				],
				"tags": [
					"Argentina",
					"Economic conditions"
				],
				"seeAlso": [],
				"attachments": [],
				"title": "Economic development in Argentina since 1921",
				"place": "Washington",
				"publisher": "Govt. print. off",
				"date": "1923",
				"numPages": "14",
				"series": "U. S. Bureau of foreign and domestic commerce (Dept. of commerce) Trade information bulletin",
				"seriesNumber": "no. 156",
				"callNumber": "HF105 .F71tr no.156",
				"libraryCatalog": "groucho.lib.rochester.edu Library Catalog"
			}
		]
	},
	{
		"type": "web",
		"url": "http://groucho.lib.rochester.edu/vwebv/search?searchArg=Economic+development+in+Argentina+since+1921&submit=+&searchCode=TALL&limitTo=none&recCount=50&searchType=1",
		"items": [
			{
				"itemType": "book",
				"creators": [
					{
						"firstName": "Mildred Anna",
						"lastName": "Phoebus",
						"creatorType": "author"
					},
					{
						"lastName": "United States",
						"fieldMode": true
					}
				],
				"notes": [
					{
						"note": "Supplement to Commerce reports. Published by the Bureau of foreign and domestic commerce. October 29, 1923"
					}
				],
				"tags": [
					"Argentina",
					"Economic conditions"
				],
				"seeAlso": [],
				"attachments": [],
				"title": "Economic development in Argentina since 1921",
				"place": "Washington",
				"publisher": "Govt. print. off",
				"date": "1923",
				"numPages": "14",
				"series": "U. S. Bureau of foreign and domestic commerce (Dept. of commerce) Trade information bulletin",
				"seriesNumber": "no. 156",
				"callNumber": "HF105 .F71tr no.156",
				"libraryCatalog": "groucho.lib.rochester.edu Library Catalog"
			}
		]
	}
]
/** END TEST CASES **/