summaryrefslogtreecommitdiff
path: root/Internet Archive.js
blob: d5d0ddb6e44eb676ea13080946176938d76c357f (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": "db0f4858-10fa-4f76-976c-2592c95f029c",
	"label": "Internet Archive",
	"creator": "Adam Crymble, Sebastian Karcher",
	"target": "^https?://(www\\.)?archive\\.org/",
	"minVersion": "3.0",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcsibv",
	"lastUpdated": "2012-07-10 06:51:49"
}

function detectWeb(doc, url) {
	var mediaType = "1";

	if (doc.evaluate('//h3', doc, null, XPathResult.ANY_TYPE, null).iterateNext()) {
		mediaType = doc.evaluate('//h3', doc, null, XPathResult.ANY_TYPE, null).iterateNext().textContent;

	} else if (doc.evaluate('//div[@class="box"][@id="spotlight"]/h1', doc, null, XPathResult.ANY_TYPE, null).iterateNext()) {
		mediaType = doc.evaluate('//div[@class="box"][@id="spotlight"]/h1', doc, null, XPathResult.ANY_TYPE, null).iterateNext().textContent;

	} else if (doc.evaluate('//div[@class="box"]/h1', doc, null, XPathResult.ANY_TYPE, null).iterateNext()) {
		mediaType = doc.evaluate('//div[@class="box"]/h1', doc, null, XPathResult.ANY_TYPE, null).iterateNext().textContent;
	}

	if (mediaType == "The Item") {
		return "artwork";
	} else if (mediaType.indexOf("Spotlight") != -1) {
		return "book";
	} else if (mediaType.indexOf("book") != -1) {
		return "book";
	} else if (mediaType.indexOf("movie") != -1) {
		return "film";
	} else if (mediaType.indexOf("audio") != -1) {
		return "audioRecording";
	} else if (doc.location.href.match("search") && mediaType == "1") {
		return "multiple";
	}
}

var typemap = {
	"texts": "book",
	"movies": "film",
	"audio": "audioRecording",
	"etree": "audioRecording",
	"software": "computerProgram"
}

function test(data){
	var clean = data ? data[0] : undefined;
	return clean;
}

