-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathweb.js
347 lines (332 loc) · 11.8 KB
/
web.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
process.title = 'eonbot-scraper-web'
const debug = !(process.env.NODE_ENV === 'production') || false
console.debug = (...args) => debug && console.log(...args)
/**
* Config (read from config.properties)
**/
const config = {
eonbot: {
dir: null,
bin: null
},
web: {
root: null,
port: null
}
}
/**
* Read bot config
**/
const prop = require('properties')
const propOptions = {
path: true,
namespaces: true
}
const configSetup = () => new Promise((resolve, reject) => {
prop.parse(path.join(__dirname, 'config.properties'), propOptions, (err, data) => {
if (!err) {
console.debug('eonbot-scraper-web: Setup: Reading config.properties')
config.eonbot.dir = data.eonbot.dir
config.eonbot.bin = data.eonbot.bin
config.web.root = data.web.root
config.web.port = data.web.port
resolve()
} else {
reject(err)
}
})
})
/**
* Web Server Setup
**/
const express = require('express')
const app = express()
const http = require('http').Server(app)
const io = require('socket.io')(http)
const path = require('path')
const setupWebServer = () => new Promise((resolve, reject) => {
app.use(express.static(path.join(__dirname, config.web.root)))
io.on('connection', function(socket){
console.debug('eonbot-scraper-web: Web Server: A user connected')
socket.on('disconnect', function(){
console.debug('eonbot-scraper-web: Web Server: A user disconnected')
})
})
try {
http.listen(config.web.port, function(){
console.log('eonbot-scraper-web: Setup: Web server listening on *:' + config.web.port)
resolve()
})
} catch (e) {
reject(e)
}
})
const startEonBot = () => new Promise((resolve, reject) => {
const spawn = require('child_process').spawn
const eon = spawn(config.eonbot.bin, {cwd: config.eonbot.dir})
var cycle_start = false
eon.stdout.on('data', function (data) {
var status = {
cycle_completed_in: null,
cycle_number: null,
next_cycle: null,
bot_up_time: null
}
var package = {
date: null,
coin: null,
base: null,
mode: null,
buy_disabled: null,
sell_disabled: null,
auto_cancel_open: null,
exchange: {
last_price: null,
ask_price: null,
bid_price: null,
base_balance: null,
coin_balance: null,
coin_value: null
},
bot: {
buy_price: null,
sell_price: null,
max_amount: null,
candle_period: null,
sma: null,
ema: null,
buy_strategy: null,
sell_strategy: null,
strategy: {
rollercoaster_highest: null,
rollercoaster_lowest: null
},
bagbuster: {
enabled: null,
next_buy_percentage: null,
next_buy_price: null,
next_buy_amount: null,
prev_buy_price: null,
buys_count: null,
loop_active: null
},
trend_watcher: {
enabled: null,
direction: null
}
},
status: [],
error: false,
cycle_start: false
}
const lines = data.toString().split('\r\n')
if (lines[0] === '----------------------') {
cycle_start = true
}
if (lines[0] === '-------------') {
if (cycle_start) {
package.cycle_start = true
cycle_start = false
}
package.date = lines[1]
const pairSplit = lines[2].split('/')
package.coin = pairSplit[1]
package.base = pairSplit[0]
var currentLineIterator = 4
var currentLine = lines[currentLineIterator]
// could be errors next
if (lines[3].indexOf('error') > -1 || lines[3] === 'Pair already has open order(s)') {
package.status = lines[3]
package.error = true
} else {
while (currentLine !== '~~~') {
if (currentLine === '*Pair is in buy mode*') {
package.mode = 'BUY'
}
if (currentLine === '*Pair is in sell mode*') {
package.mode = 'SELL'
}
if (currentLine === 'Bot buying for this pair is disabled') {
package.buy_disabled = true
}
if (currentLine === 'Bot selling for this pair is disabled') {
package.sell_disabled = true
}
if (currentLine === 'Bot open orders auto cancelling is activated') {
package.auto_cancel_open = true
}
currentLine = lines[currentLineIterator++]
}
if (currentLine === '~~~') {
currentLine = lines[currentLineIterator++]
if (currentLine === 'Exchange data:') {
// Exchange data
currentLine = lines[currentLineIterator++]
while (currentLine !== '~~~') {
const lineParts = currentLine.split(/ - ([\w\s]+)(?:\(in (\w+)\))?\: ([\d\.]+)/)
if (lineParts.length === 5) {
if (lineParts[1] === 'Last price ') {
package.exchange.last_price = lineParts[3]
}
if (lineParts[1] === 'Ask price ') {
package.exchange.ask_price = lineParts[3]
}
if (lineParts[1] === 'Bid price ') {
package.exchange.bid_price = lineParts[3]
}
if (lineParts[1] === package.coin + ' balance') {
package.exchange.coin_balance = lineParts[3]
}
if (lineParts[1] === package.base + ' balance') {
package.exchange.base_balance = lineParts[3]
}
if (lineParts[1] === 'Value ') {
package.exchange.coin_value = lineParts[3]
}
}
currentLine = lines[currentLineIterator++]
}
}
// Bot data
while (currentLine !== '~~~~~') {
const lineParts = currentLine.split(/ - ([\w\s]+)\: ([\d\.]+m?|\w+)/)
if (lineParts.length === 4) {
if (lineParts[1] === 'Buy price') {
package.bot.buy_price = lineParts[2]
}
if (lineParts[1] === 'Average buy price') {
package.bot.avg_buy_price = lineParts[2]
}
if (lineParts[1] === 'Sell price') {
package.bot.sell_price = lineParts[2]
}
if (lineParts[1] === 'Max amount of ' + package.base + ' to spend') {
package.bot.max_amount = lineParts[2]
}
if (lineParts[1] === 'Candle Period') {
package.bot.candle_period = lineParts[2]
}
if (lineParts[1] === 'SMA') {
package.bot.sma = lineParts[2]
}
if (lineParts[1] === 'EMA') {
package.bot.ema = lineParts[2]
}
if (lineParts[1] === 'Buy strategy') {
package.bot.buy_strategy = lineParts[2]
}
if (lineParts[1] === 'Sell strategy') {
package.bot.sell_strategy = lineParts[2]
}
if (lineParts[1] === 'RollerCoaster highest point') {
package.bot.strategy.rollercoaster_highest = lineParts[2]
}
if (lineParts[1] === 'RollerCoaster lowest point') {
package.bot.strategy.rollercoaster_lowest = lineParts[2]
}
}
// BagBuster
if (currentLine === ' - BagBuster is active') {
package.bot.bagbuster.enabled = true
currentLine = lines[currentLineIterator++]
while (currentLine.indexOf('BagBuster') > -1) {
const bagBusterLineParts = currentLine.split(/ - ([\w\s]+)\: ([\d\.]+m?|\w+)/)
if (bagBusterLineParts[1] === 'Previous BagBuster buy price') {
package.bot.bagbuster.prev_buy_price = bagBusterLineParts[2]
}
if (bagBusterLineParts[1] === 'BagBuster buys count since last sell') {
package.bot.bagbuster.buys_count = bagBusterLineParts[2]
}
if (currentLine.indexOf(' - Next BagBuster buy: after ') === 0) {
const buyPctMatches = currentLine.match(/[\d\.]+%/)
package.bot.bagbuster.next_buy_percentage = buyPctMatches[0]
}
if (currentLine.indexOf(' - Next BagBuster buy price:') === 0) {
const buyPriceMatches = currentLine.match(/[\d\.]+/)
package.bot.bagbuster.next_buy_price = buyPriceMatches[0]
}
if (currentLine.indexOf(' - Next BagBuster buy amount:') === 0) {
const buyAmountMatches = currentLine.match(/[\d\.]+%/)
package.bot.bagbuster.next_buy_amount = buyAmountMatches[0]
}
if (currentLine.indexOf(' - BagBuster levels loop is active') === 0) {
package.bot.bagbuster.loop_active = true
}
currentLine = lines[currentLineIterator++]
}
currentLine = lines[currentLineIterator--]
}
// Trends
if (currentLine === ' - Trends watcher is active') {
package.bot.trend_watcher.enabled = true
currentLine = lines[currentLineIterator++]
while (currentLine.indexOf(' - Trend') === 0) {
if (currentLine.indexOf(' - Trend: ') === 0) {
package.bot.trend_watcher.direction = currentLine.substring(' - Trend: '.length)
}
currentLine = lines[currentLineIterator++]
}
currentLine = lines[currentLineIterator--]
}
currentLine = lines[currentLineIterator++]
}
currentLine = lines[currentLineIterator++]
// if (currentLine !== 'No trades') {
var status = []
while (currentLine !== '-------------') {
status.push(currentLine)
currentLine = lines[currentLineIterator++]
}
package.status = status
// }
}
}
// We've reached the end of the coin's chunk
if (currentLine === '-------------') {
io.emit('cycle_chunk', package)
}
// But we can still scrape status
if (currentLine.indexOf('Cycle completed in') === 0) {
while (currentLine !== '----------------------') {
if (currentLine.indexOf('Cycle completed in') === 0) {
const completedMatchesTime = currentLine.match(/[\d\.]+m?s?/)
status.cycle_completed_in = completedMatchesTime[0]
}
const completedMatches = currentLine.match(/Cycle (\d+) completed, starting cycle (\d+)/)
if (completedMatches && completedMatches.length === 3) {
status.cycle_number = completedMatches[1]
}
if (currentLine.indexOf('Next cycle will start in') === 0) {
const nextCycleMatches = currentLine.match(/[\d]+s/)
status.next_cycle = nextCycleMatches[0]
}
if (currentLine.indexOf('Bot up time:') === 0) {
const botUpTimeMatches = currentLine.match(/[\d]+s/)
status.bot_up_time = botUpTimeMatches[0]
}
currentLine = lines[currentLineIterator++]
}
io.emit('cycle_status', status)
}
}
// Pass all output through to the user
console.log(data.toString())
})
eon.stderr.on('data', function (data) {
console.log('stderr: ' + data.toString())
reject(data.toString())
})
eon.on('exit', function (code) {
console.log('child process exited with code ' + code.toString())
reject('exited')
process.exit(1)
})
resolve()
})
configSetup()
.then(() => setupWebServer())
.then(() => startEonBot())
.catch((reason) => {
console.log('Exiting: ', reason)
process.exit(1)
})