summaryrefslogtreecommitdiff
path: root/Bracero History Archive.js
blob: a2a1036dca5b7f120f53e65016288d20558e56f9 (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
{
	"translatorID": "f9373e49-e6ac-46f7-aafe-bb24a2fbc3f0",
	"label": "Bracero History Archive",
	"creator": "Adam Crymble",
	"target": "^https?://braceroarchive\\.org",
	"minVersion": "1.0.0b4.r5",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcsibv",
	"lastUpdated": "2012-01-30 22:52:20"
}

function detectWeb(doc, url) {
	if (doc.title.match("Item")) {
		return "book";
	} else if (doc.evaluate('//div[@class="item-meta"]/h2/a', doc, null, XPathResult.ANY_TYPE, null).iterateNext()) {
		return "multiple";
	}
}

//Bracero History Archive translator; Code by Adam Crymble

function associateData (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) {
		if (prefix == 'x') return namespace; else return null;
	} : null;	
	
	var dataTags = new Object();
	var fieldTitle;
	var contents1;

	var headers = doc.evaluate('//h3', doc, nsResolver, XPathResult.ANY_TYPE, null);
	var contents = doc.evaluate('//div[@class="field"]/div', doc, nsResolver, XPathResult.ANY_TYPE, null);
	var xPathCount = doc.evaluate('count (//div[@class="field"]/div)', doc, nsResolver, XPathResult.ANY_TYPE, null);

	for (i=0; i<xPathCount.numberValue -1; i++) {	 	
	 			
	 		fieldTitle = headers.iterateNext().textContent.replace(/\s+/g, '');
	 		contents1 = contents.iterateNext().textContent.replace(/^\s*|\s*$/g, '');
  
	 		if (contents1.match("Empty")) {
		 		contents1 = '';
	 		}
	 		dataTags[fieldTitle] = Zotero.Utilities.cleanTags(contents1);
	 	}

   //media type?
	if (dataTags["TypeName"]) {
		if (dataTags["TypeName"].match("Oral History")) {
			Zotero.debug(doc.title);
			var newItem = new Zotero.Item("audioRecording");
		} else {
			var newItem = new Zotero.Item("book");
		}
	} else {
			var newItem = new Zotero.Item("book");
	}
	
   //creators	
	if (dataTags["Interviewee"] && dataTags["Interviewee"] != '') {
		if (dataTags["Interviewee"].match(", ")) {
			var authors = dataTags["Interviewee"].split(", ");
			authors = authors[1] + ' ' + authors[0];
			newItem.creators.push(Zotero.Utilities.cleanAuthor(authors, "interviewee"));
		} else {
			newItem.creators.push({lastName: dataTags["Interviewee"], creatorType: "interviewee"});
		}
	}
	
	if (dataTags["Interviewer"] && dataTags["Interviewer"] != '') {
		if (dataTags["Interviewer"].match(", ")) {
			var authors = dataTags["Interviewer"].split(", ");
			authors = authors[1] + ' ' + authors[0];
			newItem.creators.push(Zotero.Utilities.cleanAuthor(authors, "interviewer"));
		} else {
			newItem.creators.push({lastName: dataTags["Interviewee"], creatorType: "interviewer"});
		}
	}
	
	if (dataTags["Creator"] && dataTags["Creator"] != '') {
		if (dataTags["Creator"].match(", ")) {
			var authors = dataTags["Creator"].split(", ");
			authors = authors[1] + ' ' + authors[0];
			newItem.creators.push(Zotero.Utilities.cleanAuthor(authors, "creator"));
		} else {
			newItem.creators.push(Zotero.Utilities.cleanAuthor(dataTags["Creator"], "creator"));
		}
	}
	
	if (dataTags["AdditionalCreator"] && dataTags["AdditionalCreator"] != '') {
		if (dataTags["AdditionalCreator"].match(", ")) {
			var authors = dataTags["AdditionalCreator"].split(", ");
			authors = authors[1] + ' ' + authors[0];
			newItem.creators.push(Zotero.Utilities.cleanAuthor(authors, "creator"));
		} else {
			newItem.creators.push({lastName: dataTags["AdditionalCreator"], creatorType: "creator"});
		}
	}
	
   //associate	
	associateData (newItem, dataTags, "Description", "abstractNote");
	associateData (newItem, dataTags, "Date", "date");
	associateData (newItem, dataTags, "Publisher", "publisher");
	associateData (newItem, dataTags, "Source", "place");
	associateData (newItem, dataTags, "Location", "place");
	associateData (newItem, dataTags, "RightsHolder", "rights");
	associateData (newItem, dataTags, "Language", "lang");
	associateData (newItem, dataTags, "Title:", "title");
	associateData (newItem, dataTags, "FileNameIdentifier", "callNumber");

   //tags
	var tags1;
	var tagsContent = new Array();

	if (doc.evaluate('//li[@class="tag"]/a', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext()) {
	 	var xPathTags = doc.evaluate('//li[@class="tag"]/a', doc, nsResolver, XPathResult.ANY_TYPE, null);
	 	while (tags1 = xPathTags.iterateNext()) {
		 	tagsContent.push(tags1.textContent);
	 	}
	}

	for (var i = 0; i < tagsContent.length; i++) {
		newItem.tags[i] = tagsContent[i];
	}
	
   //title	
	newItem.title = doc.evaluate('//h1', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent;

	newItem.url = doc.location.href;
	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 fullRecord = "view=full";
	var extraChar = "?";
	
	if (detectWeb(doc, url) == "multiple") {
		var items = new Object();
				
		var titles = doc.evaluate('//div[@class="item-meta"]/h2/a', doc, nsResolver, XPathResult.ANY_TYPE, null);
		
		var next_title;
		while (next_title = titles.iterateNext()) {
			items[next_title.href + extraChar + fullRecord] = next_title.textContent;
		}
		items = Zotero.selectItems(items);
		for (var i in items) {
			articles.push(i);
		}
	} else {
		articles = [url];
	}

	Zotero.Utilities.processDocuments(articles, scrape, function() {Zotero.done();});
	Zotero.wait();
}/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://braceroarchive.org/items/show/3229",
		"items": [
			{
				"itemType": "book",
				"creators": [
					{
						"firstName": "Mario",
						"lastName": "Perez",
						"creatorType": "creator"
					}
				],
				"notes": [],
				"tags": [],
				"seeAlso": [],
				"attachments": [],
				"title": "\"The lemons are Freezing!\"",
				"url": "http://braceroarchive.org/items/show/3229",
				"libraryCatalog": "Bracero History Archive",
				"accessDate": "CURRENT_TIMESTAMP"
			}
		]
	},
	{
		"type": "web",
		"url": "http://braceroarchive.org/items/browse?search=juarez&submit_search=Search",
		"items": "multiple"
	}
]
/** END TEST CASES **/