-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.js
368 lines (323 loc) · 11.6 KB
/
scraper.js
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
var request = require('request'),
phantom = require('phantom'),
u = require('url');
var Scraper = function(url) {
this.url = unescape(url);
this.minImageSize = 10240/2;
this.imageUrlRules = {
'www.shopbop.com': this.shopBigBop,
'images.urbanoutfitters.com': this.urbanTransformers
};
this.pageUrlRules = {
'm.asos.com': this.getAsosToUSFromUK
};
};
Scraper.prototype.getMetaData = function() {
var title = null;
var description = null;
var ogImage = null;
var siteName = null;
var metaElements = document.getElementsByTagName('meta');
for (var i = 0; i < metaElements.length; i++) {
// TODO: Break early?
var element = metaElements[i];
// Handle OpenGraph elements
var property = element.getAttribute('property');
console.log(property);
if (property !== null) {
switch(property) {
case 'og:title':
title = element.getAttribute('content');
break;
case 'og:description':
description = element.getAttribute('content');
break;
case 'og:image':
ogImage = element.getAttribute('content');
break;
case 'og:site_name':
siteName = element.getAttribute('content');
break;
}
}
// Handle general meta information
var name = element.getAttribute('name');
if (name !== null) {
switch(name) {
case 'title':
title = title || element.getAttribute('content');
break;
case 'description':
description = description || element.getAttribute('content');
break;
}
}
}
if (ogImage !== null) this.ogImage = ogImage;
title = title || document.title;
return {
title: title,
description: description,
siteName: siteName,
finalDestination: document.URL
};
};
Scraper.prototype.getPrice = function() {
var string = document.body.textContent;
var regex = /(\$\s*[\d,]+\.\d+)|([\d,]+\.\d+\s*USD)/g;
// Replace the element (string) or the regex that's used on per domain basis
switch(window.location.hostname) {
case 'www.gap.com':
case 'bananarepublic.gap.com':
case 'oldnavy.gap.com':
case 'piperlime.gap.com':
case 'athleta.gap.com':
string = document.getElementById('addToBagContent').textContent;
break;
case 'www.jcrew.com':
var stringElement = document.getElementById('product_details_form') || document.body;
string = stringElement.textContent;
break;
default:
break;
}
var matches = string.match(regex);
if (matches === null) {
return null;
}
// return first non-zero price
for(var i = 0; i < matches.length; i++) {
var price = matches[i];
// clearn price
price = price.replace(' ', '');
price = price.replace(',', '');
price = price.replace('$', '');
price = price.replace('USD', '');
price = price.replace('>', '');
if (price > 0) {
return price;
}
}
return null;
};
Scraper.prototype.getPotentialImageUrls = function() {
var imgElements = document.images;
var imageUrls = Array.prototype.slice.call(imgElements).map(function(element) {
return element.src;
});
switch(window.location.hostname) {
case 'www.zara.com':
var extraLargeImageElements = Array.prototype.slice.call(document.getElementsByClassName('pAuxMZoom'));
var extraLargeImages = extraLargeImageElements.map(function(element) {
return element.getAttribute('value');
});
imageUrls = imageUrls.concat(extraLargeImages);
break;
case 'www.neimanmarcus.com':
var largeImage = document.getElementsByClassName('img-wrap')[0].getElementsByTagName('img')[0].dataset.zoomUrl;
imageUrls.push(largeImage);
break;
default:
break;
}
var aElements = document.links;
var aUrls = Array.prototype.slice.call(aElements).map(function(element) {
return element.href;
});
// Modified version of Gruber's regexp
var regexp = /\b((?:https?:(?:\/{1,2}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi;
var scriptElements = document.scripts;
var scriptUrls = [];
for (var i = 0; i < scriptElements.length; i++) {
element = scriptElements[i];
var urls = element.textContent.match(regexp);
if (urls !== null) scriptUrls = scriptUrls.concat(urls);
}
var urls = [].concat(imageUrls, aUrls, scriptUrls);
urls.push(this.ogImage);
urls = urls.filter(function(e) { return e ? true : false; });
return urls;
};
Scraper.prototype.getImage = function(images, callback) {
// This can be a single pass if integrated with line 134
images = this.uniqueArray(images);
var biggestSize = this.minImageSize;
var biggestImage = null;
var alternateImages = [];
var count = images.length;
var scraper = this;
if (!count) {
callback(biggestImage, alternateImages);
return;
}
images.forEach(function(image) {
scraper.getImageSize(image, function(url, size) {
if (size > scraper.minImageSize) {
if (size > biggestSize) {
if (biggestImage !== null) {
alternateImages.push({url: biggestImage, size: biggestSize});
}
biggestSize = size;
biggestImage = url;
} else {
alternateImages.push({url: url, size: size});
}
}
if (!--count) {
callback(biggestImage, scraper.convertToSortedUrlsArray(alternateImages));
}
});
});
};
Scraper.prototype.getImageSize = function(imageUrl, callback) {
var options = this.getRequestOptions(imageUrl);
var parsedUrl = u.parse(options.url);
var host = parsedUrl.host;
if (typeof host === 'undefined') {
callback(imageUrl, -1);
return;
}
if (parsedUrl.protocol != 'http:' && parsedUrl.protol != 'https:') {
callback(imageUrl, -2);
return;
}
console.log('Attempting Content-Length for', imageUrl);
request(options, function (error, response, body) {
if (error || response.statusCode != 200) {
callback(imageUrl, -1);
console.log('Error requesting', imageUrl);
return;
}
if (typeof response.headers['content-type'] !== 'undefined' && response.headers['content-type'].substr(0, 5) != 'image') {
callback(imageUrl, -1);
return;
}
var range = response.headers['content-length'];
var size = parseInt(range, 10);
callback(imageUrl, size);
});
};
Scraper.prototype.getRequestOptions = function(url) {
// Uses the host of the page URL and not that of the image URL
var host = u.parse(this.url).host;
var methodRules = {
'm.shopbop.com': 'GET',
'www.shopbop.com': 'GET'
};
var method = methodRules[host] || 'HEAD';
// Setting up the options
var options = {
url: url,
method: method,
timeout: 2000
};
return options;
};
Scraper.prototype.convertToSortedUrlsArray = function(alternateImages) {
alternateImages.sort(function(a, b) {
return a.size > b.size ? (a.size == b.size ? 0 : -1) : 1;
});
var imageUrls = [];
var count = alternateImages.length;
for(var i = 0; i < count; i++) {
imageUrls.push(alternateImages[i].url);
}
return imageUrls;
};
Scraper.prototype.uniqueArray = function(a) {
var seen = {};
var b = [];
for (var i = 0, l = a.length; i < l; i++) {
if (seen.hasOwnProperty(a[i])) {
continue;
}
b.push(a[i]);
seen[a[i]] = true;
}
return b;
};
Scraper.prototype.getData = function(callback) {
var scraper = this;
phantom.create('--load-images=no', function(ph) {
ph.createPage(function(page) {
page.set('onConsoleMessage', function(msg) {
// console.log(msg);
});
page.open(scraper.applyUrlRules(scraper.url, 'page'), function(status) {
if (status == 'fail') {
callback({
'status': 'error'
});
// Exit PhantomJS
ph.exit();
return;
}
var semaphore = 3;
var callbackMetaData;
var callbackPrice;
var callbackImage;
var callbackAlternateImages;
var sendCallback = function() {
if (!--semaphore) {
callback({
status: 'ok',
title: callbackMetaData.title,
description: callbackMetaData.description,
image: callbackImage,
alternateImages: callbackAlternateImages,
price: callbackPrice,
siteName: callbackMetaData.siteName,
finalDestination: callbackMetaData.finalDestination
});
// Exit PhantomJS
ph.exit();
}
}
// Get title, description and the OpenGraph image
page.evaluate(scraper.getMetaData, function(metaData) {
callbackMetaData = metaData;
sendCallback();
});
// Get price
page.evaluate(scraper.getPrice, function(price) {
callbackPrice = price;
sendCallback();
});
// Get images
page.evaluate(scraper.getPotentialImageUrls, function(potentialImageUrls) {
scraper.getImage(potentialImageUrls, function(image, alternateImages) {
callbackImage = image;
callbackAlternateImages = alternateImages;
sendCallback();
});
});
});
});
});
};
Scraper.prototype.applyUrlRules = function(url, type) {
var rules;
if (type == 'page') {
rules = this.pageUrlRules;
} else if (type == 'image') {
rules = this.imageUrlRules;
}
// Host of requested page
var host = u.parse(this.url).host;
rule = rules[host];
return rule ? rule(url) : url;
};
Scraper.prototype.getAsosToUSFromUK = function(url) {
url = url.replace('http://m.asos.com/mt/www.asos.com/countryid/2/', 'http://m.asos.com/mt/www.asos.com/');
url = url.replace('http://m.asos.com/mt/www.asos.com/', 'http://m.asos.com/mt/www.asos.com/countryid/2/');
return url;
};
Scraper.prototype.urbanTransformers = function(url) {
var hackedUrl = url.replace('$cat$', '$zoom$');
hackedUrl = hackedUrl.replace('$detailthumb$', '$zoom$');
return hackedUrl;
};
Scraper.prototype.shopBigBop = function(url) {
return url.replace('p1_1-0_254x500.jpg', 'q1_1-0.jpg');
};
exports.scraper = Scraper;