This repository has been archived by the owner on Aug 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrap.js
133 lines (112 loc) · 4.7 KB
/
scrap.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
module.exports = scrap;
var events = require('events');
var eventEmitter = new events.EventEmitter();
const https = require('https');
var fs = require('fs');
var ended = 0;
var pocet = 0;
var vsetky = [];
var cats = [];
var stare = [];
if (fs.existsSync("./vsetky.json")) {
stare = JSON.parse(fs.readFileSync("./vsetky.json"));
}
console.log(Array.isArray(stare));
function scrap(){
if (!fs.existsSync("./cashe")){
fs.mkdirSync("./cashe");
}
https.get('https://dsystem.synapse5.com/katalog-dortu/?kategorie=19', (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
var reg = /<a href="(.*?)">([\s\S]*?)<\/a>/g;
var regLink = /<font[\s\S]*?><strong>([\s\S]*?)<\/strong><\/font>/
var result;
var cont;
while ((result = reg.exec(data)) !== null) {
if((cont = regLink.exec(result[2])) !== null){
childSite(result[1], cont[1], ()=>{ended++});
pocet++;
}
}
wait()
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}
scrap.event = eventEmitter;
function end() {
fs.readdir("./cashe", (err, files) => {
var a = [];
files.forEach(file => {
a.push( fs.readFileSync("./cashe/" + file));
});
a.forEach((file) => {
var parsed = JSON.parse(file);
parsed.forEach((jedenKolac) => {
vsetky.push(jedenKolac)
})
})
fs.writeFileSync("./vsetky.json", JSON.stringify(vsetky));
fs.writeFileSync("./pattern.json", JSON.stringify({"category": cats}));
})
console.log("scrapping finished");
eventEmitter.emit("loaded", {"category": cats}, vsetky);
}
function wait() {
if(ended<pocet){
var waiTout = setTimeout(wait, 1000);
}
else end();
}
function childSite(link, catname, callback){
cats.push(catname);
https.get(link, (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
var reg = /<div class="wrapper[\s\S]*?">([\s\S]*?)<\/div>[\s\S]*?<!-- Site footer -->/g;
var row = /<ul[\s\S]*?>([\s\S]*?)<\/ul>/g;
//var col = /<li[\s\S]*?>([\s\S]*?)<\/li>/g;
var col = /<div[\s\S]*?>[\s\S]*?<div[\s\S]*?>[\s\S]*?<div[\s\S]*?>[\s\S]*?<div[\s\S]*?>[\s\S]*?<img[\s\S]*?src="([\s\S]*?)"[\s\S]*?<\/div>[\s\S]*?[\s\S]*?<div[\s\S]*?>[\s\S]*?<h2>([\s\S]*?)<\/h2>[\s\S]*?<p>([\s\S]*?) g<\/p>[\s\S]*?<p>([\s\S]*?)<\/p>[\s\S]*?<\/div>[\s\S]*?<\/div>[\s\S]*?<\/div>[\s\S]*?<\/div>/g;
var all = [];
var result, rows, cols, obsahy;
/*while ((result = reg.exec(data)) !== null) {
while ((rows = row.exec(result)) !== null){*/
console.log(catname)
while ((cols = col.exec(data)) !== null){
//console.log("stuck")
//console.log(cols[1] + " " + cols[2] + " " + cols[3])
var find = stare.find(function(element){
return element.meno == cols[2];
});
if(find){
var obrazok = (cols[1] == find.obrazok)? find.obrazok : cols[1];
var vaha = (cols[3] == find.vaha)? find.vaha : cols[3];
var alergeny = (cols[4] == find.alergeny)? find.alergeny : cols[4];
var touched = (find.touched) ? true : false;
all.push({"obrazok": obrazok, "meno": cols[2], "vaha": vaha, "alergeny": alergeny, "popis": "zatial nieje popis", "cena": find.cena, "category": catname, "touched": touched});
}else{
all.push({"obrazok": cols[1], "meno": cols[2], "vaha": cols[3], "alergeny": cols[4], "popis": "zatial nieje popis", "cena": Math.floor(Math.random()*1000)/100, "category": catname, "touched": false});
}
}
/* }
}*/
console.log(all.length)
fs.writeFile('./cashe/'+catname+'.json', JSON.stringify(all), (err)=>{if(err) throw err;})
callback();
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}