summaryrefslogtreecommitdiff
path: root/DPLA.js
blob: 9b2c131f07cc3371cc11f4877038cee11f92754a (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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
{
	"translatorID": "117feb72-21bb-4424-a47b-c9ca6b71f979",
	"label": "DPLA",
	"creator": "Sebastian Karcher",
	"target": "^https?://dp\\.la/",
	"minVersion": "3.0",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcv",
	"lastUpdated": "2013-10-24 09:48:34"
}

/**
	Copyright (c) 2012 Sebastian Karcher
	
	This program 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.
	
	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
	Affero General Public License for more details.
	
	You should have received a copy of the GNU Affero General Public
	License along with this program. If not, see
	<http://www.gnu.org/licenses/>.
*/

function detectWeb(doc, url) {
	if (url.indexOf("/search?") != -1) return "multiple";

	else if (url.indexOf("/item/") != -1) {
		var type = ZU.xpathText(doc, '//article[@id="content"]/div[@class="table"]/ul/li[h6[contains(text(), "Type")]]/following-sibling::li')
		//Z.debug(type)
		//we can't use the typemap below, as the types get merged together when scraping them
		if (type){
			if (type.search(/^(image|physical)/) != -1) return "artwork";
			else if (type.search(/^(image|physical)/) != -1) return "artwork";
			else if (type.search(/^sound/) != -1) return "audioRecording";
			else if (type.search(/^moving/) != -1) return "film";
			else if (type.search(/^software/) != -1) return "computerProgram";
			else if (type.search(/^(dataset|interactive)/) != -1) return "webpage";
			else return "book";
		}
		else return "book";
	}
}

function doWeb(doc, url) {
	//take this code mostly from pubmed, which has a very similar API structure
	if (detectWeb(doc, url) == "multiple") {
		var results = ZU.xpath(doc, '//div[@class="search"]//h4/a[contains(@href, "/item/")]');
		var items = {};
		var title, id;
		for (var i = 0, n = results.length; i < n; i++) {
			title = results[i].textContent;
			id = results[i].href.match(/item\/([^\?]+)/)[1]

			if (id && title) {
				// Keys must be strings. Otherwise, Chrome sorts numerically instead of by insertion order.
				items["j" + id] = title;
			}
		}

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

			var ids = [];
			for (var i in selectedItems) {
				ids.push(i.substr(1));
			}
			getJSON(ids);
		});
	} else {
		var id = url.match(/item\/([^\?]+)/)[1];
		getJSON([id]);
	}
}

function getJSON(ids) {
	Z.debug(ids)
	var JASONurl = "http://api.dp.la/v2/items/" + ids.join(",") + "?api_key=910de961922b85c6e95ee1311938ece6";
	Zotero.Utilities.doGet(JASONurl, parseDPLAapi);
}

var typemap = {
	"image": "artwork",
	"physical object": "artwork",
	"text": "book",
	"collection": "book",
	"moving image": "film",
	"interactive resource": "webpage",
	"dataset": "webpage",
	"software": "computerProgram",
	"sound": "audioRecording"
}

