Last active
September 9, 2020 23:28
-
-
Save WitherOrNot/299e254b835c9ed83e7dffbf30482889 to your computer and use it in GitHub Desktop.
Ad-watching bot for https://www.channel-watcher.com/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from selenium import webdriver | |
from selenium.webdriver.firefox.options import Options | |
import timeit | |
import psutil | |
import proxyscrape | |
import requests | |
CHW_URL = "https://www.channel-watcher.com/supplier/520/witherornot" # Change this to your syndicate url | |
coll = proxyscrape.create_collector('default', 'https') | |
driver = None | |
need_proxy = False | |
def killall(proc_name): | |
for p in psutil.process_iter(): | |
try: | |
if p.name().lower().startswith(proc_name.lower()): | |
p.kill() | |
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): | |
pass | |
return False | |
def kill_driver(): | |
global driver | |
if driver: | |
driver.quit() | |
driver = None | |
killall("firefox") | |
def kill_bot(): | |
print("Exiting...") | |
kill_driver() | |
quit() | |
def get_new_proxy(): | |
success = False | |
prox = "" | |
print("Getting new IP...") | |
while not success: | |
try: | |
p = coll.get_proxy({"type": "https", "code": "us"}) | |
prox = p.host+":"+p.port | |
print("Trying "+prox) | |
t = requests.get("https://ipv4.icanhazip.com/", proxies={ | |
"http": "http://"+prox, | |
"https": "https://"+prox | |
}, timeout=5).text | |
print("Our IP is now "+t) | |
success = True | |
except Exception: | |
pass | |
except KeyboardInterrupt: | |
kill_bot() | |
return prox | |
while True: | |
while driver: | |
driver.implicitly_wait(0) | |
try: | |
driver.find_element_by_xpath("/html/body/app-root/app-syndication-control/main/section/div/div/div/h3") | |
kill_driver() | |
print("Rate limited, we need a new IP") | |
need_proxy = True | |
break | |
except KeyboardInterrupt: | |
kill_bot() | |
except Exception: | |
pass | |
try: | |
smiley_button = driver.find_element_by_xpath("/html/body/app-root/app-syndication-control/main/header/div/div/div[3]/ul/li[3]/i") | |
timer = driver.find_element_by_xpath("/html/body/app-root/app-syndication-control/main/header/div/div/div[5]/h3") | |
print(timer.text) | |
if timer.text in ["00 : 03", "00 : 02", "00 : 01"]: | |
print("CLICK!") | |
smiley_button.click() | |
click_wait = timeit.default_timer() | |
except KeyboardInterrupt: | |
kill_bot() | |
except Exception: | |
print("Waiting for something else...") | |
if psutil.virtual_memory().available < 1 * 1024 * 1024 * 1024: | |
print("!!!!!!!MEMORY FAILSAFE TRIGGERED!!!!!!!") | |
print("Aborting and restarting...") | |
kill_driver() | |
if (timeit.default_timer() - click_wait) > 180: | |
print("Went too long w/o clicking, restarting...") | |
kill_driver() | |
try: | |
killall("firefox") | |
capab = webdriver.DesiredCapabilities.FIREFOX | |
capab['marionette'] = True | |
if need_proxy: | |
proxy = get_new_proxy() | |
capab['proxy'] = { | |
"proxyType": "MANUAL", | |
"httpProxy": proxy, | |
"ftpProxy": proxy, | |
"sslProxy": proxy | |
} | |
options = Options() | |
options.headless = True | |
driver = webdriver.Firefox(options=options, capabilities=capab) | |
print("Starting browser session...") | |
driver.set_page_load_timeout(30) | |
driver.get(CHW_URL) | |
driver.implicitly_wait(10) | |
old_window = driver.window_handles[0] | |
driver.find_element_by_xpath("/html/body/app-root/app-mediasyndication/section/div/div/div/div/div/div[1]/button").click() | |
print("Waiting for rating window...") | |
start_window_wait = timeit.default_timer() | |
while len(driver.window_handles) < 2: | |
if (timeit.default_timer() - start_window_wait) > 10: | |
print("Took too long, retrying...") | |
kill_driver() | |
continue | |
pass | |
rating_window = driver.window_handles[1] | |
driver.switch_to.window(rating_window) | |
print("Searching relevant buttons...") | |
click_wait = timeit.default_timer() | |
except Exception: | |
kill_driver() | |
continue | |
except KeyboardInterrupt: | |
kill_bot() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment