-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoddsPicker.js
More file actions
282 lines (252 loc) · 8.25 KB
/
oddsPicker.js
File metadata and controls
282 lines (252 loc) · 8.25 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
var consts = require("./consts.js");
module.exports = {
pickOdds: function(URLs,bookmaker){
var Horseman = require("node-horseman");
//Horseman object with options
var horseman = new Horseman({
ignoreSSLErrors: true,
loadImages: false,
timeout: bookmaker.selectorTimeout === undefined ? 5000 : bookmaker.selectorTimeout,
});
//UserAgent String
var strUserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0";
//Number of URLs opened
var nTabs = URLs.length;
//Variables defined in the global context to handle selectors, tabs and time
var tabCompleted = 0;
var selectors_tested = Array.apply(null, Array(nTabs)).map(Number.prototype.valueOf,0);
var arrayMapper = Array.apply(null, Array(nTabs)).map(Number.prototype.valueOf,0);
for(var i=0; i<nTabs; i++){
arrayMapper[i] = i+1;
}
var startTime = new Date().getTime();
var timeOddsPicking;
var dataFromSelectors = [];
for (var i=0; i<nTabs; i++){
dataFromSelectors[i] = {};
}
var selectorKeys = Object.keys(bookmaker.selectors);
var nSelectors = selectorKeys.length;
horseman
.userAgent(strUserAgent)
.viewport(1920,1080);
function startHorseman(horseman, url, tabNB){
console.log('Opening URL ' + url + ' with bookmaker ' + bookmaker.name);
horseman
.openTab(url).status().then((sta) => {
if (sta != 200) {
console.log('Wrong status!');
}
})
.do(function(){
var sltr = bookmaker.selectors[selectorKeys[selectors_tested[tabNB]]];
startSelectorAnalysis(horseman,sltr,tabNB);
})
.catch(function(err){
console.log("Got into error catcher : " + err);
saveOdds();
tryWithNewInstance();
return horseman.close();
})
}
function loop(){
horseman = new Horseman({
ignoreSSLErrors: true,
loadImages: false,
timeout: 15000,
});
var dataFromSelectors = [];
for (var i=0; i<nTabs; i++){
dataFromSelectors[i] = {};
}
selectors_tested = Array.apply(null, Array(nTabs)).map(Number.prototype.valueOf,0);
arrayMapper = Array.apply(null, Array(nTabs)).map(Number.prototype.valueOf,0);
for(var i=0; i<nTabs; i++){
arrayMapper[i] = i+1;
}
tabCompleted = 0;
currentTab = 0;
startTime = new Date().getTime();
startHorseman(horseman,URLs[currentTab][0],currentTab);
}
//main
var currentTab = 0;
startHorseman(horseman,URLs[currentTab][0],currentTab);
//---------- SIDE FUNCTIONS
//This function is called if one of the tabs URL fail to be loaded.
//Launches horseman with a new instance
function tryWithNewInstance(){
console.log("Trying with a new instance of horseman...")
wait(2000);
horsemanRetry = new Horseman({
ignoreSSLErrors: true,
loadImages: false,
timeout: 15000,
});
var dataFromSelectors = [];
for (var i=0; i<nTabs; i++){
dataFromSelectors[i] = {};
}
selectors_tested = Array.apply(null, Array(nTabs)).map(Number.prototype.valueOf,0);
arrayMapper = Array.apply(null, Array(nTabs)).map(Number.prototype.valueOf,0);
for(var i=0; i<nTabs; i++){
arrayMapper[i] = i+1;
}
tabCompleted = 0;
currentTab = 0;
startTime = new Date().getTime();
startHorseman(horsemanRetry,URLs[currentTab][0],currentTab);
}
//Checks whether all the URL selectors have been processed. If so, closes the tab and opens the following URL.
//Then, checks whether all the tabs have been processed. If so, saves the odds and closes the horseman instance.
function onSelectorProcessed(horseman,numTab) {
selectors_tested[numTab]++;
console.log("Selector completed! vect is now " + selectors_tested);
if (selectors_tested[numTab] == nSelectors) {
console.log('Finished tab number ' + numTab);
shiftTabsNumber(numTab);
console.log('Arraymapper is now ' + arrayMapper);
tabCompleted++;
currentTab++;
if(currentTab < nTabs){
horseman.closeTab(arrayMapper[numTab]);
startHorseman(horseman,URLs[currentTab][0],currentTab);
}
if(tabCompleted == nTabs){
console.log('Closing horseman object and saving odds into DB...');
saveOdds();
return horseman.close();
}
}
else{
startSelectorAnalysis(horseman,bookmaker.selectors[selectorKeys[selectors_tested[numTab]]],numTab);
}
}
function startSelectorAnalysis(horseman,sltr,tabNB){
horseman
.waitForSelector(sltr.name)
.catch(function(err){
console.log("Got into error catcher : " + err);
})
.exists(sltr.name).then((bool) => {
if (bool) {
console.log("Evaluating " + sltr.name + " selector...");
horseman //PMU : do (function(){evaluateNoJQUERY(selector)})??? + options horseman si elles existent
.do(function(){
if(bookmaker.containsJQuery){
evaluateJQuery(horseman,sltr,tabNB);
}
else{
evaluateNoJQuery(horseman,sltr,tabNB);
}
})
}
else{
console.log('Moneyline selector does not exist.');
dataFromSelectors[tabNB][sltr.name] = [];
return onSelectorProcessed(horseman,tabNB);
}
})
}
function evaluateJQuery(horseman,sltr,tabNB){
horseman.evaluate(function(selector){
var sltrData = {
innerText: [],
//ADD OTHER PROPERTIES IF NEEDED HERE AND COMPLETE PUSH BELOW
};
var nElements = $(selector).length;
for(var i=0; i<nElements; i++){
sltrData.innerText.push($(selector)[(i).toString()].innerText);
}
return sltrData;
},sltr.name)
.then((sltrData) => {
timeOddsPicking = new Date().getTime();
dataFromSelectors[tabNB][sltr.dataname] = sltr.fn(sltrData);
return onSelectorProcessed(horseman,tabNB);
})
}
function evaluateNoJQuery(horseman,sltr,tabNB){
horseman.evaluate(function(selector){
var sltrData = {
innerText: [],
//ADD OTHER PROPERTIES IF NEEDED HERE AND COMPLETE PUSH BELOW
};
var nElements = document.querySelectorAll(selector).length;
for(var i=0; i<nElements; i++){
sltrData.innerText.push(document.querySelectorAll(selector)[(i).toString()].innerText);
}
return sltrData;
},sltr.name)
.then((sltrData) => {
timeOddsPicking = new Date().getTime();
dataFromSelectors[tabNB][sltr.dataname] = sltr.fn(sltrData);
return onSelectorProcessed(horseman,tabNB);
})
}
//Handles the horseman tabs number when closing a tab.
function shiftTabsNumber(numTab){
for(var i=numTab+1; i<nTabs;i++ ){
arrayMapper[i]--;
}
}
//The following functions are used to process the lists retrieved from the selectors and save the team names and odds into the DB.
function saveOdds() {
games = [];
for (var j=0; j<nTabs; j++){
var selectorsData = dataFromSelectors[j];
var bookieName = bookmaker.name;
var leagueName = URLs[j][1];
if(consts.threeOutcomes.indexOf(leagueName) > -1){
games = bookmaker.createGameThreeOutcomes(games,selectorsData,leagueName,bookieName,timeOddsPicking);
}
else if(consts.twoOutcomes.indexOf(leagueName) > -1){
games = bookmaker.createGameTwoOutcomes(games,selectorsData,leagueName,bookieName,timeOddsPicking);
}
}
bookmaker.db.insert(games, function (err, newDoc){});
console.log(games);
var endTime = new Date().getTime();
console.log('It took ' + (endTime - startTime) + ' to retrieve these odds');
// Relaunch bookmaker
if((endTime - startTime)<30000){
console.log("Waiting " + (30000 - endTime + startTime) + " milliseconds..." );
setTimeout(function(){
loop();
}, (30000 - endTime + startTime));
}
else{
loop();
}
}
//Function to wait for <ms> milliseconds before executing next line of code.
function wait(ms){
var start = new Date().getTime();
var end = start;
while(end < start + ms) {
end = new Date().getTime();
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
//Parse the bookmaker name or league name from the URL
function parseBookieURL(url) {
var begin = url.search("www.") + 4;
var domain = "";
for (i = begin; url[i] != "."; i++) {
domain += url[i];
}
return domain;
}
function parseLeagueURL(url) {
var league = "";
ind = url.length-1;
while(url[ind] != '/' && ind > -1){
league = url[ind] + league;
ind--;
}
return league;
}
}
}