forked from scotow/bk-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcasper-generator.js
67 lines (54 loc) · 1.69 KB
/
casper-generator.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
// Setup
var MAX_ATTEMPTS = 50;
var attempts = 0;
var SURVEY_CODES = [
22365, // Auchan Leers - Leers
23911, // Auchan v2 - Villeneuve d'Ascq
21109, // Euralille - Lille
19974, // Gare Saint-Lazare - Paris
24191, // Zone Commerciale Grand Tour 2 - Sainte-Eulalie
];
// Utils
function pad(num, size) {
var it = String(num);
while (it.length < size) it = '0' + it;
return it;
}
function randomElement(array) {
return array[Math.floor(Math.random() * array.length)];
}
// Main
var casper = require('casper').create();
casper.start('https://www.bkvousecoute.fr');
casper.then(function() {
this.click('input[type=submit]');
});
casper.then(function() {
var date = new Date();
var day = date.getUTCDate();
var month = date.getUTCMonth() + 1; //months from 1-12
var year = date.getUTCFullYear();
this.fillSelectors('#surveyEntryForm', {
'#SurveyCode': String(randomElement(SURVEY_CODES)),
'#InputDay': String(day),
'#InputMonth': pad(month, 2),
'#InputYear': String(year).slice(-2),
'#InputHour': String(12 + Math.floor(Math.random() * 3)),
'#InputMinute': String(10 + Math.floor(Math.random() * 25))
}, false);
this.click('input[type=submit]');
});
casper.continueUntilCode = function() {
attempts++;
if(attempts >= MAX_ATTEMPTS) this.die('Too many attempts.', 1);
this.then(function() {
if(this.getTitle() === 'Mon expérience BK - Merci') {
this.echo(this.fetchText('.ValCode').split(':').reverse()[0].trim());
} else {
this.click('#NextButton');
this.continueUntilCode();
}
});
};
casper.continueUntilCode();
casper.run();