function scrape(apiurl) {
		ZU.doGet(apiurl, function (text) {
			Z.debug(text)
			try {
				var obj = JSON.parse(text).metadata;
			} catch (e) {
				Zotero.debug("JSON parse error");
				throw e;
			}
			var type = obj.mediatype[0];
			var itemType = typemap[type];

			if (itemType) var newItem = new Zotero.Item(itemType);
			else var newItem = new Zotero.Item("Document");
			newItem.title = obj.title[0];
			var creators = obj.creator;
			//sometimes authors are in one field delimiter by ;
			if (creators && creators[0].match(/;/)) {
				creators = creators[0].split(/\s*;\s*/);
			}
			for (var i in creators) {
				//authors are lastname, firsname, additional info - only use the first two.
				var author = creators[i].replace(/(\,[^\,]+)(\,.+)/, "$1");
				newItem.creators[i] = ZU.cleanAuthor(author, "author", true);
			}
			var contributors = obj.contributor;
			for (i in contributors) {
				//authors are lastname, firsname, additional info - only use the first two.
				var contributor = contributors[i].replace(/(\,[^\,]+)(\,.+)/, "$1");
				newItem.creators.push(ZU.cleanAuthor(contributor, "contributor", true));
			}

			for (i in newItem.creators) {
				if (!newItem.creators[i].firstName) {
					newItem.creators[i].fieldMode = 1;
				}
			}
			//abstracts can be in multiple fields;
			if(obj.description) newItem.abstractNote = obj.description.join("; ");

			var date = obj.date;
			if (!date) date = obj.year;
			var tags = test(obj.subject);
			if (tags) tags = tags.split(/\s*;\s*/);
			for (i in tags) {
				newItem.tags.push(tags[i]);
			}
			newItem.date = test(date);
			newItem.publisher = test(obj.publisher);
			newItem.language = test(obj.language);
			newItem.callNumber = test(obj.call_number);
			newItem.numPages = test(obj.imagecount);
			newItem.runningTime = test(obj.runtime);
			newItem.rights = test(obj.licenseurl);
			newItem.url = "http://archive.org/details/" + test(obj.identifier);

			newItem.complete();
		});
	}

	function doWeb(doc, url) {

		var items = {};
		var articles = new Array();

		if (detectWeb(doc, url) == "multiple") {
			Zotero.debug("multiple");
			var items = new Object();

			var titles = doc.evaluate('//td[2][@class="hitCell"]/a[@class="titleLink"]', doc, null, XPathResult.ANY_TYPE, null);
			var next_title;
			while (next_title = titles.iterateNext()) {
				items[next_title.href] = next_title.textContent;
			}

			Zotero.selectItems(items, function (items) {
				if (!items) {
					return true;
				}
				for (var i in items) {
					articles.push(i + "&output=json");
				}
				scrape(articles, function () {})
			});
		} else {
			scrape(url + "&output=json");
		}
	}/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://archive.org/details/gullshornbookstu00dekk",
		"items": [
			{
				"itemType": "book",
				"creators": [
					{
						"firstName": "Thomas",
						"lastName": "Dekker",
						"creatorType": "author"
					},
					{
						"firstName": "John",
						"lastName": "Nott",
						"creatorType": "author"
					},
					{
						"lastName": "University of Pittsburgh Library System",
						"creatorType": "contributor",
						"fieldMode": 1
					}
				],
				"notes": [],
				"tags": [],
				"seeAlso": [],
				"attachments": [],
				"title": "The gull's hornbook : Stultorum plena sunt omnia. Al savio mezza parola basta",
				"abstractNote": "Notes by John Nott; Bibliography of Dekker: p. iii-ix",
				"date": "1812",
				"publisher": "Bristol, Reprinted for J.M. Gutch and Sold in London by R. Baldwin, and R. Triphook",
				"language": "eng",
				"callNumber": "31735060398496",
				"numPages": "228",
				"url": "http://archive.org/details/gullshornbookstu00dekk",
				"libraryCatalog": "Internet Archive",
				"accessDate": "CURRENT_TIMESTAMP",
				"shortTitle": "The gull's hornbook"
			}
		]
	},
	{
		"type": "web",
		"url": "http://www.archive.org/search.php?query=cervantes%20AND%20mediatype%3Atexts",
		"items": "multiple"
	},
	{
		"type": "web",
		"url": "http://archive.org/details/Allen_Ginsberg__Anne_Waldman__Steven_Tay_89P046",
		"items": [
			{
				"itemType": "audioRecording",
				"creators": [
					{
						"firstName": "Allen",
						"lastName": "Ginsberg",
						"creatorType": "author"
					},
					{
						"firstName": "Bobbie Louise",
						"lastName": "Hawkins",
						"creatorType": "author"
					},
					{
						"firstName": "Steven",
						"lastName": "Taylor",
						"creatorType": "author"
					},
					{
						"firstName": "Anne",
						"lastName": "Waldman",
						"creatorType": "author"
					}
				],
				"notes": [],
				"tags": [],
				"seeAlso": [],
				"attachments": [],
				"title": "Allen Ginsberg, Anne Waldman, Steven Taylor and Bobbi Louise Hawkins performance, July, 1989.",
				"abstractNote": "Second half of a reading with Allen Ginsberg, Bobbie Louise Hawkins, Anne Waldman, and Steven Taylor.  This portion of the reading features Waldman and Ginsberg. (Continued from 89P045)",
				"date": "1989-07-08 00:00:00",
				"publisher": "Jack Kerouac School of Disembodied Poetics",
				"runningTime": "1:30:05",
				"rights": "http://creativecommons.org/licenses/by-nd-nc/1.0/",
				"url": "http://archive.org/details/Allen_Ginsberg__Anne_Waldman__Steven_Tay_89P046",
				"libraryCatalog": "Internet Archive",
				"accessDate": "CURRENT_TIMESTAMP"
			}
		]
	},
	{
		"type": "web",
		"url": "http://archive.org/details/AboutBan1935",
		"items": [
			{
				"itemType": "film",
				"creators": [
					{
						"lastName": "Castle Films",
						"creatorType": "author",
						"fieldMode": 1
					}
				],
				"notes": [],
				"tags": [
					"Agriculture: Bananas",
					"Central America"
				],
				"seeAlso": [],
				"attachments": [],
				"title": "About Bananas",
				"abstractNote": "Complete presentation of the banana industry from the clearing of the jungle and the planting to the shipment of the fruit to the American markets.",
				"date": "1935",
				"runningTime": "11:03",
				"rights": "http://creativecommons.org/licenses/publicdomain/",
				"url": "http://archive.org/details/AboutBan1935",
				"libraryCatalog": "Internet Archive",
				"accessDate": "CURRENT_TIMESTAMP"
			}
		]
	}
]
/** END TEST CASES **/