-
Notifications
You must be signed in to change notification settings - Fork 1
/
gunbot_logs_to_telegram.py
151 lines (127 loc) · 4.63 KB
/
gunbot_logs_to_telegram.py
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
from watchdog.events import PatternMatchingEventHandler
from watchdog.observers.polling import PollingObserver as Observer
import time
import Queue
import telegram
import threading
import os
telegram_bot_token = "" # put in the bot token
telegram_your_id = "" # put in your chat id
folder_to_watch = "" # Your GUNBOT Folder or parent folder from multiple GUNBOTS e.g "C:\Users\Myself\Desktop\GUNBOT_3" ; leave blank for directory where this file is.
if folder_to_watch == "":
folder_to_watch = os.path.dirname(os.path.abspath(__file__))
class MyHandler(PatternMatchingEventHandler):
def __init__(self, queue):
super(MyHandler, self).__init__()
self.queue = queue
patterns = ["*-log.txt"]
def process(self, event):
if event.is_directory is False and event.event_type in ["modified", "created", "moved"]:
# print("Event in %s detected" % event.src_path)
# line = subprocess.check_output(['tail', '-1', event.src_path])
# print(line)
with open(event.src_path, 'r') as f:
last_lines = tail(f)
for line in last_lines:
if "MARKET CALLBACK" in line:
data = get_attributes_from_logfilename(event.src_path)
self.queue.put((line, data))
def on_modified(self, event):
self.process(event)
def on_created(self, event):
self.process(event)
def watch_folder(folder, queue):
observer = Observer()
observer.schedule(MyHandler(queue), path=folder, recursive=True)
observer.start()
try:
while True:
observer.join()
time.sleep(1)
except:
pass
def bot_message_loop(telegram_bot_token, telegram_your_id):
global queue
messages_so_far = set()
bot = telegram.Bot(token=telegram_bot_token)
bot.send_message(chat_id=telegram_your_id, text="Bot started now!")
while True:
message, data = queue.get()
if message not in messages_so_far:
messages_so_far.add(message)
print("Sending message %s" % message + "\nEstimate Total: %s %s\nExchange: %s" % (
str(message_to_total_estimate(message)), data["primary_pair"], data["exchange"]))
bot = telegram.Bot(token=telegram_bot_token)
bot.send_message(chat_id=telegram_your_id, text=message + "\nEstimate Total: %s %s\nExchange: %s" % (
str(message_to_total_estimate(message)), data["primary_pair"], data["exchange"]))
def tail(f, window=25):
"""
Returns the last `window` lines of file `f` as a list.
"""
if window == 0:
return []
BUFSIZ = 1024
f.seek(0, 2)
bytes = f.tell()
size = window + 1
block = -1
data = []
while size > 0 and bytes > 0:
if bytes - BUFSIZ > 0:
# Seek back one whole BUFSIZ
f.seek(block * BUFSIZ, 2)
# read BUFFER
data.insert(0, f.read(BUFSIZ))
else:
# file too small, start from begining
f.seek(0, 0)
# only read what was not read
data.insert(0, f.read(bytes))
linesFound = data[0].count('\n')
size -= linesFound
bytes -= BUFSIZ
block -= 1
return ''.join(data).splitlines()[-window:]
def isfloat(value):
try:
float(value)
return True
except ValueError:
return False
def message_to_total_estimate(message):
words = message.split()
counter = 0
result = 1
for word in words:
if isfloat(word) is True:
counter += 1
result = result * float(word)
if counter == 2:
return result
else:
return -1
def get_attributes_from_logfilename(path):
filename = os.path.basename(path)
data = dict()
data["primary_pair"] = ""
data["secondary_pair"] = ""
data["exchange"] = ""
split_list = filename.split("-")
if len(split_list) < 2:
return data
split_pairs = split_list[1].split("_")
if split_pairs < 1:
return data
data["primary_pair"] = split_pairs[0]
data["secondary_pair"] = split_pairs[1]
data["exchange"] = split_list[0]
return data
def main():
global queue
queue = Queue.Queue()
message_thread = threading.Thread(target=bot_message_loop,
kwargs={"telegram_bot_token": telegram_bot_token, "telegram_your_id": telegram_your_id})
message_thread.start()
watch_folder(folder_to_watch, queue)
if __name__ == "__main__":
main()