summaryrefslogtreecommitdiff
path: root/Glenbow Library.js
blob: 93fd03b84c4ef611b17b9e5d60cead14b241d1fb (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
257
258
259
260
261
262
263
264
265
266
267
{
	"translatorID": "330f283f-12e9-4421-aa59-e17ec5f4aa37",
	"label": "Glenbow Library",
	"creator": "Adam Crymble",
	"target": "^https?://ww2\\.glenbow\\.org/",
	"minVersion": "1.0.0b4.r5",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcsibv",
	"lastUpdated": "2012-03-12 01:00:30"
}

function detectWeb(doc, url) {

	if (doc.title.match("Library Main Catalogue Search Results") && doc.location.href.match("GET_RECORD")) {
			return "book";
	} else if
		(doc.title.match("Library Map Collection Search Results") && doc.location.href.match("GET_RECORD")) {
			return "map";

	} else if
		(doc.title.match("Library Main Catalogue Search Results") && !(doc.location.href.match("GET_RECORD"))) {
			return "multiple";
	} else if
		(doc.title.match("Map Collection Search Results") && !(doc.location.href.match("GET_RECORD"))) {
			return "multiple";
	}
}

//Translator for the Glenbow Museum Collection. Code by Adam Crymble
//Only works for Library Main Catalogue and Map Collection. The other categories do not have stable URLs for individual entries.


function associateContent (newItem, dataTags, field, zoteroField) {
	if (dataTags[field]) {
		newItem[zoteroField] = dataTags[field];
	}
}

function scrape (doc, url) {
	var namespace = doc.documentElement.namespaceURI;
	var nsResolver = namespace ? function(prefix) {
	} : null;

	var fieldTitle = new Array();
	var tagsContent = new Array();

	if (detectWeb(doc, url) == "book") {

		newItem = new Zotero.Item("book");
		authorType= "author";

	} else if (detectWeb(doc, url) == "map") {

		newItem = new Zotero.Item("map");
		authorType= "cartographer";
	}

		var dataTags= new Object();
		var authorType;
		var organizeName;

	if (doc.evaluate('//tr/td/p', doc, nsResolver, XPathResult.ANY_TYPE, null)) {
		var xPathContent = doc.evaluate('//tr/td/p', doc, nsResolver, XPathResult.ANY_TYPE, null);
		var xPathCount = doc.evaluate('count (//tr/td/p)', doc, nsResolver, XPathResult.ANY_TYPE, null);

			for (var i = 0; i < xPathCount.numberValue; i++) {

				fieldTitle= xPathContent.iterateNext().textContent;

				var separate = fieldTitle.indexOf(":");
				var fieldTitle1 = fieldTitle.substr(0, separate);
				fieldTitle1 = fieldTitle1.replace(/\s+/g, '');

				var fieldContent = fieldTitle.substr(separate + 2);

				dataTags[fieldTitle1] = (fieldContent);

			}


		//names start
			if (dataTags["Names"]) {

			//if there are multiple authors:
				if (dataTags["Names"].match("\n")) {
					var multipleNames = dataTags["Names"].split("\n");

					for (j = 0; j < multipleNames.length; j++) {
						if (detectWeb(doc, url) == "book") {
							multipleNames[j] = multipleNames[j].substr(3);

						} else if (detectWeb(doc, url) == "map") {
							multipleNames[j] = multipleNames[j];
						}

						if (multipleNames[j].match(/\,/)) {

							organizeName = multipleNames[j].split(",");
							organizeName = (organizeName[1] + (" ") + organizeName[0]);
							newItem.creators.push(Zotero.Utilities.cleanAuthor(organizeName, authorType));

						} else {
							newItem.creators.push({lastName: multipleNames[j], creatorType: authorType});
						}

					}

			//if there is 1 human author
				} else if (dataTags["Names"].match(/\,/)) {
					if (detectWeb(doc, url) == "book") {
						var organizeName = dataTags["Names"].substr(3).split(",");

					} else if (detectWeb(doc, url) == "map") {
						var organizeName = dataTags["Names"].split(",");
					}

					organizeName = (organizeName[1] + (" ") + organizeName[0]);
					newItem.creators.push(Zotero.Utilities.cleanAuthor(organizeName,authorType));

			//if there is 1 corporate author
				} else {
					if (detectWeb(doc, url) == "book") {
						newItem.creators.push({lastName: dataTags["Names"].substr(3), creatorType: authorType});

					} else if (detectWeb(doc, url) == "map") {
						newItem.creators.push({lastName: dataTags["Names"], creatorType: authorType});

					}
				}
			}

		//tags start
			if (dataTags["Subjects"]) {
				if (dataTags["Subjects"].match("\n")) {
					var multipleSubjects= dataTags["Subjects"].split("\n");

					for (j = 0; j < multipleSubjects.length; j++) {
						multipleSubjects[j] = multipleSubjects[j].substr(3);
						tagsContent.push(Zotero.Utilities.cleanTags(multipleSubjects[j]));
					}
				} else {
					dataTags["Subjects"] = dataTags["Subjects"].substr(3);
					tagsContent.push(Zotero.Utilities.cleanTags(dataTags["Subjects"]));
				}

				for (var y = 0; y < tagsContent.length; y++) {
					newItem.tags[y] = tagsContent[y];
				}
			}

		//book publisher info start
			if (dataTags["PublishingInformation"]) {
					dataTags["PublishingInformation"] = dataTags["PublishingInformation"].replace(/\[|\]*/g, '');

					var pubLoc= dataTags["PublishingInformation"].split(":");
					if (pubLoc[1]) {
						dataTags["Place"] = pubLoc[0];

						var pubAndDate = pubLoc[1].split(",");
						dataTags["Publisher"] = pubAndDate[0];
						dataTags["Date"] = pubAndDate[1];
					} else {
						associateContent (newItem, dataTags, "PublishingInformation", "date");
					}
			}

		//accession number start
			if (dataTags["Accessionnumber"]) {

				dataTags["Accessionnumber"] = ("Accession number: " + dataTags["Accessionnumber"]);
			}

			if (dataTags["CallNumber"]) {
				if (dataTags["CallNumber"] == ' ') {
					dataTags["CallNumber"] = "None";
					Zotero.debug(dataTags["CallNumber"]);
				}
			}
		}
			associateContent (newItem, dataTags, "CallNumber", "callNumber");
			associateContent (newItem, dataTags, "Title", "title");
			associateContent (newItem, dataTags, "Place", "place");
			associateContent (newItem, dataTags, "Publisher", "publisher");
			associateContent (newItem, dataTags, "Date", "date");
			associateContent (newItem, dataTags, "Description", "pages");
			associateContent (newItem, dataTags, "Edition", "edition");
			associateContent (newItem, dataTags, "Notes", "abstractNote");
			associateContent (newItem, dataTags, "Accessionnumber", "extra");
			associateContent (newItem, dataTags, "Scale", "scale");

			newItem.url = doc.location.href;
			if (!newItem.title) newItem.title = "New Search Terms Suggested"
			newItem.complete();
}

function doWeb(doc, url) {
	var namespace = doc.documentElement.namespaceURI;
	var nsResolver = namespace ? function(prefix) {
		if (prefix == 'x') return namespace; else return null;
	} : null;

	var articles = new Array();
	var dataTags = new Object();
	var titleList = new Array();
	var uris = new Array();
	var next_title= new Array();

	if (detectWeb(doc, url) == "multiple") {

	//checks multiple entries for a link to a single entry page.
		if (doc.evaluate('//td/div[@class="floatRight"]/a', doc, nsResolver, XPathResult.ANY_TYPE, null)) {

			var items = new Object();
			var titles = doc.evaluate('//td/p', doc, nsResolver, XPathResult.ANY_TYPE, null);

			var xPathMultiCount = doc.evaluate('count (//td/p)', doc, nsResolver, XPathResult.ANY_TYPE, null);


				for (var i = 0; i < xPathMultiCount.numberValue; i++) {

					articles= titles.iterateNext().textContent;

					var separateMulti = articles.indexOf(":");
					var articles1 = articles.substr(0, separateMulti);
					articles1 = articles1.replace(/\s+/g, '');

					var multiContent = articles.substr(separateMulti + 2);

					dataTags[articles1] = (multiContent);

					if (articles1 == "Title") {
						titleList.push(dataTags["Title"]);
					}
					if (articles1 == "See") {
						titleList.push("skip");
					}
				}
			var links = doc.evaluate('//td/div[@class="floatRight"]/a', doc, nsResolver, XPathResult.ANY_TYPE, null);
			var xPathLinksCount = doc.evaluate('count (//td/div[@class="floatRight"]/a)', doc, nsResolver, XPathResult.ANY_TYPE, null);

			for (i=0; i<xPathLinksCount.numberValue; i++) {
				next_title.push(links.iterateNext().href);
				if (titleList[i] != "skip") {
					items[next_title] = titleList[i];
				}
			}

			items = Zotero.selectItems(items);

			for (var i in items) {
				uris.push(i);
			}

		}

//code if single entry only.
	} else {
		uris = [url];
	}

	Zotero.Utilities.processDocuments(uris, scrape, function() {Zotero.done();});
	Zotero.wait();
}
//Session time-out --> no tests