-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcontrollers.py
321 lines (277 loc) · 11.8 KB
/
controllers.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
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
import sys
import requests
import json
import datetime
import logging
import pyautogui
import os
import pathlib
import datetime
import time
import yaml
import telegram
import pandas as pd
from pywinauto import Desktop
def read_configurations():
'''
Function to read the configuration from .yaml file
'''
with open(os.path.join(os.path.sep, pathlib.Path(__file__).parent.resolve(), "config.yaml"), 'r', encoding='utf8') as s:
stream = s.read()
return yaml.safe_load(stream)
try:
streamConfig = read_configurations()
telegram_integration = streamConfig['telegram_options']['telegram_integration']
telegram_pic_integration = streamConfig['telegram_options']['telegram_pic_integration']
telegram_token = streamConfig['telegram_options']['telegram_token']
telegram_chatid = streamConfig['telegram_options']['telegram_chatid']
create_logfiles = streamConfig['bot_options']['create_logfiles']
delete_old_logfiles = streamConfig['bot_options']['delete_old_logfiles']
delete_old_folders = streamConfig['bot_options']['delete_old_folders']
multiaccount_names = streamConfig['bot_options']['multiaccount_names']
enable_multiaccount = streamConfig['bot_options']['enable_multiaccount']
create_bat = streamConfig['bot_options']['create_bat_file']
except FileNotFoundError:
print('Error: config.yaml file not found, make sure config.yaml are placed in the folder..')
exit()
async def delete_folders():
'''
Function do delete old folders created from this bot
'''
if delete_old_folders != False:
# Path to log folder
rootdir = os.path.join(os.path.sep, pathlib.Path(__file__).parent.resolve())
# Today's date
todays_date = datetime.date.today()
for folder in os.walk(rootdir):
folder_name = pathlib.Path(folder[0]).name
try:
if datetime.datetime.strptime(folder_name, '%Y-%m-%d').date() != todays_date:
os.rmdir(str(os.path.join(folder[0])))
print('%s folder deleted!\n' % folder_name)
pass
except Exception as e:
#print("Exception: " + str(e))
pass
async def delete_log_files():
'''
Function do delete old log files from the log folder.
'''
if delete_old_logfiles != False:
# Path to log folder
filesPath = os.path.join(os.path.sep, pathlib.Path(__file__).parent.resolve(), 'logs')
# Today's date
todays_date = datetime.date.today()
# For file in folder
for file in pathlib.Path(filesPath).glob('*'):
if file.is_file():
# If file has the extension .log
if file.suffix == '.log':
file_dt = datetime.datetime.strptime(file.stem, '%Y-%m-%d').date()
# If file is different from today's date
if file_dt != todays_date:
os.remove(str(file.absolute()))
print('Log file %s deleted from folder!\n' % file.stem)
pass
def start_telegram():
'''
Function to start telegram
'''
if telegram_integration != False:
if telegram_token is None:
print('Telegram token not found on config.yaml file...')
else:
TelegramBot = telegram.Bot(token=telegram_token)
return TelegramBot
else:
print('Telegram integration not enabled on config.yaml file...')
def get_telegram_chat_id():
'''
Function to get telegram chat id from telegram token
It's not possible to retrieve chat id, if telegram token is None
'''
try:
if telegram_chatid is None:
if telegram_token is None:
print('Telegram token not found on config.yaml file...')
else:
url = 'https://api.telegram.org/bot' + str(telegram_token) + '/getUpdates'
response = requests.request("GET", url)
telegram_response = json.loads(response.text)
chat_id = telegram_response['result'][0]['message']['chat']['id']
return chat_id
except:
print('Telegram chat id not found! Make sure to send at least any message to your bot on Telegram, so the endpoint from API might work corectly! Exiting bot..')
os._exit(0)
def send_telegram_msg(message, bot_name=''):
'''
Function to send Telegram message
'''
if telegram_integration != False:
if telegram_chatid is None:
chat_id = get_telegram_chat_id()
else:
chat_id = telegram_chatid
if bot_name != '':
TelegramBot = start_telegram()
TelegramBot.send_message(text='Bot (' + str(bot_name) + '): ' + message, chat_id=chat_id)
else:
pass
# Function to send Telegram pictures
def send_telegram_pic(image):
'''
Function to send Telegram pictures
'''
if telegram_pic_integration != False:
if telegram_chatid is None:
chat_id = get_telegram_chat_id()
else:
chat_id = telegram_chatid
try:
TelegramBot = start_telegram()
TelegramBot.send_photo(chat_id=chat_id, photo=open(image, 'rb'))
except:
pass
def take_screenshot(folder='', sub_folder='', info = ''):
'''
Function to take a screenshot and save the file wherever you want.
'''
if folder != '':
path = os.path.join(os.path.sep, pathlib.Path(__file__).parent.resolve(), 'static', 'img', folder)
else:
path = os.path.join(os.path.sep, pathlib.Path(__file__).parent.resolve(), 'static', 'img')
if not os.path.exists(path):
os.mkdir(path)
if sub_folder != '':
path = os.path.join(path,sub_folder)
if not os.path.exists(path):
os.mkdir(path)
path = os.path.join(path, time.strftime("%Y-%m-%d"))
if not os.path.exists(path):
os.mkdir(path)
if info != '':
info = f"_{info}"
# File name
file = time.strftime(f"%Y-%m-%d-%H-%M-%S{info}.jpg")
path_file = os.path.join(path, file)
try:
# Save screenshot
pyautogui.screenshot(path_file)
except:
pass
return path_file
async def initialize_pyautogui():
'''
Function to initialize pyautogui library.
Reference: https://pyautogui.readthedocs.io/en/latest/quickstart.html
'''
# Initialized PyAutoGUI
# https://pyautogui.readthedocs.io/en/latest/introduction.html
# When fail-safe mode is True, moving the mouse to the upper-left corner will abort your program.
pyautogui.FAILSAFE = False
pyautogui.PAUSE = 1
# Define the logging level and the file name
def setup_logger(telegram_integration=False, bot_name=''):
'''
Function to log the steps you need.
You can define the logging level and the file name.
To setup as many loggers as you want.
'''
if not os.path.exists(os.path.join(os.path.sep, pathlib.Path(__file__).parent.resolve(), 'logs')):
os.mkdir(os.path.join(os.path.sep, pathlib.Path(__file__).parent.resolve(), 'logs'))
filename = os.path.join(os.path.sep, pathlib.Path(__file__).parent.resolve(), 'logs', str(datetime.date.today()) + '.log')
if bot_name != '':
formatter = logging.Formatter('%(levelname)s | Function: %(funcName)s | %(asctime)s: Bot (' + str(bot_name) + '): %(message)s', datefmt='%m/%d/%Y %H:%M:%S')
else:
formatter = logging.Formatter('%(levelname)s | Function: %(funcName)s | %(asctime)s: %(message)s', datefmt='%m/%d/%Y %H:%M:%S')
level = logging.INFO
if create_logfiles != False:
handler = logging.FileHandler(filename, 'a')
handler.setFormatter(formatter)
consolehandler = logging.StreamHandler(sys.stdout)
consolehandler.setFormatter(formatter)
if telegram_integration != False and telegram_token is not None:
class TelegramHandler(logging.Handler):
def emit(self, record):
message = self.format(record)
try:
send_telegram_msg(message, bot_name)
# send_telegram_msg(message, record.levelno) # Passing level
# send_telegram_msg(message, record.levelname) # Passing level name
except:
pass
logger = logging.getLogger('logs')
if logger.hasHandlers():
# Logger is already configured, remove all handlers
logger.handlers = []
logger.setLevel(level)
if create_logfiles != False:
logger.addHandler(handler)
logger.addHandler(consolehandler)
if telegram_integration != False and telegram_token is not None:
telegram_handler = TelegramHandler()
logger.addHandler(telegram_handler)
return logger
def get_browser():
logger = setup_logger(telegram_integration=True)
# Get all profiles from Brave web browser
profiles = multiaccount_names
if enable_multiaccount != False:
logger.info(str("Profiles selected: " + '%s' % ', '.join(map(str, profiles))))
if not profiles:
logger.error("Please type the profile names correctly in the file config.yaml before running this bot! Exiting bot..")
os._exit(0)
if enable_multiaccount != False:
# Get all windows opens
windows = Desktop(backend="uia").windows()
window = [w.window_text() for w in windows]
# Create a dataframe in order to store the windows needed
df_windows = pd.DataFrame(window, columns =['WebBrowser'])
# Filter dataframe only to show all windows from Brave web browser
df_windows = df_windows.loc[df_windows['WebBrowser'].str.contains("Brave", case=False)]
# Add column key to find profile
df_windows['Key'] = df_windows['WebBrowser'].str.replace(' ','').str.strip()
# Add column profile from Brave
df_windows['Profile'] = df_windows['Key'].str.split('Bombcrypto-Brave').str[1].str.replace(':','').str.replace('-','').str.strip()
# Add column about the website open from Brave
df_windows['Website'] = df_windows['WebBrowser'].str.split().str[0].str.strip()
# Filter dataframe only to show all bombcrypto game window
df_windows = df_windows.loc[df_windows['Website'] == 'Bombcrypto']
applications = []
website_browser = []
try:
for profile in df_windows['Profile']:
if profile in profiles:
website = df_windows.loc[df_windows.Profile==profile,'WebBrowser'].values[0]
applications.append([website, profile])
bomb = df_windows.loc[df_windows.Website=='Bombcrypto','Website'].values[0]
website_browser.append(bomb)
except:
pass
return applications, website_browser
else:
applications = [['Bot', 'BCOIN', 'None']]
website_browser = ['Bombcrypto']
return applications, website_browser
async def create_bat_file():
try:
if create_bat != False:
path = os.path.join(os.path.sep, pathlib.Path(__file__).parent.resolve(), 'bot.bat')
with open(path, 'w+') as f:
f.write('cd ' + os.path.join(os.path.sep, pathlib.Path(__file__).parent.resolve()) + '\n')
f.write('python main.py')
f.close()
logger = setup_logger(telegram_integration=True)
logger.info('Bot.bat file created at ' + os.path.join(os.path.sep, pathlib.Path(__file__).parent.resolve()))
except:
pass
async def countdown_timer():
'''
Function to countdown time before start
'''
# Countdown timer
print("Starting Bot", end="", flush=True)
for i in range(0, 5):
print(".", end="", flush=True)
time.sleep(1)
print(" Bot started!\n")