-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (140 loc) · 6.38 KB
/
index.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require('dotenv').config();
const moment = require("moment");
const fsPromises = require('fs').promises;
const path = require('path');
const { Telegraf } = require('telegraf');
// const bot = new Telegraf(process.env.TEST_BOT_TOKEN);
const bot = new Telegraf(process.env.BOT_TOKEN);
// prod db
const db = require('./util/db');
// local db
// const db = require('./util/db-local');
const getDataFromBetexplorer = require('./main/main');
const getResultsFromBEandCalculateTotals = require('./main/results');
const helper = require('./main/helper');
const tgChannelId_TEST = process.env.TEST_CHANNEL_ID;
const tgChannelId = process.env.CHANNEL_ID;
const pathToResLastUpdateFile = path.join(
path.dirname(process.mainModule.filename),
'util',
'resupdate.txt'
);
getDataFromBetexplorer(formBetsAndSendMessageToBot);
const delay = 1000 * 60 * 10;
setInterval(() => {
sendResults();
console.log(moment().format());
getDataFromBetexplorer(formBetsAndSendMessageToBot);
try {
bot.telegram.sendMessage(tgChannelId_TEST, 'alive');
} catch (error) {
console.log('alive ping error', error);
}
}, delay);
function sendResults() {
if (moment().hours() >= 11) {
fsPromises.readFile(pathToResLastUpdateFile, {encoding: 'utf8'})
.then(resUpdateDate => {
if (!moment(resUpdateDate).isSame(moment(), 'day')) {
console.log(resUpdateDate);
getResultsFromBEandCalculateTotals()
.then((res) => {
if (!res) {
return;
}
res = res[0];
const results = res[0];
if (results) {
console.log('results', results);
const finres = (results.finres - results.betsSum).toFixed(2);
let ROI = (finres*100)/results.bets;
ROI = ROI.toFixed(2);
const resultsMessage = `${results.bets} bets made for ${results.betsSum}% , Won: ${results.won_bets}, Lost: ${results.bets - results.won_bets}, Result: ${finres}%, ROI: ${ROI}%`;
sendMessageToBot(resultsMessage).then(() => {
fsPromises.writeFile(pathToResLastUpdateFile, moment().format(), {encoding: 'utf8'});
})
.catch(error => console.log(error));
};
})
.catch(error => console.log(error));
}
})
.catch(error => console.log(error));
}
}
function formBetsAndSendMessageToBot(matchDate, matchTime, matchName, matchClicks, matchOdds, matchId, matchLink) {
const totalClicks = matchClicks.reduce((previousValue, currentValue) => {
return previousValue + currentValue;
});
if (totalClicks < 50) {
return;
}
const [homeOdds, drawOdds, awayOdds] = matchOdds;
const [homeClicks, drawClicks, awayClicks] = matchClicks;
if (totalClicks >= 50 && homeOdds*(homeClicks/totalClicks) > 1.4 && homeOdds < 2.6) {
const bet = 'AWAY';
const oddsPosition = helper.getBetPosition(bet);
return helper.getPinnacleOddsFromBE(matchId, matchLink, oddsPosition)
.then(odds => {
const betMessage = `${matchDate} ${matchTime} * ${matchName} * AWAY WIN * @${odds || awayOdds} * 1%`;
insertBetsInDB(matchId, matchDate, matchTime, matchName, matchLink, bet, 1, odds || awayOdds)
.then(([res]) => {
if (res.affectedRows > 0) {
sendMessageToBot(betMessage);
}
})
.catch(err => console.log('DB save error (home):', err));;
});
}
if (totalClicks >= 100 && awayOdds*(awayClicks/totalClicks) > 1) {
if (homeOdds > 3.0) {
const bet = 'HOME';
const oddsPosition = helper.getBetPosition(bet);
return helper.getPinnacleOddsFromBE(matchId, matchLink, oddsPosition)
.then(odds => {
const betMessage = `${matchDate} ${matchTime} * ${matchName} * HOME WIN * @${odds || homeOdds} * 1%`;
insertBetsInDB(matchId, matchDate, matchTime, matchName, matchLink, bet, 1, odds || homeOdds)
.then(([res]) => {
if (res.affectedRows > 0) {
sendMessageToBot(betMessage);
}
})
.catch(err => console.log('DB save error (away):', err));;
});
} else {
const bet = 'DRAW';
const oddsPosition = helper.getBetPosition(bet);
return helper.getPinnacleOddsFromBE(matchId, matchLink, oddsPosition)
.then(odds => {
const betMessage = `${matchDate} ${matchTime} * ${matchName} * DRAW * @${odds || drawOdds} * 0.5%`;
insertBetsInDB(matchId, matchDate, matchTime, matchName, matchLink, bet, 0.5, odds || drawOdds)
.then(([res]) => {
if (res.affectedRows > 0) {
sendMessageToBot(betMessage);
}
})
.catch(err => console.log('DB save error (draw):', err));;
});
}
}
}
function insertBetsInDB
(id, date, time, match, link, bet, sum, odds) {
// const oddsPosition = helper.getBetPosition(bet);
return db.execute(
`INSERT IGNORE INTO bets_kedlo VALUES (?, ?, ?, ?, ?, ?, ?, null, null, ?, null, null, null)`,
[id, date, time, match, link, bet, sum, odds]
);
// return helper.getPinnacleOddsFromBE(id, link, oddsPosition)
// .then(odds => {
// return db.execute(
// `INSERT IGNORE INTO bets_kedlo VALUES (?, ?, ?, ?, ?, ?, ?, null, null, ?, null, null, null)`,
// [id, date, time, match, link, bet, sum, odds]
// );
// });
}
function sendMessageToBot(message) {
// for production cgange to tgChannelId !!!
// return bot.telegram.sendMessage(tgChannelId_TEST, message);
return bot.telegram.sendMessage(tgChannelId, message);
}