This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
193 lines (163 loc) · 8.38 KB
/
main.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
import time
import threading
import random
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.proxy import Proxy
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import selenium.common.exceptions as selenium_exceptions
from config import config
import logging
import colorlog
handler = colorlog.StreamHandler()
handler.setFormatter(colorlog.ColoredFormatter(
'%(asctime)s - %(log_color)s(%(levelname)s)%(reset)s - %(message)s',
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red,bg_white',
},
datefmt='%Y-%m-%d %H:%M:%S'
))
logger = colorlog.getLogger('selenium-automation')
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
drivers = [] # Keep track of all driver instances
def open_chrome(url, profile_directory, position, size):
global drivers
options = Options()
options.add_argument(f"--user-data-dir={profile_directory}")
options.add_experimental_option("detach", True)
if config["headless"]:
options.add_argument("--headless")
options.add_argument("--mute-audio")
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 17_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) EdgiOS/124.0.2478.50 Version/17.0 Mobile/15E148 Safari/604.1"
options.add_argument(f"user-agent={user_agent}")
# Disable various features to make headless mode less detectable
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
drivers.append(driver) # Add driver to the global list for cleanup
print("..........................")
try:
driver.get(url)
driver.set_window_position(*position)
driver.set_window_size(*size)
attempt = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="__APP"]/div/div[1]/div/div[2]/div[2]/div[3]/div[2]')))
remaining_attempts = int(attempt.text.split('/')[0])
balance = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.bn-flex.Game_entry__info__15l1V > div.Game_entry__coin__33Nan'))).text
logger.info(f"Remaining attempts: {remaining_attempts} and Balance: {balance}")
play_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "Game_entry__playBtn__1Gi2c")))
if play_button:
while True:
attempt = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="__APP"]/div/div[1]/div/div[2]/div[2]/div[3]/div[2]')))
remaining_attempts = int(attempt.text.split('/')[0])
while remaining_attempts > 0:
play_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "Game_entry__playBtn__1Gi2c")))
play_button.click()
logger.info("Clicked Play button.")
logger.info("Game Started.")
canvas = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, 'canvas')))
logger.info("Canvas found. Starting to click...")
end_time = time.time() + 45 # Set end time for 45 seconds
while time.time() < end_time:
try:
# Check if the canvas is still present
canvas = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.TAG_NAME, 'canvas')))
# Perform click action
time.sleep(config["click_delay"])
canvas.click()
time.sleep(config["after_click_delay"])
# Sleep for the specified delay before the next click
except selenium_exceptions.TimeoutException:
logger.warning("Canvas not found, stopping clicks.")
break
logger.info("Game completed,going back to main...")
time.sleep(2)
logger.info("Fetching reward amount...")
reward = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'text-5xl'))).text
if reward:
logger.info(f"Reward for current round: {reward}")
else:
logger.info("Reward not found.")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#__APP > div > div > div > svg'))).click()
# Fetch remaining attempts again after game completes
logger.info("Sleeping for two seconds before fetching attempts...")
time.sleep(2)
attempt = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="__APP"]/div/div[1]/div/div[2]/div[2]/div[3]/div[2]')))
remaining_attempts = int(attempt.text.split('/')[0])
balance = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.bn-flex.Game_entry__info__15l1V > div.Game_entry__coin__33Nan'))).text
logger.info(f"Remaining attempts: {remaining_attempts} and Balance: {balance}")
logger.warning("No more attempts left. Quitting Chrome...")
driver.quit() # Quit the driver when no attempts left
drivers.remove(driver) # Remove from the global list
logger.warning(f"Sleeping for {config['sleep_time']} minutes...")
time.sleep(config['sleep_time'] * 60) # Sleep for the defined period (in seconds)
logger.warning("Relaunching Chrome...")
open_chrome(url, profile_directory, position, size) # Relaunch
else:
logger.warning("Play button not found.")
return
except Exception as e:
logger.error(f"Error: {e}" )
finally:
logger.error("Exiting...")
driver.quit() # Quit the driver when no attempts left
drivers.remove(driver)
time.sleep(60) # Sleep for the defined period (in seconds)
logger.warning("Relaunching Chrome...")
open_chrome(url, profile_directory, position, size)
def launch_profile(url, profile_directory, position, size, delay):
try:
time.sleep(delay)
open_chrome(url, profile_directory, position, size)
except Exception as e:
logger.error(f"Error in launch_profile function: {e}")
try:
url = config["url"]
profiles = []
profile_path_base = config["profile_url"]
window_width = 570
window_height = 700
window_border = 5
window_title_bar = 45
start = 1
end = 2
for i in range(start, end):
row = (i - 1) // 3 % 2
col = (i - 1) % 3
position = (
col * (window_width + window_border),
row * (window_height + window_title_bar)
)
delay = (i - start) * 10 # Calculate delay based on the current iteration
profiles.append((f"{profile_path_base}{i}", position, (window_width, window_height), delay))
threads = []
for profile_directory, position, size, delay in profiles:
thread = threading.Thread(target=launch_profile, args=(url, profile_directory, position, size, delay))
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
except KeyboardInterrupt:
logger.warning("Keyboard Interrupted")
finally:
# Quit all active ChromeDriver instances
for driver in drivers:
try:
driver.quit()
logger.warning("Closed Chrome session.")
except Exception as e:
logger.warning(f"Error closing Chrome session: {e}")
logger.warning("Exiting...")