function parseDPLAapi(text) {
	try {
		var obj = JSON.parse(text).docs;
	} catch (e) {
		Zotero.debug("JSON parse error");
		throw e;
	}

	for (i in obj) {
		var data = obj[i]
		var source = data.sourceResource
		//Z.debug(source)
		var item = new Zotero.Item("book");
		//a lot of fields come either as arrays or as strings - check for that, else we'll get ugliness esp. when we expect an array.
		if (typeof source.title === "string") item.title = source.title;
		else item.title = source.title[0];
		if (source.date) item.date = source.date.displayDate;
		var pubinfo = source.publisher;
		if (pubinfo) {
			if (pubinfo.indexOf(":") != -1) {
				pubinfo = pubinfo.match(/([^:]*):\s*(.+)/);
				item.place = pubinfo[1];
				item.publisher = pubinfo[2];
			}
		}
		//authors are sometimes strings, sometimes arrays
		if (typeof source.creator === "string") {
			var fieldmode = 0;
			var comma = true;
			if (source.creator.indexOf(",") == -1) {
				fieldmode = 1;
			}
			item.creators.push(ZU.cleanAuthor(source.creator, "author", true))
			item.creators[0].fieldMode = fieldmode;
		} else {
			for (i in source.creator) {
				var fieldmode = 0;
				var comma = true;
				if (source.creator[i].indexOf(",") == -1) {
					fieldmode = 1;
				}
				item.creators.push(ZU.cleanAuthor(source.creator[i], "author", true))
				item.creators[i].fieldMode = fieldmode;
			}
		}
		if (typeof source.description === "string") {
			item.abstractNote = source.description;
		}
		//description could be either tag or abstract, but overall abstract makes more sense;
		else {
			var abstract = [];
			for (i in source.description) {
				abstract.push(source.description[i]);
			}
			item.abstractNote = abstract.join("; ");
		}
		for (i in source.subject) {
			item.tags.push(source.subject[i].name);
		}
		if (!item.place) {
			if (source.spatial) {
				//this again is displayed in two different ways:
				if (source.spatial.length){
					if (source.spatial[0].city) item.place = source.spatial[0].city;
					else if (source.spatial[0].name) item.place = source.spatial[0].name;
				}
				else{
					if (source.spatial.city) item.place = source.spatial.city;
					else if (source.spatial.name) item.place = source.spatial.name;
				}
			}
		}

		if (source.format && typeof source.format === "string") item.medium = source.format;
		else if (source.format) {
			format = [];
			for (i in source.format) {
				format.push(source.format[i]);
				item.medium = format.join(", ");
			}
		}
		item.attachments.push({
			url: "http://dp.la/item/" + data.id,
			title: "DPLA Link",
			mimeType: "text/html",
			snapshot: false
		})
		item.rights = source.rights;
		item.url = data.isShownAt;
		item.archive = data.dataProvider;
		if (source.language) {
			if (source.language.iso639_3) item.language = source.language.iso639_3;
			else if (source.language.iso639_2) item.language = source.language.iso639_2;

		}
		var type = source.type;
		if (type){
			if (typeof type != "string") type = type[0];
			if (typemap[type]) item.itemType = typemap[type];
		}
		else item.itemType = "book";
		item.complete();
	}
}/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://dp.la/search?&q=labor&utf8=%E2%9C%93",
		"items": "multiple"
	},
	{
		"type": "web",
		"url": "http://dp.la/item/100b1a8594ff7d162e13ff9dfca27c3d",
		"items": [
			{
				"itemType": "artwork",
				"creators": [],
				"notes": [],
				"tags": [
					"Palmer, William R., 1877-1960",
					"Portraits"
				],
				"seeAlso": [],
				"attachments": [
					{
						"title": "DPLA Link",
						"mimeType": "text/html",
						"snapshot": false
					}
				],
				"title": "O. W. Croxall",
				"date": "1899-08",
				"rights": "Digital image c1999 Sherratt Library, Southern Utah University. All rights reserved.",
				"abstractNote": "O. W. Croxall; Photographs",
				"url": "http://thoth.library.utah.edu:1701/primo_library/libweb/action/dlDisplay.do?vid=MWDL&afterPDS=true&docId=digcoll_suu_13palmer/918",
				"archive": "Southern Utah University - Sherratt Library",
				"libraryCatalog": "DPLA",
				"accessDate": "CURRENT_TIMESTAMP"
			}
		]
	},
	{
		"type": "web",
		"url": "http://dp.la/item/b3d8c3ad9a97610d77eb9c53c7def845?back_uri=http%3A%2F%2Fdp.la%2Fsearch%3Fq%3Dlabor%26type%255B%255D%3Dsound%26utf8%3D%25E2%259C%2593",
		"items": [
			{
				"itemType": "audioRecording",
				"creators": [
					{
						"firstName": "Mary",
						"lastName": "Moultrie",
						"creatorType": "author",
						"fieldMode": 0
					},
					{
						"firstName": "Jean-Claude",
						"lastName": "Bouffard",
						"creatorType": "author",
						"fieldMode": 0
					}
				],
				"notes": [],
				"tags": [
					"African American nurses--South Carolina Charleston",
					"African Americans--Civil rights--South Carolina--Charleston",
					"African Americans--South Carolina--Charleston--History--20th century",
					"African Americans--South Carolina--Charleston--Social conditions",
					"Charleston (S.C.)--Race relations--History--20th century",
					"Clark, Septima Poinsette, 1898-1987",
					"Hospital Workers' Strike, Charleston, S.C., 1969",
					"Hospitals--Employees--Labor unions--South Carolina Charleston",
					"Hospitals--Employees--Salaries, etc",
					"Hospitals--South Carolina--Charleston--History--20th century",
					"Labor unions--South Carolina--Charleston",
					"McCord, William M. (William Mellon), 1907-1996",
					"Medical College of South Carolina (1952-1969)--History",
					"Moultrie, Mary",
					"Race discrimination--South Carolina--History",
					"Saunders, William, 1935-",
					"Southern Christian Leadership Conference",
					"Strikes and lockouts--Hospitals--South Carolina",
					"Women--Employment--South Carolina--Charleston"
				],
				"seeAlso": [],
				"attachments": [
					{
						"title": "DPLA Link",
						"mimeType": "text/html",
						"snapshot": false
					}
				],
				"title": "Oral History Interview with Mary Moultrie",
				"date": "1982-07-28",
				"abstractNote": "In this interview, Mary Moultrie (born 1943) talks about her involvement in the 113-day Charleston Hospital Strike at the Medical University of South Carolina (MUSC) in March 1969. After graduating from Burke High School in 1960, Moultrie went to Goldwater Memorial Hospital, New York to become a Licensed Practical Nurse (LPN). In 1967, she returned to Charleston and was hired at MUSC only as a nurses assistant since her LPN was not accepted. She speaks in detail about the working conditions and employee relationships at MUSC prior to and after the strike. Ms. Moultrie explains the various types of nursing titles and the unequal pay between black and white nurses. She retells in detail how the racial tensions that led up to the strike at MUSC increased due to the harassing treatment toward black nurses. In the interview, Moultrie details the first informal meetings and get-togethers that were held, until the black nurses joined forces with the 1199 union and Bill Saunders. Moultrie elaborates, in particular, on the lack of support from the white community under the Gaillard administration, as well as the hesitation of the black community to join them in their efforts for equal pay and treatment. She then refers to support from the Southern Christian Leadership Conference (SCLC), Septima Poinsette Clark, and various leaders such as Andrew Young and Ralph Abernathy. Moultrie mentions the difficult and hostile negotiation process with MUSCs president Dr. William McCord and the memorandum of agreement that was ultimately reached. The interview closes by the interviewer inquiring about the current work force diversity at MUSC and Moultries feelings regarding the strikes accomplishments.",
				"place": "Charleston",
				"rights": "Digital resource copyright 2010, Avery Research Center at the College of Charleston Libraries. All rights reserved. For more information contact the Avery Research Center, College of Charleston, Charleston, SC 29424.",
				"url": "http://lowcountrydigital.library.cofc.edu/u?/AOH,203",
				"archive": "Avery Research Center at the College of Charleston",
				"libraryCatalog": "DPLA",
				"audioRecordingFormat": "Audio"
			}
		]
	},
	{
		"type": "web",
		"url": "http://dp.la/item/9824dc12222f81e6040f85db570a2a32?back_uri=http%3A%2F%2Fdp.la%2Fsearch%3Flanguage%255B%255D%3DSpanish%26q%3Dlabor%26utf8%3D%25E2%259C%2593",
		"items": [
			{
				"itemType": "book",
				"creators": [
					{
						"firstName": "Mariano",
						"lastName": "Cuevas",
						"creatorType": "author",
						"fieldMode": 0
					}
				],
				"notes": [],
				"tags": [
					"Catholic Church",
					"Catholic Church",
					"Missions"
				],
				"seeAlso": [],
				"attachments": [
					{
						"title": "DPLA Link",
						"mimeType": "text/html",
						"snapshot": false
					}
				],
				"title": "Historia de la Iglesia en México, v. 2",
				"date": "1921",
				"place": "Tlalpam [D.F.] (México)",
				"publisher": "Impr. del asilo \"Patricio Sanz",
				"abstractNote": "Added t.p; Bibliografia de la historia de la Iglesia en México\" : v. 1, p. [13]-27, and at head of chapters; Includes indexes; Appendices; STH LIBRARY HAS: vols. 1-3 ; later edition (1928) has vols. 4-5. See Theo Research BX1428.C8 1928; V. 1. Estado del país de Anahuac antes de su evangelización. Orígenes de la Iglesia en Nueva España, 1511-1548.--v. 2. Consolidación y actividades de las instituciones fundadoras, 1548-1572. Los elementos regeneradores, 1572-1600. Frutos especiales de la Iglesia en el Siglo XVI.--v. 3. Instituciones y labor de la Iglesia organizada. Las Misiones. Frutos de la Iglesia en el Siglo XVII",
				"rights": "Access to the Internet Archive's Collections is granted for scholarship and research purposes only. Some of the content available through the Archive may be governed by local, national, and/or international laws and regulations, and your use of such content is solely at your own risk.",
				"url": "http://www.archive.org/details/historiadelaigle02cuev",
				"archive": "School of Theology, Boston University",
				"libraryCatalog": "DPLA"
			}
		]
	}
]
/** END TEST CASES **/