-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathcolabctl.py
214 lines (187 loc) · 7.45 KB
/
colabctl.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import sys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pickle
import time
import validators
def sleep(seconds):
for i in range(seconds):
try:
time.sleep(1)
except KeyboardInterrupt:
continue
def exists_by_text2(driver, text):
try:
WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.XPATH,"//*[contains(text(), '"+str(text)+"')]")))
except Exception:
return False
return True
def exists_by_xpath(driver, thex, howlong):
try:
WebDriverWait(driver, howlong).until(ec.visibility_of_element_located((By.XPATH, thex)))
except:
return False
def exists_by_text(driver, text):
driver.implicitly_wait(2)
try:
driver.find_element_by_xpath("//*[contains(text(), '"+str(text)+"')]")
except NoSuchElementException:
driver.implicitly_wait(5)
return False
driver.implicitly_wait(5)
return True
def user_logged_in(driver):
try:
driver.find_element_by_xpath('//*[@id="file-type"]')
except NoSuchElementException:
driver.implicitly_wait(5)
return False
driver.implicitly_wait(5)
return True
def wait_for_xpath(driver, x):
while True:
try:
driver.find_element_by_xpath(x)
return True
except:
time.sleep(0.1)
pass
def scroll_to_bottom(driver):
SCROLL_PAUSE_TIME = 0.5
# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
# Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
def file_to_list(filename):
colabs = []
for line in open(filename):
if validators.url(line):
colabs.append(line)
return colabs
def switch_to_tab(driver, tab_index):
print("Switching to tab " + str(tab_index))
try:
driver.switch_to.window(driver.window_handles[tab_index])
except:
print("Error switching tabs.")
return False
def new_tab(driver, url, tab_index):
print("Opening new tab to " + str(url))
try:
driver.execute_script("window.open('" + str(url) + "')")
except:
print("Error opening new tab.")
return False
switch_to_tab(driver, tab_index)
return True
fork = sys.argv[1]
timeout = int(sys.argv[2])
colab_urls = file_to_list('notebooks.csv')
if len(colab_urls) > 0 and validators.url(colab_urls[0]):
colab_1 = colab_urls[0]
else:
raise Exception('No notebooks')
chrome_options = Options()
chrome_options.add_argument('--headless') # uncomment for headless mode
chrome_options.add_argument('--no-sandbox')
#chrome_options.add_argument("user-data-dir=profile") # left for debugging
chrome_options.add_argument('--disable-dev-shm-usage')
wd = webdriver.Chrome('chromedriver', options=chrome_options)
wd.get(colab_1)
try:
for cookie in pickle.load(open("gCookies.pkl", "rb")):
wd.add_cookie(cookie)
except Exception:
pass
wd.get(colab_1)
if exists_by_text(wd, "Sign in"):
print("No auth cookie detected. Please login to Google.")
wd.close()
wd.quit()
chrome_options_gui = Options()
chrome_options_gui.add_argument('--no-sandbox')
#chrome_options.add_argument("user-data-dir=profile") # left for debugging
chrome_options_gui.add_argument('--disable-infobars')
wd = webdriver.Chrome('chromedriver', options=chrome_options_gui)
wd.get("https://accounts.google.com/signin")
wait_for_xpath(wd, '//*[@id="yDmH0d"]/c-wiz/div/div[2]/c-wiz/c-wiz/div/div[4]/div/div/header/div[2]')
print("Login detected. Saving cookie & restarting connection.")
pickle.dump(wd.get_cookies(), open("gCookies.pkl", "wb"))
wd.close()
wd.quit()
wd = webdriver.Chrome('chromedriver', options=chrome_options)
while True:
for colab_url in colab_urls:
complete = False
wd.get(colab_url)
print("Logged in.") # for debugging
running = False
wait_for_xpath(wd, '//*[@id="file-menu-button"]/div/div/div[1]')
print('Notebook loaded.')
sleep(10)
while not exists_by_text(wd, "Sign in"):
if exists_by_text(wd, "Runtime disconnected"):
try:
wd.find_element_by_xpath('//*[@id="ok"]').click()
except NoSuchElementException:
pass
if exists_by_text2(wd, "Notebook loading error"):
wd.get(colab_url)
try:
wd.find_element_by_xpath('//*[@id="file-menu-button"]/div/div/div[1]')
if not running:
wd.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.SHIFT + "q")
wd.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.SHIFT + "k")
exists_by_xpath(wd, '//*[@id="ok"]', 10)
wd.find_element_by_xpath('//*[@id="ok"]').click()
sleep(10)
wd.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.F9)
running = True
except NoSuchElementException:
pass
if running:
try:
wd.find_element_by_css_selector('.notebook-content-background').click()
#actions = ActionChains(wd)
#actions.send_keys(Keys.SPACE).perform()
scroll_to_bottom(wd)
print("performed scroll")
except:
pass
for frame in wd.find_elements_by_tag_name('iframe'):
wd.switch_to.frame(frame)
'''
links = browser.find_elements_by_partial_link_text('oauth2/auth')
for link in links:
new_tab(wd, link.get_attribute("href"), 1)
wd.find_element_by_css_selector('li.M8HEDc:nth-child(1)>div:nth-child(1)').click()
wd.find_element_by_css_selector('#submit_approve_access>content:nth-child(3)>span:nth-child(1)').click()
auth_code = wd.find_element_by_xpath('/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/div/div/form/content/section/div/content/div/div/div/textarea').text
'''
for output in wd.find_elements_by_tag_name('pre'):
if fork in output.text:
running = False
complete = True
print("Completion string found. Waiting for next cycle.")
break
wd.switch_to.default_content()
if complete:
break
if complete:
break
sleep(timeout)