-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.js
95 lines (82 loc) · 2.56 KB
/
crawler.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
request = require('./helper/request')
/********************************** main ***************************************/
/* @main */
//input function
//must return a Promise with chaining array of urls
function input(){
return request('https://www.deutsche-digitale-bibliothek.de/sitemaps/index.xml')
.then(xml=>{
return extractElements(xml,'sitemap',['lastmod','loc']);
}).then(list=>{
//filter by lastmod -> @todo:later
return list
}).then(list=>{
return objArrayToArray(list,'loc');
});
}
/* @child */
//must return the parsing result
//do not use console.log inside this function, it is use to pass data between main and child process
function parse(siteData){
//let utf8=siteData.toString('utf8');
//reduce so fast as possible the data overhead in child process
siteData = siteData.replace(new RegExp("https://www.deutsche-digitale-bibliothek.de/item/", 'g'),''); //urls not need
return reduceToRelated(siteData,'loc');//xml tags not need
}
/* @main */
//logic to save the data
function save(data,counter,url){
console.log('Infos:',counter,url);
stringToArray(data);
//to do
}
/********************************** helper ***************************************/
function stringToArray(string){
console.log('string',string,string.length);
string=string.replace(/\n|\s|\'|\[|\]/g,'');
let array = string.split(new RegExp(',', 'g'));
console.log(array.length);
}
function objArrayToArray(array,key){
return array.map(item=>{
return item[key];
});
}
function reduceToRelated(xml,element){
let array=xml.split(new RegExp('</'+element+'>', 'g'));
return array.map( item => {
item=item.split('<'+element+'>')[1];
return item ? item : '';
});
}
function splitAll(xml,element){
return xml.replace(new RegExp('<'+element+'>', 'g'),'').split('</'+element+'>')
}
function splitOne(xml,element){
return xml.split('</'+element+'>')[0].split('<'+element+'>')[1]
}
function convertXmlToJson(xml,filters){
return filters.reduce((json,filter)=>{
json[filter]=splitOne(xml,filter);
return json
},{});
}
function extractElements(xml,element,filters){
let array=splitAll(xml,element);
array.shift(); //remove first
array.pop(); //remove last
return array.map(item=>{
return filters!=undefined ? convertXmlToJson(item,filters) : item ;
});
}
/******************************* export & config ********************************************/
module.exports={
input:input,
save:save,
parse:parse,
config:{ //todo:later @override via npm run parameters in child
REQUEST_TIMEOUT:30000, //ms
TASK:8,
PATH:'' //'https://deutsche-digitale-bibliothek.de'
}
}