forked from dobizz/awbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawbot.py
661 lines (526 loc) · 23.6 KB
/
awbot.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
import sys
import os
import time
import random
import pathlib
import http.client as httplib
from itertools import count
from plyer import notification
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium_stealth import stealth
from webdriver_manager.chrome import ChromeDriverManager
from awapi import Account
path = os.path.dirname(__file__)
# initialize loop count
loop_count = 0
# initialize mine loop count
mine_loop_count = 0
# variable as condition to exit script
exit_sc = False
sell_file = False
sign_file = True
def _print_(text: str) -> None:
sys.stdout.write(text)
sys.stdout.flush()
def main():
global path, exit_sc, loop_count, mine_loop_count, sell_file, sign_file
# clear terminal
os.system("cls" if os.name == "nt" else "clear")
# check internet connection
conn = httplib.HTTPConnection("1.1.1.1", timeout = 10)
try:
print("Internet check.")
conn.request("HEAD", "/")
conn.close()
print("Internet available.")
except KeyboardInterrupt:
print("\nStopping bot.")
exit_sc = True
return
except:
conn.close()
print("No Internet. Re-checking in 30 seconds.")
for x in range(30):
time.sleep(1)
_print_(".")
return
# define game url
url = "https://play.alienworlds.io"
# wax signin page
signin = "https://all-access.wax.io/"
# alcor to sell tlm
sell = "https://wax.alcor.exchange/trade/tlm-alien.worlds_wax-eosio.token"
# for account.txt file
try:
# check for account.txt file
if os.path.exists("account.txt"):
# check for file size & read
if os.stat("account.txt").st_size != 0:
afile = open("account.txt", "r")
wallet = afile.read().rstrip("\n")
afile.close()
# delete the file if found empty account.txt file
else:
print("\nDeleting corrupted \"account.txt\".")
os.remove("account.txt")
print("File deleted.")
print("Restarting bot.")
return
# create account.txt file if not found
else:
anfile = open("account.txt", "w")
anfile.write(input("\nPlease enter your wax wallet address: "))
anfile.close()
print("Restarting bot.")
return
except KeyboardInterrupt:
print("\nStopping bot.")
exit_sc = True
return
except:
print("\nBot encountered an error. Restarting.")
return
# check for sign.file and sell.file file
if os.path.exists("sell.file"):
print("\n\"sell.file\" found.")
headless = False
sell_file = True
elif os.path.exists("sign.file"):
headless = True
sign_file = True
else:
headless = False
sign_file = False
# create AW Account instance
aw = Account(wallet)
print(f"\nWallet address: \"{wallet}\"")
# define range for loop delay
delay_min = 60 # min delay before next loop
delay_max = 300 # max delay before next loop
# define resource threshold
resource_limit = 100 # skip mining if resources are above the set limit
resource_sleep = 30 # sleep for given time if there is not enough resources
chrome_options = Options()
# set Chrome options
chrome_options.add_argument("--log-level=3")
chrome_options.add_argument("--ignore-certificate-errors")
chrome_options.add_argument("--ignore-certificate-errors-spki-list")
chrome_options.add_argument("--ignore-ssl-errors")
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_argument("--mute-audio")
chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
# headless mode when signed in
if headless:
# to open in hidden browser window
chrome_options.add_argument("--headless")
# save current browsing session to make it persistent
pwd = pathlib.Path().absolute()
chrome_options.add_argument(f"--user-data-dir={pwd}\\chrome-data")
# check for latest chromedriver
driver_service = Service()
# instantiate Chrome driver with given Chrome options
try:
driver = webdriver.Chrome(service = driver_service, options = chrome_options)
# instantiate stealth
stealth(driver,
languages=["en-US", "en"],
vendor="Google Inc.",
platform="Win32",
webgl_vendor="Intel Inc.",
renderer="Intel Iris OpenGL Engine",
fix_hairline=True,
)
# save current window handle
main_window = driver.current_window_handle
# change page load timeout
driver.set_page_load_timeout(60)
# make GET request
if headless:
driver.get(url)
else:
if sell_file:
driver.get(sell)
driver.set_window_position(0, 0)
input("\nPress any key when selling of \"TLM\" tokens is done.")
os.remove("sell.file")
print("\"sell.file\" deleted.")
print("Restarting bot.")
return
else:
driver.get(signin)
driver.set_window_position(0, 0)
print(f"\nSuccessfully loaded \"{driver.title}\".")
except TypeError:
print("\nPlease update your selenium package.")
exit_sc = True
return
except KeyboardInterrupt:
print("\nStopping bot.")
exit_sc = True
return
except:
print("\nBot encountered an error. Restarting.")
return
try:
# check for sign.file file
if sign_file:
print("\nStarting bot in 3 seconds. \n\t- Delete \"sign.file\" in case of re-login. \n\t- Press \"Ctrl + C\" to stop")
for x in range(3):
time.sleep(1)
_print_(".")
# create sign.file if not found
else:
driver.execute_script("""
var wallet = arguments[0];
document.title = wallet;
alert(wallet);
""", wallet)
print("\nPausing bot.")
notification.notify(title = os.path.basename(path) + "\\" + os.path.basename(__file__), message = "Sign-in required.")
input("Please sign-in, then press any key to continue.")
open("sign.file", "a").close()
print("Restarting bot.")
return
except KeyboardInterrupt:
print("\nStopping bot.")
exit_sc = True
driver.quit()
return
except:
print("\nBot encountered an error. Restarting.")
return
# load tlm balance
tlm_old = aw.tlm_balance
# initialize tlm_sum
tlm_sum = 0
# activation switch for checking tlm mined per click
i = False
# main bot loop
for y in count(1):
loop_count += 1
# clear terminal
os.system("cls" if os.name == "nt" else "clear")
print(f"Wallet address: \"{wallet}\"")
try:
# fetch cpu usage details
cpu_usage = aw.cpu_usage
cpu_max = cpu_usage["max"]
cpu_used = cpu_usage["used"]
cpu_pct = int(cpu_used / cpu_max * 100)
# fetch ram usage details
ram_usage = aw.ram_usage
ram_max = ram_usage["max"]
ram_used = ram_usage["used"]
ram_pct = int(ram_used / ram_max * 100)
# fetch net usage details
net_usage = aw.net_usage
net_max = net_usage["max"]
net_used = net_usage["used"]
net_pct = int(net_used / net_max * 100)
print("\nCPU: [ {:,} / {:,} ms ]\t\t\tUsed: {} %".format(cpu_used, cpu_max, cpu_pct))
print("NET: [ {:,} / {:,} B ]\t\t\tUsed: {} %".format(net_used, net_max, net_pct))
print("RAM: [ {:,} / {:,} B ]\t\t\tUsed: {} %".format(ram_used, ram_max, ram_pct))
# show balances
print(f"\nWAX Balance: {aw.wax_balance:.4f}")
try:
print(f"TLM Balance: {aw.tlm_balance:.4f}")
except:
print("Unable to retrieve the TLM value.")
tlm_new = aw.tlm_balance
# show tlm mined per click
if i and tlm_old < tlm_new:
try:
# to find the value of tlm mined
tlm_mined = tlm_new - tlm_old
print(f"\nTLM mined in last claim: {tlm_mined:.4f}")
tlm_old = tlm_new
# to find average rate of tlm mining
tlm_sum += tlm_mined
avg = tlm_sum/mine_loop_count
print(f"Average rate for TLM mining: {avg:.4f}/claim")
# to show sum of tlm mined
print(f"Total TLM mined in this session: {tlm_sum:.4f}")
except KeyboardInterrupt:
print("\nStopping bot.")
exit_sc = True
driver.quit()
return
except:
print("\nUnable to show the value(s).")
# check for throttle.txt file
if os.path.exists("throttle.txt"):
# check for file size & read
if os.stat("throttle.txt").st_size != 0:
tfile = open("throttle.txt", "r")
throttle = tfile.read().rstrip("\n")
tfile.close()
if throttle == "Y" or throttle == "y":
# resource utilization throttling
if (cpu_pct > resource_limit) or (ram_pct > resource_limit) or (net_pct > resource_limit):
print(f"\nResource utilization is above the set threshold of {resource_limit} %.")
print(f"Sleeping for {resource_sleep} seconds.")
for x in range(resource_sleep):
time.sleep(1)
_print_(".")
continue
elif throttle == "N" or throttle == "n":
print("\nResource utilization throttling is OFF. Turn ON by changing value to \"Y\" in the \"throttle.txt\" file.")
# delete the file if value found other than Y or N
else:
print("\nAppropriate input not found.")
print("Deleting corrupted \"throttle.txt\".")
os.remove("throttle.txt")
print("File deleted.")
print("Restarting bot.")
driver.quit()
return
# delete the file if found empty throttle.txt file
else:
print("\nDeleting corrupted \"throttle.txt\".")
os.remove("throttle.txt")
print("File deleted.")
print("Restarting bot.")
driver.quit()
return
# create throttle.txt file if not found
else:
ntfile = open("throttle.txt", "w")
ntfile.write(input("\nDo you want to enable the bot resource(s) throttling [Y/N]: "))
ntfile.close()
driver.quit()
return
# wait for mine button to be found
print("\nSearching for \"Start Now\" button.")
while True:
# try to search for "start now" button
try:
start_btn = WebDriverWait(driver, 5).until(ec.visibility_of_element_located((By.XPATH, "//*[starts-with(text(), 'Start')]")))
except KeyboardInterrupt:
print("\nStopping bot.")
exit_sc = True
driver.quit()
return
# if button is not found
except:
print('\n\tUnable to load or find \"Start Now\" button.')
break
# if button is found, then click
else:
print("Found \"Start Now\" button!")
# full page screenshot
# total_width = driver.execute_script("return document.body.offsetWidth")
# total_height = driver.execute_script("return document.body.scrollHeight")
# driver.set_window_size(total_width, total_height)
# driver.save_screenshot("sc_main_mine.png") # image will be saved as "sc_main_mine.png" in the bot's directory
start_btn.click()
break
# wait for mine button to be found
print("\nSearching for \"Mine\" button.")
while True:
# try to find mine button
try:
mine_btn = WebDriverWait(driver, 30).until(ec.visibility_of_element_located((By.XPATH, "//*[starts-with(text(), 'Mine')]")))
except KeyboardInterrupt:
print("\nStopping bot.")
exit_sc = True
driver.quit()
return
# if button is not found
except:
print('\n\tUnable to load or find \"Mine\" button.')
break
# if button is found, then click
else:
print("Found \"Mine\" button!")
# full page screenshot
# total_width = driver.execute_script("return document.body.offsetWidth")
# total_height = driver.execute_script("return document.body.scrollHeight")
# driver.set_window_size(total_width, total_height)
# driver.save_screenshot("sc_main_mine.png") # image will be saved as "sc_main_mine.png" in the bot's directory
mine_btn.click()
break
# wait for claim button
print("\nSearching for \"Claim\" button.")
try:
claim_btn = WebDriverWait(driver, 15).until(ec.visibility_of_element_located((By.XPATH, "//*[starts-with(text(), 'Claim')]")))
print("Found \"Claim\" button!")
# full page screenshot
# total_width = driver.execute_script("return document.body.offsetWidth")
# total_height = driver.execute_script("return document.body.scrollHeight")
# driver.set_window_size(total_width, total_height)
# driver.save_screenshot("sc_main_claim.png") # image will be saved as "sc_main_claim.png" in the bot's directory
# click claim button
claim_btn.click()
except KeyboardInterrupt:
print("\nStopping bot.")
exit_sc = True
driver.quit()
return
except:
print('\n\tUnable to load or find \"Claim\" button. Restarting.')
driver.quit()
return
# wait for pop-up window
print("\n\tSearching for pop-up window.")
# to continue below while loop
switch = False
while True:
if switch:
break
else:
this_window = None
# switch control to pop-up window
for this_window in driver.window_handles:
if this_window != main_window:
try:
print("\tSwitching to pop-up window.")
driver.switch_to.window(this_window)
print(f"\tSwitched successfully to \"{driver.title}\".")
except KeyboardInterrupt:
print("\n\tStopping bot.")
exit_sc = True
driver.quit()
return
except:
print("\n\tBot encountered an error. Restarting.")
driver.quit()
return
# to exit while loop
switch = True
break
# for pop-up window click(s)
try:
# wait for approve button to be visible & click button
btn = WebDriverWait(driver, 30).until(ec.visibility_of_element_located((By.XPATH, "//*[contains(text(), 'Approve')]")))
# if button found then it'll be clicked
if btn:
print("\n\tFound \"Approve\" button!")
# full page screenshot
# total_width = driver.execute_script("return document.body.offsetWidth")
# total_height = driver.execute_script("return document.body.scrollHeight")
# driver.set_window_size(total_width, total_height)
# driver.save_screenshot("sc_popup_approve.png") # image will be saved as "sc_popup_approve.png" in the bot's directory
btn.click()
print("\tApproving transaction.")
mine_loop_count += 1
except KeyboardInterrupt:
print("\n\tStopping bot.")
exit_sc = True
driver.quit()
return
except:
try:
# wait for cancel button to be visible & click button
btn_can = WebDriverWait(driver, 5).until(ec.visibility_of_element_located((By.XPATH, "//*[contains(text(), 'Cancel')]")))
# if cancel button found then it'll be clicked
if btn_can:
print("\n\tFound \"Cancel\" button!")
# full page screenshot
# total_width = driver.execute_script("return document.body.offsetWidth")
# total_height = driver.execute_script("return document.body.scrollHeight")
# driver.set_window_size(total_width, total_height)
# driver.save_screenshot("sc_popup_can.png") # image will be saved as "sc_popup_can.png" in the bot's directory
btn_can.click()
print("\tCancelling transaction.")
except KeyboardInterrupt:
print("\n\tStopping bot.")
exit_sc = True
driver.quit()
return
except:
try:
# wait for login button to be visible
btn_login = WebDriverWait(driver, 5).until(ec.visibility_of_element_located((By.XPATH, "//*[contains(text(), 'Login')]")))
# if login button found then print message & restart
if btn_login:
# full page screenshot
# total_width = driver.execute_script("return document.body.offsetWidth")
# total_height = driver.execute_script("return document.body.scrollHeight")
# driver.set_window_size(total_width, total_height)
# driver.save_screenshot("sc_popup_login.png") # image will be saved as "sc_popup_login.png" in the bot's directory
os.remove("sign.file")
print("\n\tRestarting bot.")
driver.quit()
return
except KeyboardInterrupt:
print("\n\tStopping bot.")
exit_sc = True
driver.quit()
return
except:
print('\n\tUnable to load or find button(s). Restarting.')
driver.quit()
return
# go control back to main window
print("\n\tSwitching back to main window.")
driver.switch_to.window(main_window)
print(f"\tSwitched successfully to \"{driver.title}\".")
try:
WebDriverWait(driver, 5).until(ec.visibility_of_element_located((By.XPATH, "//*[contains(text(), 'Nothing')]")))
notification.notify(title = os.path.basename(path) + "\\" + os.path.basename(__file__), message = "Nothing to be mined!")
print("\n\tNothing to be mined!")
time.sleep(5)
except KeyboardInterrupt:
print("\nStopping bot.")
exit_sc = True
driver.quit()
return
except:
pass
else:
continue
# full page screenshot
# total_width = driver.execute_script("return document.body.offsetWidth")
# total_height = driver.execute_script("return document.body.scrollHeight")
# driver.set_window_size(total_width, total_height)
# driver.save_screenshot("sc_main.png") # image will be saved as "sc_main.png" in the bot's directory
# show the number of loops done
print(f"\nTotal number of execution(s): {loop_count}")
# show the number of loops done
print(f"Total number of approval(s): {mine_loop_count}")
i = True
# delay the bot before starting next loop
delay = random.randint(delay_min, delay_max)
print(f"\nSleeping for {delay} seconds.")
for x in range(delay):
time.sleep(1)
_print_(".")
print("Done sleeping.")
except KeyboardInterrupt:
print("\nStopping bot.")
exit_sc = True
driver.quit()
return
# if any error occured
except:
print("\nBot encountered an error. Restarting.")
driver.quit()
return
# close the webdriver
driver.quit()
while True:
assert sys.version_info >= (3, 6), "Python 3.6+ required."
try:
if not exit_sc:
# call main routine
main()
else:
# notification for termination
notification.notify(title = os.path.basename(path) + "\\" + os.path.basename(__file__), message = "Script terminated.")
print("\nScript terminated.")
os._exit(0)
except KeyboardInterrupt:
# notification for termination
notification.notify(title = os.path.basename(path) + "\\" + os.path.basename(__file__), message = "Script terminated.")
print("\nScript terminated.")
os._exit(0)
except:
# notification for termination
notification.notify(title = os.path.basename(path) + "\\" + os.path.basename(__file__), message = "Script unable to restart.")
print("\nScript cannot be restarted due to an unknown error.")
os._exit(0)