-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
134 lines (116 loc) · 3.97 KB
/
run.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
const casper = require('casper').create();
const moment = require('moment');
const fs = require('fs');
const BASE_URL = "https://torgi.gov.ru";
const today = moment().format('DD.MM.YYYY');
casper.options.waitTimeout = 45000;
casper.start();
// Ex: casperjs run.js --companyId=1 --categoryId=13 --inn=2312180144
// get cli parameters
const companyId = casper.cli.get('companyId');
const categoryId = casper.cli.get('categoryId');
const inn = casper.cli.get('inn');
if(!companyId) throw new Error("companyId can not be null");
if(!categoryId) throw new Error("categoryId can not be null");
if(!inn) throw new Error("inn can not be null");
// parse auctions for target
var auctions = [];
const target = {
companyId: companyId,
categoryId: categoryId,
inn: inn
};
parse(target);
casper.run(function() {
fs.write('./auctions/auctions_' + inn + '.json', JSON.stringify(auctions), 'w');
console.log("finished");
casper.exit();
});
/**
* Helper functions
*/
/**
* Returns total number of auctions
*/
function getAuctionsCount() {
var text = document.getElementsByTagName('h2')[1].innerText;
text = text.replace('Все: найдено лотов ', '');
return text;
}
/**
* Returns all auctions on current page by category id and company id
*/
function getAuctionsOnPage(categoryId, companyId) {
var results = [];
var rows = document.querySelectorAll('.datarow');
for(i = 0; i < rows.length; i++) {
var columns = rows[i].querySelectorAll('td');
var auction = null;
// Продажа государственного и муниципального имущества
if(categoryId == 8) {
auction = {
itemNumber: columns[2].querySelectorAll('span span')[1].innerText,
messageNumber: columns[2].querySelectorAll('span span')[0].innerText,
about: columns[3].innerText,
startPrice: columns[4].innerText,
location: columns[6].innerText
};
}
// Реализация имущества должников
if(categoryId == 13) {
auction = {
itemNumber: columns[2].querySelectorAll('span span')[1].innerText,
messageNumber: columns[2].querySelectorAll('span span')[0].innerText,
about: columns[3].innerText,
startPrice: columns[4].innerText,
location: columns[5].innerText
};
}
// additional properties
auction['categoryId'] = categoryId;
auction['companyId'] = companyId;
if(!auction) throw new Error("auction can not be null");
results.push(auction);
}
return results;
}
/**
* Parses auctions by target
*/
function parse(target) {
var companyId = target.companyId;
var categoryId = target.categoryId;
var inn = target.inn;
// open search page by category
casper.open(BASE_URL + "/lotSearch1.html?bidKindId=" + categoryId);
// reload
casper.reload();
// click on extended search
casper.then(function() { this.click("#ext_search"); });
// wait for extended search to be loaded
casper.waitForText("Расширенный поиск лотов");
// fill inn and date to form and submit it
casper.then(function() {
this.fill("form.lot-search", {
"extended:bidOrganization:bidOrganizationInn": inn,
"extended:bidNumberExtended:publishDateFrom": today
}, true);
});
// open pages one by one and load auctions
casper.then(function() {
const auctionsCount = this.evaluate(getAuctionsCount);
if(auctionsCount != 0) {
// load auctions on page 1
const pagesCount = Math.ceil(auctionsCount / 10);
auctions = auctions.concat(this.evaluate(getAuctionsOnPage, categoryId, companyId));
// if there is more than 1 page
if(pagesCount > 1) {
for(var pageIndex = 2; pageIndex <= pagesCount; pageIndex++) {
casper.then(function(){ this.click('a[title="Перейти на одну страницу вперед"]'); });
casper.waitUntilVisible('span[title="Перейти на страницу ' + pageIndex + '"] em[style="font-style: normal; color: black;"]');
casper.then(function() { auctions = auctions.concat(this.evaluate(getAuctionsOnPage, categoryId, companyId)); });
}
}
}
});
}