summaryrefslogtreecommitdiff
path: root/Lulu.js
blob: 50b7a8b9057215c941b3b785d8a4cd596a6e285a (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
{
	"translatorID": "9a0ecbda-c0e9-4a19-84a9-fc8e7c845afa",
	"label": "Lulu",
	"creator": "Aurimas Vinckevicius",
	"target": "https?://www.lulu.com/shop/",
	"minVersion": "3.0",
	"maxVersion": "",
	"priority": 101,
	"inRepository": true,
	"translatorType": 12,
	"browserSupport": "gcsibv",
	"lastUpdated": "2013-10-11 03:33:45"
}

function getSearchResults(doc) {
	return ZU.xpath(doc, '//div[@class="middle-column"]/div[@class="products"]/div//a[@class="title" and @href]');
}

function detectWeb(doc, url) {
	if(url.search(/\/product-\d+\.html/) != -1) {
		return 'book';
	}
	
	if(url.indexOf('/search.ep?') != -1
		&& getSearchResults(doc).length) {
		return 'multiple';
	}
}

function doWeb(doc, url) {
	var results = getSearchResults(doc);
	if(results.length) {
		var items = {};
		for(var i=0, n=results.length; i<n; i++) {
			items[results[i].href] = ZU.trimInternal(results[i].textContent);
		}
		
		Z.selectItems(items, function(selectedItems) {
			if(!selectedItems) return true;
			
			var urls = [];
			for(var i in selectedItems) {
				urls.push(i);
			}
			ZU.processDocuments(urls, scrape);
		})
	} else {
		scrape(doc, url);
	}
}

function scrape(doc, url) {
	var item = makeItem(doc, url);
	item.complete();
}

function makeItem(doc, url) {
	var item = new Zotero.Item('book');
	item.title = ZU.capitalizeTitle(
		ZU.trimInternal(ZU.xpathText(doc, '//div[@class="product-information"]/h2[1]')),
		true
	);
	
	var authors = ZU.xpath(doc, '//div[@class="product-information"]//span[@class="authors"]/a/span');
	for(var i=0, n=authors.length; i<n; i++) {
		var name = ZU.trimInternal(authors[i].textContent).replace(/^(?:Dr|Prof)\.?\s|\s(?:M.?A|Ph\.?D|B\.?S|B\.?A|M\.?D(?:\.?\sPh\.?D)?)\.?$/gi, '');
		item.creators.push(ZU.cleanAuthor(ZU.capitalizeTitle(name, true), 'author'));
	}
	
	var description = doc.getElementsByClassName('description')[0];
	if(description.getElementsByClassName('expandable-text').length) {
		description = ZU.xpathText(description, './span/text()[1]')
			+ ' ' + ZU.xpathText(description, './span/span[@class="more-text"]/text()[1]');
	} else {
		description = description.textContent;
		if(ZU.trimInternal(description) == 'No description supplied') {
			description = false;
		}
	}
	
	if(description) {
		item.abstractNote = description.trim().replace(/ +/, ' ');
	}
	
	var productDetails = doc.getElementsByClassName('product-details')[0];
	item.ISBN = ZU.cleanISBN(ZU.xpathText(productDetails, './dd[@class="isbn"]') || '', true);
	item.publisher = ZU.trimInternal(ZU.xpathText(productDetails, './dd[@class="publisher"]') || '');
	item.rights = ZU.trimInternal(ZU.xpathText(productDetails, './dd[@class="copyright-info"]') || '');	
	item.language = ZU.trimInternal(ZU.xpathText(productDetails, './dd[@class="language"]') || '');
	item.date = ZU.trimInternal(ZU.xpathText(productDetails, './dd[@class="publication-date"]') || '');
	item.numPages = ZU.trimInternal(ZU.xpathText(productDetails, './dd[@class="pages"]') || '');
	
	item.attachments.push({
		title: "Lulu Link",
		url: url,
		mimeType: 'text/html',
		snapshot: false
	})
	
	return item;
}

function detectSearch(items) {
	if(items.ISBN) return true;
	
	if(!items.length) return;
	
	for(var i=0, n=items.length; i<n; i++) {
		if(items[i].ISBN && ZU.cleanISBN('' + items[i].ISBN)) {
			return true;
		}
	}
}

function doSearch(items) {
	if(!items.length) items = [items];
	
	var query = [];
	for(var i=0, n=items.length; i<n; i++) {
		var isbn;
		if(items[i].ISBN && (isbn = ZU.cleanISBN('' + items[i].ISBN))) {
			(function(item, isbn) {
				ZU.processDocuments('http://www.lulu.com/shop/search.ep?keyWords=' + isbn, function(doc, url) {
					var results = getSearchResults(doc);
					if(!results.length) {
						if(item.complete) item.complete();
						return;
					}
					
					ZU.processDocuments('http://www.lulu.com' + ZU.xpathText(results[0], './@href'), function(doc, url) {
						var newItem = makeItem(doc, url);
						if(newItem.ISBN == isbn) {
							newItem.complete();
						} else {
							if(item.complete) item.complete();
						}
					});
				})
			})(items[i], isbn);
		} else if(items[i].complete) {
			items[i].complete();
		}
	}
}/** BEGIN TEST CASES **/
var testCases = [
	{
		"type": "web",
		"url": "http://www.lulu.com/shop/dr-r-selvakumar/diseases-of-plantation-crops/ebook/product-17472985.html",
		"items": [
			{
				"itemType": "book",
				"creators": [
					{
						"firstName": "R.",
						"lastName": "Selvakumar",
						"creatorType": "author"
					}
				],
				"notes": [],
				"tags": [],
				"seeAlso": [],
				"attachments": [
					{
						"title": "Lulu Link",
						"mimeType": "text/html",
						"snapshot": false
					}
				],
				"title": "Diseases of Plantation Crops",
				"publisher": "Dr.SELVAKUMAR RAJAN",
				"rights": "selvakumar (Standard Copyright License)",
				"language": "English",
				"date": "September 29, 2011",
				"numPages": "15",
				"libraryCatalog": "Lulu"
			}
		]
	},
	{
		"type": "web",
		"url": "http://www.lulu.com/shop/search.ep?keyWords=zotero&categoryId=107110&sorter=relevance-desc",
		"items": "multiple"
	},
	{
		"type": "web",
		"url": "http://www.lulu.com/shop/daniel-segars-and-kelli-segars/4-week-fat-loss-program-for-busy-people/ebook/product-21169392.html",
		"items": [
			{
				"itemType": "book",
				"creators": [
					{
						"firstName": "Daniel",
						"lastName": "Segars",
						"creatorType": "author"
					},
					{
						"firstName": "Kelli",
						"lastName": "Segars",
						"creatorType": "author"
					}
				],
				"notes": [],
				"tags": [],
				"seeAlso": [],
				"attachments": [
					{
						"title": "Lulu Link",
						"mimeType": "text/html",
						"snapshot": false
					}
				],
				"title": "4 Week Fat Loss Program for Busy People",
				"abstractNote": "Fitness Blender's 4 Week Fat Loss Program for Busy People features workouts that are 30 minutes or less, combining fat blasting HIIT with metabolism boosting strength training to bring about incredible results quickly. This challenging home workout program only requires dumbbells. The detailed, day-by-day plan uses Fitness Blender's free online workout videos to challenge & change your body fast. HIIT, cardio, strength training, circuit training, supersets, plyometrics, Pilates, yoga, & kettlebell training (dumbbells are an ample substitute) come together to create the ideal workout program.  Many people who complete these programs see weight loss - often 8-12 lbs in just 1 month - reduced body fat, drastic improvements in body tone, endurance, strength, & flexibility gains. Each day you get a Calorie Burn estimate & we include a brief but effective Nutrition section to give you the essentials on how to properly nourish yourself during the program. 30 Minutes is 1/48th  of your day; no more excuses.",
				"publisher": "Fitness Blender",
				"rights": "Standard Copyright License",
				"language": "English",
				"date": "August 20, 2013",
				"numPages": "63",
				"libraryCatalog": "Lulu"
			}
		]
	},
	{
		"type": "search",
		"input": {
			"ISBN": "9780951470329"
		},
		"items": [
			{
				"itemType": "book",
				"creators": [
					{
						"firstName": "Stephen Skelton",
						"lastName": "MW",
						"creatorType": "author"
					}
				],
				"notes": [],
				"tags": [],
				"seeAlso": [],
				"attachments": [
					{
					"title": "Lulu Link",
					"mimeType": "text/html",
					"snapshot": false
					}
				],
				"abstractNote": "ALL YOU NEED TO KNOW ABOUT GROWING VINES IN 123 PAGES. \n\nThis book is a basic introduction to growing grapes and aimed at the serious student in the wine trade, WSET Diploma student or Master of Wine candidate. It is also very useful for those thinking of setting up vineyards as it answers a lot of the basic questions. \n\nHas sold over 3,500 copies now and received LOTS of emails saying how helpful it has been. \n\n\"Couldn't have become an MW without your book\" was the latest endorsement!",
				"ISBN": "9780951470329",
				"rights": "Stephen Skelton (Standard Copyright License)",
				"language": "English",
				"numPages": "146",
				"libraryCatalog": "Lulu",
				"title": "Viticulture - An Introduction to Commercial Grape Growing for Wine Production",
				"publisher": "Stephen Skelton",
				"date": "May 24, 2014"
			}
		]
	}
]
/** END TEST CASES **/