-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
366 lines (309 loc) · 8.43 KB
/
index.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
/**
* Created by Amir on 02/09/16.
*/
"use strict";
var request = require('request');
var cheerio = require('cheerio');
var Q = require('q');
var scrappers = require('./scrappers');
/**
* default scrappers object fetch most common open graph stuff.
*/
const defaultScrappers = scrappers.openGraph;
class Scrappy {
get scrappers(){
return scrappers;
}
/**
*
* @param config - if this is a string it will be used as the url, otherwise:
* {
* url - the web page we want to scrap
* html - html string to scrap
* $ - cheerio context or any other object which support the jQuery interface. if this is provided there will be no loading of html from a web page.
* loader - if you want to load the web page with external service and just provide the html back.
* scrappers - scrappers to be fetched. you can use the scrappers built in module or use whatever you want. if this is not peovided the open graph scrappers will be used.
* }
* @param cb - callback that will get the json object object with all the keys as defined in the scrappy object and/or in the default scrappy object.
*/
get(config, cb ){
if( typeof config === 'string' ){
config = {url:config};
}
this._getContext(config, ($)=>{
if( !$ ){
console.error("context is null. ");
cb(null);
return;
}
let data = {};
let scrappers = defaultScrappers;
if( config.scrappers ){
scrappers = config.scrappers;
}
const self = this;
Q.async(function* (){
for( var part in scrappers ){
data[part] = yield self._getData($,scrappers[part]);
}
cb(data);
})()
.catch((err)=>{
cb(null,err);
});
});
}
/**
* we are working with cheerio, so we want to make sure we get it, either by loading a webpage html, or the developer can provide us with a cheerio or some other interface that support the jQuery api.
* developers can also provide cheerio object which is already fitered to a specific element, for example they only want to run query from an article tag.
* @param config
* @param cb
* @private
*/
_getContext(config,cb){
if( config.$ ){
cb(config.$);
return;
}
if( config.loader ){
config.loader(config.url, (html)=>{
if( html && typeof html === 'string' ){
let $ = cheerio.load(html);
cb($);
return;
}
cb();
return;
});
}
else if( config.html ){
let $ = cheerio.load(config.html);
cb($);
return;
}
else {
if( !this._validUrl(config.url) ){
cb( null, {message: "Invalid URL"});
return;
}
let options = this._getOptions(config.url);
request(options, (error, response, html)=>{
if( error ){
cb(null, error);
return;
}
let $ = cheerio.load(html);
cb($);
});
}
}
_extractValue($element,option,cb) {
if( typeof option.valueFn === 'function' ) {
option.valueFn($element, (value)=>{
cb(value);
});
}
else {
let value;
switch( option.dataType ){
case 'text':
value = $element.text();
break;
case 'attr':
value = $element.attr(option.attrName);
break;
default:
value = $element.html();
break;
}
cb(value);
}
}
_getValuesFromMatch(match, option, cb){
//console.log("_getValuesFromMatch: ", match.length );
if( !match || !match.length ) {
cb();
return;
}
var value,matchIndex, self = this;
//if we only want one element from the match, we need to know which one.
var i = -1;
if( option.onlyOne ) {
matchIndex = 0;
if( option.whichOne === 'first'){
matchIndex = 0;
}
else if( option.whichOne === 'last' ){
matchIndex = match.length-1;
}
else if( typeof option.whichOne === 'number' ){
matchIndex = option.whichOne;
}
i = matchIndex-1;
}
//todo: replace with generators
(function next(){
i++;
//we reached the end and found nothing...
if( i === match.length ){
cb(value);
return;
}
if( option.onlyOne && i !== matchIndex ){
cb(value);
return;
}
self._extractValue(match.eq(i), option, (extractedValue)=>{
//console.log("_extractValue return ",extractedValue );
if( extractedValue === null || extractedValue === undefined ){
next();
return;
}
self._filter(option.filter, extractedValue, match.eq(i), (pass)=>{
if( pass ){
if( option.onlyOne ){
cb(extractedValue);
return;
}
if( !value ){
value = [extractedValue];
}
else{
value.push(extractedValue);
}
}
next();
});
});
})();
}
/**
*
* @param $
* @param options
* {
* selector - css selctor to find an element/s
* dataType - html, text, attr
* attrName - in case data type is attr, this tell us which attribute to get
* onlyOne - if true and the selector finds more than one, we only get one.
* whichOne - "first","last" or number for the index. if this is not provided, we assume the first.
* }
*
* @returns {*|promise}
* @private
*/
_getData($,options){
if( typeof options === 'string' ){
options = [{selector:options, dataType:"text"}];
}
if( !(options instanceof Array) ){
options = [options];
}
var retVal = null;
var deferred = Q.defer();
var self = this;
var match;
//var continueToNext = false;
var i = -1;
//todo: replace with generators
(function next(){
i++;
if( i === options.length ){
// we reached the end
deferred.resolve(retVal);
return;
}
const option = options[i];
if( typeof option.selector !== 'string' || !option.selector.length ){
next();
return;
}
match = $(option.selector);
self._getValuesFromMatch(match, option, (res)=>{
if( res ){
if( option.next ){
if( !retVal ){
retVal = res;
}
else{
if( !(retVal instanceof Array) ){
retVal = [retVal];
}
retVal = retVal.concat(res);
}
next();
}
else{
if( retVal ){
if( !(retVal instanceof Array) ){
retVal = [retVal];
}
retVal = retVal.concat(res);
}
else{
retVal = res;
}
deferred.resolve(retVal);
}
}
else{
next();
}
});
})();
return deferred.promise;
}
_filter(filter, value,$element, cb){
if( !filter ){
cb(true);
return;
}
if( typeof filter.filterValue === 'function' ){
filter.filterFn(value, cb);
return;
}
if( typeof filter.filterElement === 'function' ){
filter.filterElement($element, cb);
return;
}
if( filter.regex ){
for( var i=0; i<filter.regex.length; i++ ){
try{
let regex = new RegExp(filter.regex[i]);
if( !regex.test(value) ){
//console.log("failed ", value);
cb(false);
return;
}
}
catch(err){
console.log("exception in regex: ", err);
}
}
}
cb(true);
}
_getOptions(url){
return {
url: url,
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language':'en-US,en;q=0.8,de;q=0.6,he;q=0.4',
'Cache-Control' : 'no-cache',
'Pragma':'no-cache'
}
};
}
/**
* validating the url.
* @param url
* @returns {boolean}
* @private
*/
_validUrl(url){
//todo: this is a very simple expression and should be replaced with a more robust one that support non english urls
return /^(http(s)?:\/\/.)(www\.)?[\S]{2,256}\.[\S]{2,256}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/.test(url);
}
}
module.exports = new Scrappy();