summaryrefslogtreecommitdiff
path: root/informIT database.js
blob: ec3524221261bd491cfa714e6a21f20868031d68 (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
{
	"translatorID": "add79dfd-7951-4c72-af1d-ce1d50aa4fb4",
	"label": "informIT database",
	"creator": "Adam Crymble, Sebastian Karcher",
	"target": "^https?://www\\.informit\\.com",
	"minVersion": "2.1.9",
	"maxVersion": "",
	"priority": 100,
	"inRepository": true,
	"translatorType": 4,
	"browserSupport": "gcsibv",
	"lastUpdated": "2012-10-15 10:37:49"
}

function detectWeb(doc,  url) {
	if (doc.title.match("Search Results")) {
		return "multiple";
	} else if (doc.location.href.match("topics")) {
		return "multiple";

	} else if (doc.location.href.match("product")) {
		return "book";
	} else if (doc.location.href.match("guides")) {
		return "book";

	} else if (doc.location.href.match("-978")) {
		return "book";
	}else if (doc.location.href.match("library")) {
		return "bookSection";
	} else if (doc.location.href.match(/articles\/article/)) {
		return "bookSection";
	}
}

//informIT database translator. Code by Adam Crymble

function scrape(doc, url) {

	var dataTags = new Object();

	//FOR GUIDES
		if (doc.location.href.match("guides")) {
			var newItem = new Zotero.Item("book");
			newItem.title = doc.evaluate('//h1', doc, null, XPathResult.ANY_TYPE, null).iterateNext().textContent;

			var authors = doc.evaluate('//div[@class="titling"]/p/a', doc, null, XPathResult.ANY_TYPE, null).iterateNext().textContent;
		}

	//FOR ARTICLES
		if (doc.location.href.match(/articles\/article/)) {
			var newItem = new Zotero.Item("bookSection");

			var contents = doc.evaluate('//div[@id="articleHeader"]/ul/li', doc, null, XPathResult.ANY_TYPE, null);
			var xPathCount = doc.evaluate('count (//div[@id="articleHeader"]/ul/li)', doc, null, XPathResult.ANY_TYPE, null);

			var authors = contents.iterateNext().textContent.substr(3);

			if (doc.evaluate('//div[@class="relatedBook"]/p/a', doc, null, XPathResult.ANY_TYPE, null).iterateNext()) {
				newItem.bookTitle = doc.evaluate('//div[@class="relatedBook"]/p/a', doc, null, XPathResult.ANY_TYPE, null).iterateNext().textContent;
			}

			newItem.date = contents.iterateNext().textContent;

			var rights1;
			if (xPathCount.numberValue> 2) {
				newItem.rights = contents.iterateNext().textContent;
			}

			newItem.title = doc.evaluate('//h1', doc, null, XPathResult.ANY_TYPE, null).iterateNext().textContent;

		} else if (doc.evaluate('//ul[@class="bibliography"]/li', doc, null, XPathResult.ANY_TYPE, null).iterateNext()) {


	//FOR STORE BOOKS
		var newItem = new Zotero.Item("book");

		var contents = doc.evaluate('//ul[@class="bibliography"]/li', doc, null, XPathResult.ANY_TYPE, null);
		var xPathCount = doc.evaluate('count (//ul[@class="bibliography"]/li)', doc, null, XPathResult.ANY_TYPE, null);

		for (i=0; i<xPathCount.numberValue; i++) {
		 		dataTags[i] = Zotero.Utilities.cleanTags(contents.iterateNext().textContent.replace(/^\s*|\s*$/g, ''));
		 	}

		var authors = dataTags[0].substr(3);

		if (dataTags[1].match("Published")) {
			var publisherInfo = dataTags[1].substr(10);
			var date = publisherInfo.substr(0, 12);
			newItem.date = date;

			if (publisherInfo.match(/\sby/)) {
				var publishCo = publisherInfo.split(/\s*by\s*/);
				newItem.publisher = publishCo[1];
			}
		}
		var bibinfo = ZU.xpathText(doc, '//ul[@id="bibPubInfo"]');
		if (bibinfo){
			var numPages = bibinfo.match(/Pages: (\d+)/)[1];
			var edition = bibinfo.match(/Edition: (.+)/)[1];
			newItem.numPages = numPages;
			newItem.edition = edition;	
		}
		var isbn = ZU.xpathText(doc, '//ul[@id="bibISBN"]');
		if (isbn){
			isbn = isbn.replace(/.+\n\s*ISBN-10:\s*/, "").replace(/\s*ISBN-13:\s*/, ", ");
			newItem.ISBN = isbn;
		}
		newItem.title = doc.evaluate('//h1', doc, null, XPathResult.ANY_TYPE, null).iterateNext().textContent;


  	 //FOR LIBRARY BOOKS
		} else if (doc.location.href.match("library")) {

			var newItem = new Zotero.Item("bookSection");

			newItem.title = doc.evaluate('//h2', doc, null, XPathResult.ANY_TYPE, null).iterateNext().textContent;
			var meta = doc.evaluate('//div[@id="columnOne"]/p', doc, null, XPathResult.ANY_TYPE, null);
			newItem.bookTitle = meta.iterateNext().textContent;

			var authors = meta.iterateNext().textContent.substr(3);
		}

	 //SHARED
		var noMoreAuthor = 0;

		if (authors.match(" and ")) {
			authors = authors.split(" and ");
		} else if (authors.match(", ")) {
			authors = authors.split(", ");
		} else {
			newItem.creators.push(Zotero.Utilities.cleanAuthor(authors, "author"));
			noMoreAuthor = 1;
		}

		if (authors.length>0 && noMoreAuthor != 1) {

			for (var i = 0; i < authors.length; i++) {
				newItem.creators.push(Zotero.Utilities.cleanAuthor(authors[i], "author"));
			}
		}

	newItem.url = doc.location.href;
	if (newItem.publisher) newItem.publisher = Zotero.Utilities.trimInternal(newItem.publisher);
	newItem.complete();
}

function doWeb(doc, url) {
	var articles = new Array();

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

	   //xPath for Topics pages, else xPaths for regular search pages.
		if (doc.location.href.match("topics")) {
			var titles = doc.evaluate('//div[@class="productList articles"]/dl/dt/a', doc, null, XPathResult.ANY_TYPE, null);
		} else {
			var titles = doc.evaluate('//div[@class="searchresult"]/ul/li/a', doc, null, XPathResult.ANY_TYPE, null);
			var chapters = doc.evaluate('//dt/a', doc, null, XPathResult.ANY_TYPE, null);
		}

		while (next_title = titles.iterateNext()) {
			items[next_title.href] = next_title.textContent;
		}

		if (doc.title.match("Search Results")) {
			while (next_title = chapters.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);
			}
			Zotero.Utilities.processDocuments(articles, scrape, function () {});
		});
	} else {
		scrape(doc, url)
	}
}

