-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
160 lines (144 loc) · 5.72 KB
/
Copy pathmain.py
File metadata and controls
160 lines (144 loc) · 5.72 KB
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
from selenium.common.exceptions import SessionNotCreatedException
import utils.logger as lg
import utils.selenium as sl
import utils.profile_lock as pl
import bot.aquaretro as aq
import bot.spn as spn
import utils.timer as tr
import time
import sys
import os
if __name__ == "__main__":
logger = lg.get_logger()
print("""
\033[91m _______ _ __ ___ __
/ __/ _ \\/ |/ / __/ _ )___ / /_
_\\ \\/ ___/ / |/ / _ / _ \\/ __/
/___/_/ /_/|_/|___/____/\\___/\\__/
\033[0mDofus Vote Bot - AquaRetro & SPN
\033[92mVersion 2.1.0\033[0m
""")
if not os.path.exists(".env"):
with open(".env", "w") as f:
f.write(
"SPN_USERNAME=\"username\"\n"
"CAPTCHA_API_KEY=\"2captcha_api_key\"\n"
"\n"
"# Optional: hours where the bot must NOT vote (local time).\n"
"# Example: 23:00 -> 07:00 (crosses midnight).\n"
"# If you don't want to set a sleep window, just leave these empty.\n"
"SLEEP_START=\"23:00\"\n"
"SLEEP_END=\"07:00\"\n"
)
logger.error(".env just created. Please fill it with your configuration and run the bot again.")
time.sleep(10)
sys.exit(1)
sl.get_driver_path()
def wait_for_setup_trigger(timeout_seconds=10):
# Wait up to timeout_seconds for Enter; return True if pressed.
logger.info(f"Press Enter within {timeout_seconds} seconds to enter setup mode (or wait to continue)...")
end = time.time() + timeout_seconds
if os.name == "nt":
try:
import msvcrt
while time.time() < end:
if msvcrt.kbhit():
ch = msvcrt.getwch()
if ch in ("\r", "\n"):
return True
time.sleep(0.1)
return False
except Exception:
pass
# Fallback for other platforms
try:
import select
import sys as _sys
rlist, _, _ = select.select([_sys.stdin], [], [], timeout_seconds)
if rlist:
_sys.stdin.readline()
return True
return False
except Exception:
time.sleep(timeout_seconds)
return False
def ensure_profile_unlocked():
status = pl.get_profile_lock_status(sl.PROFILE_DIR)
pl.print_status(status, logger)
if status.locked_by_process:
logger.process("Profile locked by Chrome. Attempting to stop locking processes...")
pl.kill_chrome_using_profile(sl.PROFILE_DIR)
status = pl.get_profile_lock_status(sl.PROFILE_DIR)
if status.locked_by_process:
logger.error("Profile is still locked after attempting to stop Chrome. Please close Chrome manually and retry.")
time.sleep(10)
sys.exit(1)
if status.lock_files_present:
removed = pl.clean_stale_lock_files(sl.PROFILE_DIR)
if removed:
logger.info(f"Removed stale lock files: {', '.join(removed)}")
if wait_for_setup_trigger():
logger.process("Setup mode triggered. Launching browser...")
ensure_profile_unlocked()
driver = sl.create_driver(headless=False)
try:
input("Press Enter after setting up the browser profile...")
finally:
driver.quit()
logger.success("Setup completed. Continuing normal workflow.")
ensure_profile_unlocked()
logger.info("Verifying browser profile...")
driver = sl.create_driver()
try:
time.sleep(5)
if aq.is_disconnected(driver):
logger.error("User is not logged in. If you haven't set up the browser profile yet, run the bot again with the 'setup' argument first.")
time.sleep(10)
sys.exit(1)
finally:
driver.quit()
logger.success("Browser profile already initialized successfully.")
timer = tr.VoteTimer(interval_minutes=90)
try:
logger.info("Initializing initial time from last vote...")
timer.fetch_time_left()
except SessionNotCreatedException as e:
logger.error(f"Error while starting Selenium driver: {e}")
logger.warning("The profile might be locked by another Chrome instance. Run the bot again with the 'lock' argument to check the profile lock status.")
time.sleep(10)
sys.exit(1)
timer.wait_next_vote(r_max=1)
while True:
logger.info("Starting a new voting cycle...")
driver = sl.create_driver()
time.sleep(3)
logger.info("Checking if user has voted manually in the meantime...")
(h, m, s) = aq.get_vote_time_left(driver)
if (h, m, s) != (0, 0, 0):
logger.warning(f"User voted manually in the meantime. Vote unavailable.")
driver.quit()
timer.fetch_time_left()
timer.wait_next_vote()
continue
else:
logger.info("No manual vote detected. Proceeding with automated voting...")
try:
try:
aq.vote(driver)
except Exception as e:
logger.error(f"Error while voting on AquaRetro: {e}")
continue
try:
spn.vote(driver)
except Exception as e:
logger.error(f"Error while voting on SPN: {e}")
continue
try:
aq.confirm_vote(driver)
except Exception as e:
logger.error(f"Error while confirming AquaRetro vote: {e}")
continue
finally:
driver.quit()
timer.reset()
timer.wait_next_vote()