introduce a class to start arbitrary gist on google colab
need to fix stop timermaster
parent
f19d20f725
commit
6b7d6d3161
Binary file not shown.
Binary file not shown.
|
@ -21,11 +21,11 @@ def clickButton(browser, by, selector, timeout=5):
|
|||
WebDriverWait(browser, timeout).until(EC.element_to_be_clickable((by, selector))).click()
|
||||
time.sleep(1)
|
||||
|
||||
def inputText(browser, by, selector, text, timeout=5):
|
||||
def inputText(browser, by, selector, text, timeout=5, interval=0.2):
|
||||
element = WebDriverWait(browser, timeout).until(EC.element_to_be_clickable((by, selector)))
|
||||
for i in text:
|
||||
element.send_keys(i)
|
||||
time.sleep(0.25)
|
||||
time.sleep(interval)
|
||||
|
||||
# A lifesaver: https://stackoverflow.com/questions/36141681/does-anybody-know-how-to-identify-shadow-dom-web-elements-using-selenium-webdriv
|
||||
def select_shadow_element_by_css_selector(browser, selector):
|
||||
|
|
|
@ -0,0 +1,132 @@
|
|||
import browser_manager
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.common.by import By
|
||||
import time
|
||||
from threading import Thread
|
||||
|
||||
class ColabGist(Thread):
|
||||
|
||||
'''
|
||||
Account is a tuple of (user, pass)
|
||||
Min is for how long the gist needs to be executed
|
||||
Backend is the backend needed for this gist ('N': None, 'T': TPU, 'G': GPU)
|
||||
For future-proofness each Gist is started in its own thread, this makes possible to run multiple gists in different threads at the same time
|
||||
'''
|
||||
|
||||
def __init__(self, url, account, minutes=40, backend='N', proxy=False):
|
||||
self.url = url
|
||||
self.account = account
|
||||
self.minutes = minutes
|
||||
self.backend = backend
|
||||
self.proxy = proxy
|
||||
|
||||
def run(self):
|
||||
self.run_colab()
|
||||
|
||||
def run_colab(self):
|
||||
self.start_browser()
|
||||
self.start_session()
|
||||
|
||||
def sign_in(self):
|
||||
try:
|
||||
self.driver.get("https://accounts.google.com/o/oauth2/v2/auth/oauthchooseaccount?redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&prompt=consent&response_type=code&client_id=407408718192.apps.googleusercontent.com&scope=email&access_type=offline&flowName=GeneralOAuthFlow") #need a fallback for when
|
||||
browser_manager.inputText(self.driver, By.CSS_SELECTOR, "#identifierId", self.account[0] + "\n")
|
||||
time.sleep(1.5)
|
||||
browser_manager.inputText(self.driver, By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input', self.account[1] + "\n")
|
||||
time.sleep(1.5)
|
||||
try:
|
||||
browser_manager.clickButton(self.driver, By.XPATH, '//*[@id="accept"]')
|
||||
time.sleep(1.5)
|
||||
browser_manager.clickButton(self.driver, By.XPATH, '//*[@id="yDmH0d"]/c-wiz[2]/c-wiz/div/div[1]/div/div/div/div[2]/div[3]/div/div[2]/div')
|
||||
time.sleep(1.5)
|
||||
except:
|
||||
pass
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
# Run the desired Colab notebook. Logging into Google account by profile is mandatory
|
||||
def start_session(self):
|
||||
if not self.sign_in():
|
||||
print("Got the old login screen, trying again")
|
||||
self.driver.quit()
|
||||
self.run_colab()
|
||||
return
|
||||
|
||||
self.driver.get(self.url)
|
||||
|
||||
browser_manager.clickButton(self.driver, By.CSS_SELECTOR, "#runtime-menu-button")
|
||||
|
||||
browser_manager.clickButton(self.driver, By.XPATH, '//*[@id=":24"]')
|
||||
time.sleep(1)
|
||||
browser_manager.clickButton(self.driver, By.CSS_SELECTOR, "#accelerator")
|
||||
time.sleep(1.5)
|
||||
acc = self.driver.find_element_by_css_selector("#accelerator")
|
||||
time.sleep(1.5)
|
||||
acc.send_keys(self.backend)
|
||||
acc.send_keys("\n")
|
||||
time.sleep(1.5)
|
||||
browser_manager.clickButton(self.driver, By.CSS_SELECTOR, "#ok")
|
||||
time.sleep(1.5)
|
||||
|
||||
browser_manager.clickButton(self.driver, By.CSS_SELECTOR, "#runtime-menu-button")
|
||||
time.sleep(1.5)
|
||||
browser_manager.clickButton(self.driver, By.XPATH, '//*[@id=":22"]')
|
||||
time.sleep(1.5)
|
||||
browser_manager.clickButton(self.driver, By.CSS_SELECTOR, "#runtime-menu-button")
|
||||
time.sleep(1.5)
|
||||
browser_manager.clickButton(self.driver, By.XPATH, '//*[@id=":1t"]')
|
||||
time.sleep(1.5)
|
||||
|
||||
browser_manager.clickButton(self.driver, By.CSS_SELECTOR, "#ok")
|
||||
time.sleep(1.5)
|
||||
# need a fallback for then it can't connect to the backend
|
||||
|
||||
# Leave the colab be for the specified time
|
||||
starting = time.time()
|
||||
print("Executing the gist for the next {} minutes. Using Backend {}".format(self.minutes, self.backend) )
|
||||
while (time.time() - starting)/60 < self.minutes:
|
||||
if(time.time() - starting > 15):
|
||||
try:
|
||||
if self.driver.find_elements(By.XPATH, "//*[contains(text(),'No backend')]"):
|
||||
print("This account has been blocked :/ try again with another one")
|
||||
self.driver.quit()
|
||||
elif self.driver.find_elements(By.XPATH, "//*[contains(text(),'Cannot connect')]"):
|
||||
print("No backend was available, maybe the service is overflooded or this account/computer is about to be blocked :(")
|
||||
self.driver.quit()
|
||||
return
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
'''char = input()
|
||||
if char == "t":
|
||||
break
|
||||
elif char == "g":
|
||||
elapsed = time()-starting
|
||||
elapsed_min = int(elapsed / 60)
|
||||
elapsed_sec = int(elapsed % 60)
|
||||
print("Elapsed time: {}m{}s".format(elapsed_min, elapsed_sec))
|
||||
else:
|
||||
print("Unrecognized option")'''
|
||||
|
||||
print("Time's up! Closing the browser")
|
||||
|
||||
self.terminate_session()
|
||||
self.stop_browser()
|
||||
|
||||
def terminate_session():
|
||||
# Terminate the session
|
||||
browser_manager.clickButton(self.driver, By.CSS_SELECTOR, "#runtime-menu-button")
|
||||
#factory reset to close session
|
||||
browser_manager.clickButton(self.driver, By.XPATH, '//*[@id=":22"]')
|
||||
|
||||
browser_manager.clickButton(self.driver, By.CSS_SELECTOR, "#ok")
|
||||
# Wait for the session to close
|
||||
time.sleep(60)
|
||||
|
||||
def start_browser(self):
|
||||
self.driver=browser_manager.start_browser(self.proxy)
|
||||
|
||||
def stop_browser(self):
|
||||
# Close browser
|
||||
self.driver.quit()
|
7
main.py
7
main.py
|
@ -1,4 +1,11 @@
|
|||
import ngrok
|
||||
import colab
|
||||
import time
|
||||
|
||||
c = colab.ColabGist('https://colab.research.google.com/gist/EmaMaker/4e1478c9913a2df58fc1b8ff422fa161/proxy.ipynb', ('giangillo.rossi@gmail.com', 'emamaker02'), minutes=3)
|
||||
c.run()
|
||||
|
||||
time.sleep(120)
|
||||
|
||||
n = ngrok.Ngrok()
|
||||
n.start_browser()
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
import subprocess
|
Loading…
Reference in New Issue