-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmeb.js
128 lines (123 loc) · 4.08 KB
/
meb.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
var casper = require('casper').create({
pageSettings: {
loadImages: false
},
onResourceRequested: function (C, requestData, request) {
if (requestData.headers[0].value.indexOf('text/css') !== -1 ||
requestData.headers[0].value.indexOf('image/png') !== -1) {
request.abort();
}
},
waitTimeout: 60000
}),
fs = require('fs');
var meb = {
kinds: [
{
param: "1",
file: "resmi_kurumlar.json"
},
{
param: "2",
file: "ozel_kurumlar.json"
}
],
lastDistrictHtml: null,
lastTableHtml: null,
resultObj: [],
provinces: []
};
casper.on('initialized', function (kind) {
meb.resultObj = [];
meb.provinces = casper.evaluate(function () {
var opts = document.querySelector("select[name=ddlil]").options,
provinces = [];
[].forEach.call(opts, function (opt) {
if (opt.value == '999') {
return;
}
provinces.push(opt.value);
});
return provinces;
});
casper.emit('selected', kind);
});
casper.on('selected', function (kind) {
casper.waitFor(function check() {
return meb.lastDistrictHtml !== this.evaluate(function () {
return document.querySelector("select[name=ddlilce]").innerHTML;
});
}, function then() {
meb.lastDistrictHtml = this.evaluate(function () {
return document.querySelector("select[name=ddlilce]").innerHTML;
});
casper.fill('form#Form1', {
'ddlKurumTuru': meb.kinds[kind].param,
'ddlilce': '0'
});
casper.click('.submitbutton');
casper.emit('listed', kind);
});
});
casper.on('listed', function (kind) {
casper.waitFor(function check() {
return meb.lastTableHtml !== casper.evaluate(function () {
return document.querySelector("table.frmlist").innerHTML;
});
}, function then() {
meb.lastTableHtml = casper.evaluate(function () {
return document.querySelector("table.frmlist").innerHTML;
});
var result = this.evaluate(function () {
var jsonObj = [], keys = [], row, trs, tds, value;
trs = document.querySelectorAll("table.frmlist>tbody>tr");
[].forEach.call(trs, function (tr, trIndex) {
tds = tr.querySelectorAll('td');
row = {};
[].forEach.call(tds, function (td, tdIndex) {
value = td.innerHTML.trim();
if (trIndex === 0) {
keys.push(value);
}
else {
row[keys[tdIndex]] = (value === ' ') ? null : value;
}
});
if (trIndex !== 0) {
jsonObj.push(row);
}
});
return jsonObj;
});
meb.resultObj = meb.resultObj.concat(result);
console.log(meb.provinces.shift() + ' ilindeki ' + result.length + ' kurum cekildi. Kalan il :' + meb.provinces.length + '/81');
this.emit('fetched', kind);
});
});
casper.on('fetched', function (kind) {
if (meb.provinces.length < 1) {
this.emit('exported', kind);
return;
}
this.fill('form#Form1', {
'ddlil': meb.provinces[0]
});
this.emit('selected', kind);
});
casper.on('exported', function (kind) {
targetFile = fs.absolute(meb.kinds[kind].file);
targetFile = casper.filter('page.target_filename', targetFile) || targetFile;
try {
fs.write(targetFile, JSON.stringify(meb.resultObj), 'w');
console.log(meb.kinds[kind].file + ' dosyasina ' + meb.resultObj.length + " kurum kaydedildi.")
} catch (err) {
console.log(meb.kinds[kind].file + 'olusturulamadi.');
}
if (kind === 0) {
this.emit('initialized', 1);
}
});
casper.start('https://mebbis.meb.gov.tr/kurumlistesi.aspx', function () {
this.emit('initialized', 0);
});
casper.run();