forked from andrehrf/hawk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractor.js
323 lines (266 loc) · 11.8 KB
/
extractor.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
/**
* Extrator do Hawk
*
* @author André Ferreira <[email protected]>
*/
var cheerio = require("cheerio");
php = require("phpjs");
module.exports = function($, filter, debug){
debug = (debug) ? debug : false;
var debug_log = {};
var extraction = {};
for(var key in filter.extracts){
var m1 = php.microtime(true);
var namespace = parse_key(filter.extracts[key].namespace);
var requered = filter.extracts[key].requered | false;
var removehtml = filter.extracts[key].removehtml | false;
var firstelement = filter.extracts[key].firstelement | false;
var converthtmlentitiesdecode = filter.extracts[key].htmlentitiesdecode | false;
var removelinebreakandtabs = filter.extracts[key].removelinebreakandtabs | false;
extraction[namespace] = [];
if($(filter.extracts[key].query).length <= 0 && requered && !debug){
extraction = null;
break;
}
else if($(filter.extracts[key].query).length <= 0 && requered && debug){
extraction["error"] = "Requered item "+key+" not valid value";
}
(function(key){
$(filter.extracts[key].query).each(function(){
switch($(this).prop('tagName')){
case "A": value = $(this).html(); break;
case "INPUT": value = $(this).val(); break;
case "IMG": value = $(this).attr("src"); break;
default: value = $(this).html(); break;
}
if(value != undefined && value != null && value != NaN){
if(typeof filter.extracts[key].maskregex == "string" && filter.extracts[key].maskregex != "" && filter.extracts[key].maskregex != null){
var reg = new RegExp(filter.extracts[key].maskregex, "gmi");
if(typeof value == "string"){
m = value.match(reg);
value = m;
}
else if(typeof value == "array" || typeof value == "object"){
for(var key2 in value){
if(typeof value[key2] == "string"){
m = value[key2].match(reg);
value[key2] = m;
}
else{
for(var key3 in value[key2]){
if(typeof value[key2][key3] == "string"){
m = value[key2][key3].match(reg);
value[key2][key3] = m;
}
}
}
}
}
}
if(value !== null && value !== undefined){
switch(filter.extracts[key].type){
case "int":
if(typeof value === "object" || typeof value === "array")
value = value[0];
value.replace(/(\d{1,})/i, function(){
value = parseInt(arguments[1]);
});
break;
case "float":
if(typeof value === "object" || typeof value === "array")
value = value[0];
value.replace(/(\d{1,}.\d{1,})/i, function(){
value = parseFloat(arguments[1]);
});
break;
case "date": value = new Date(value).toDateString(); break;
case "time": value = new Date(value).toTimeString(); break;
case "datetime": value = new Date(value).toGMTString(); break;
case "table": value = maptable(value); break;
case "img": value = getattr(value, "src"); break;
case "imagegallery": value = mapimages(value); break;
case "currency":
value = value.replace(/(,|\.)+/gmi, "");//Removendo . e ,
value.replace(/(\d{1,})/i, function(){
value = arguments[1];
value = (value.substring(0, value.length-2) + "." + value.substring(value.length-2, value.length));
value = parseFloat(value);
});
if(isNaN(value))
value = null;
break;
case "linklist":
value = maplinks(value, filter.extracts[key].linkremovequery);
if(filter.extracts[key].linkunique && (typeof value == "object" || typeof value == "array"))
value = array_unique(value);
break;
case "link":
if(filter.extracts[key].linkremovequery){
var parse = value.split("?");
value = parse[0];
}
break;
}
}
var m2 = php.microtime(true);
if(removehtml){
value = replaceInReturn(value, function(v){
return php.strip_tags(v);
});
}
if(converthtmlentitiesdecode){
value = replaceInReturn(value, function(v){
return php.html_entity_decode(v);
});
}
if(removelinebreakandtabs){
value = replaceInReturn(value, function(v){
return v.replace(/(\r\n|\n|\r|\t)/gm, "");
});
}
//Correção de trim
value = replaceInReturn(value, function(v){
return v.replace(/ +(?= )/g,'');
});
var m3 = php.microtime(true);
debug_log[namespace] = {e: m2-m1, f: m3-m2, t: m3-m1}
if((typeof value === "object" || typeof value === "number" || typeof value === "string") && value !== null && value !== undefined)
if(!firstelement || (extraction[namespace].length <= 0 && firstelement))
extraction[namespace].push(value);
}
});
})(key);
}
if(debug){
var total = 0;
for(var key in debug_log)
total += debug_log[key].t
debug_log["total"] = total;
extraction["debug"] = debug_log;
}
return extraction;
};
function parse_key(key){
//key = php.utf8_encode(key);
key = removerAcentos(key);
key = key.replace(/(\r\n|\n|\r|\t)/gm, "");//Removendo espaçamentos e tabs
key = php.html_entity_decode(key);//Removendo HTML Entity
key = php.strip_tags(key);//Removendo qualquer HTML que possa ter
key = key.replace(/(,|\.|\(|\))/gmi, "");//Removendo . e ,
key = key.replace(/(\s|\/|\\)/ig, "-");//Alterando espaços \ e / para -
key = key.replace(/\u0000/g, "");//Removendo caracter \u0000
return key.toLowerCase();
}
function replaceInReturn(value, cb){
if(typeof value == "string"){
value = cb(value);
}
else if(typeof value == "array" || typeof value == "object"){
for(var key2 in value){
if(typeof value[key2] == "string"){
value[key2] = cb(value[key2]);
}
else{
for(var key3 in value[key2])
if(typeof value[key2][key3] == "string")
value[key2][key3] = cb(value[key2][key3]);
}
}
}
return value;
}
function getattr(html, atribute){
var $ = cheerio.load(html);
return $("*").attr(atribute);
}
function maptable(html){
html = html.trim();
var r = {};
var count = 0;
var $ = cheerio.load(html);
$("tr").each(function(){
var value = [];
var key = ($("th", $(this)).length > 0) ? $("th", this).html().trim() : count;
if(typeof key === "string")
key = parse_key(key);
if($("td", this).length > 0){
$("td", this).each(function(){
value.push($(this).html().trim());
});
}
r[key] = value;
count++;
});
//console.log($(html).find("dt").length);
//$(html).find("dl").each(function(){
var value = [];
/*if($("dt", this).length <= 1){
var key = ($("dt", this).length > 0) ? $("dt", this).html().trim() : count;
if(typeof key === "string")
key = parse_key(key);
if($("dd", this).length > 0){
$("dd", this).each(function(){
value.push($(this).html().trim());
});
}
r[parse_key(key)] = value;
}
else if($("dt", this).length > 1){//Casos que o programador cotoco colocou vários DT no mesmo DL*/
var keys = [];
var values = [];
$("dt", html).each(function(){
keys.push($(this).html().trim());
});
$("dd", html).each(function(){
values.push($(this).html().trim());
});
for(var key in keys)
r[parse_key(keys[key])] = values[key];
//}
count++;
//});
return r;
}
function mapimages(html){
var r = [];
var $ = cheerio.load(html);
$("img").each(function(){
var src = $(this).attr("src");
if(src != null && src != undefined)
r.push(src);
});
return r;
}
function maplinks(html, linkremovequery){
var r = [];
var $ = cheerio.load(html);
$("a").each(function(){
var href = $(this).attr("href");
if(linkremovequery){
var parse = href.split("?");
href = parse[0];
}
if(href != null && href != undefined)
r.push(href);
});
return r;
}
/**
* @see https://gist.github.com/marioluan/6923123
* @see http://www.ogenial.com.br/javascript-funcao-remover-acentos
*/
function removerAcentos( strToReplace ) {
strToReplace = strToReplace.replace(/&#(\w+);/gi, function(match, dec){
var mask = {
"xe0": "a", "xe1": "a", "xe2": "a", "xe3": "a", "xe4": "a", "xe5": "a", "xe6": "a",
"xe8": "e", "xe9": "e", "xea": "e", "xeb": "e",
"xec": "i", "xed": "i", "xee": "i", "xef": "i",
"xf2": "o", "xf3": "o", "xf4": "o", "xf5": "o", "xf6": "o",
"xf9": "u", "xfa": "u", "xfb": "u", "xfc": "u",
"xe7": "c",
"xf1": "n"
}
return (mask[dec.toLowerCase()] != undefined && mask[dec.toLowerCase()] != "undefined") ? mask[dec.toLowerCase()] : "&"+dec+";";
});
return strToReplace;
}