Skip to content

Commit

Permalink
chores: refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
simonfarah authored Mar 4, 2024
1 parent 4f05b6b commit ee383ea
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
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.common.exceptions import (
NoSuchElementException,
ElementNotInteractableException,
)
import random
from selenium.common.exceptions import NoSuchElementException

init(autoreset=True)

Expand All @@ -28,6 +24,7 @@ def __init__(self):
"--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
)
self.driver = webdriver.Chrome(options=options)

print(Fore.GREEN + "[+] Driver loaded successfully")
print()
except Exception as e:
Expand Down Expand Up @@ -71,19 +68,27 @@ def __init__(self):

def start(self):
self.driver.get(self.url)

print(Fore.MAGENTA + "[!] In case of a 502 Bad Gateway error")
print(Fore.MAGENTA + "[!] please refresh the page")
print()

self.wait_for_xpath(self.captcha_xpath)

print(Fore.YELLOW + "[~] Please complete the captcha")

self.wait_for_xpath(self.services["followers"]["xpath"])

print(Fore.GREEN + "[+] Captcha completed successfully")
print()

self.driver.minimize_window()
self.check_services()

for index, service in enumerate(self.services):
title = self.services[service]["title"]
status = self.services[service]["status"]

print(Fore.BLUE + f"[{str(index + 1)}] {title}".ljust(20), status)

while True:
Expand All @@ -100,62 +105,68 @@ def start(self):
def select_service(self, choice):
div = 6 + choice
service_key = list(self.services.keys())[choice - 1]

self.driver.find_element(By.XPATH, self.services[service_key]["xpath"]).click()

print()
video_url = input(Fore.MAGENTA + "[-] Video URL : ")
print()

self.start_service(div, video_url)

def start_service(self, div, video_url):
url_input_xpath = f"/html/body/div[{div}]/div/form/div/input"
search_btn_xpath = f"/html/body/div[{div}]/div/form/div/div/button"
send_btn_xpath = f"/html/body/div[{div}]/div/div/div[1]/div/form/button"

# Ensure the URL is input initially
input_element = self.driver.find_element(By.XPATH, url_input_xpath)
input_element.clear()
input_element.send_keys(video_url)

while True:
# Click the search button
self.driver.find_element(By.XPATH, search_btn_xpath).click()
print("[DEBUG] Clicked search button.")

# Attempt to click the send button if it's present
try:
WebDriverWait(self.driver, 5).until(
EC.element_to_be_clickable((By.XPATH, send_btn_xpath))
).click()
print("[DEBUG] Clicked send button.")
except TimeoutException:
# If the send button isn't found, click the search button again
print("[DEBUG] Send button not found, clicking search button again.")
self.driver.find_element(By.XPATH, search_btn_xpath).click()

# Wait for 2.5 minutes before attempting the next iteration
print("[DEBUG] Waiting for 2.5 minutes before the next iteration.")
sleep(150)
remaining_time = self.check_remaining_time(div)

def check_submit(self, div):
if remaining_time is not None:
print(Fore.YELLOW + f"[~] Sleeping for {remaining_time} seconds")
sleep(remaining_time)

def check_remaining_time(self, div):
remaining_time_xpath = f"/html/body/div[{div}]/div/div/span[1]"

try:
element = self.driver.find_element(By.XPATH, remaining_time_xpath)
text = element.text

if "Please wait" in text:
minutes = text.split("Please wait ")[1].split(" ")[0]
seconds = text.split(" second")[0].split()[-1]
sleep_duration = int(minutes) * 60 + int(seconds) + 5
return sleep_duration, False

return sleep_duration
else:
return 30, False
return None
except NoSuchElementException:
return None, True
return None

def check_services(self):
for service in self.services:
xpath = self.services[service]["xpath"]

try:
element = self.driver.find_element(By.XPATH, xpath)

if element.is_enabled():
self.services[service]["status"] = Fore.GREEN + "[WORKING]"
else:
Expand Down

0 comments on commit ee383ea

Please sign in to comment.