/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://www.informit.com/articles/article.aspx?p=1756412",
		"items": [
			{
				"itemType": "bookSection",
				"creators": [
					{
						"firstName": "Jeffrey",
						"lastName": "Hughes",
						"creatorType": "author"
					}
				],
				"notes": [],
				"tags": [],
				"seeAlso": [],
				"attachments": [],
				"date": "Nov 3, 2011",
				"title": "Your iPhone and iPad App Marketing Strategy: Grand Slam or Base Hits?",
				"url": "http://www.informit.com/articles/article.aspx?p=1756412",
				"libraryCatalog": "informIT database",
				"accessDate": "CURRENT_TIMESTAMP",
				"shortTitle": "Your iPhone and iPad App Marketing Strategy"
			}
		]
	},
	{
		"type": "web",
		"url": "http://www.informit.com/store/marketing-your-new-business-9780132371780",
		"items": [
			{
				"itemType": "book",
				"creators": [
					{
						"firstName": "Bruce",
						"lastName": "Barringer",
						"creatorType": "author"
					}
				],
				"notes": [],
				"tags": [],
				"seeAlso": [],
				"attachments": [],
				"date": "Aug 12, 2010",
				"publisher": "FT Press. Part of the FT Press Delivers Elements series.",
				"numPages": "8",
				"edition": "1st",
				"ISBN": "0-13-237178-2, 978-0-13-237178-0",
				"title": "Marketing Your New Business",
				"url": "http://www.informit.com/store/marketing-your-new-business-9780132371780",
				"libraryCatalog": "informIT database",
				"accessDate": "CURRENT_TIMESTAMP"
			}
		]
	},
	{
		"type": "web",
		"url": "http://www.informit.com/search/index.aspx?query=marketing&imageField.x=0&imageField.y=0",
		"items": "multiple"
	}
]
/** END TEST CASES **/