Skip to content

Instantly share code, notes, and snippets.

@gfhuertac
Created May 20, 2020 19:32
Show Gist options
  • Save gfhuertac/133caa032471010c9a3ccb1bf386d295 to your computer and use it in GitHub Desktop.
Save gfhuertac/133caa032471010c9a3ccb1bf386d295 to your computer and use it in GitHub Desktop.
# python modules
from enum import Enum
from time import sleep
# 3rd party modules
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.webkitgtk.options import Options
# local modules
class Methods(Enum):
LOGIN = 0
OAUTH = 1
class OAuthProviders(Enum):
GITHUB = 0
GOOGLE = 1
class OAuthProvider(object):
def __init__(self, username, password):
self.username = username
self.password = password
def click(self, driver):
pass
def login(self, driver):
pass
@staticmethod
def factory(provider, username, password):
if provider == OAuthProviders.GITHUB:
return GitHubProvider(username, password)
elif provider == OAuthProviders.GOOGLE:
return GoogleProvider(username, password)
class GitHubProvider(OAuthProvider):
def click(self, driver):
wait = WebDriverWait(driver, 5)
login_button = wait.until(
EC.element_to_be_clickable(
(By.CSS_SELECTOR,'div > a.social-login-item:nth-child(2) > div.social-login-icon')
)
)
driver.execute_script("arguments[0].click();", login_button)
def login(self, driver):
wait = WebDriverWait(driver, 5)
username_input = wait.until(
EC.presence_of_element_located(
(By.ID,'login_field')
)
)
username_input.send_keys(self.username)
password_input = driver.find_element_by_id('password')
password_input.send_keys(self.password)
submit_button = driver.find_element_by_xpath('//input[@value="Sign in"]')
driver.execute_script("arguments[0].click();", submit_button)
sleep(3)
class GoogleProvider(OAuthProvider):
def click(self, driver):
wait = WebDriverWait(driver, 5)
login_button = wait.until(
EC.element_to_be_clickable(
(By.CSS_SELECTOR,'div > a.social-login-item:nth-child(1) > div.social-login-icon')
)
)
driver.execute_script("arguments[0].click();", login_button)
def login(self, driver):
wait = WebDriverWait(driver, 5)
username_input = wait.until(
EC.presence_of_element_located(
(By.ID,'identifierId')
)
)
username_input.send_keys(self.username)
next_button = driver.find_element_by_id('identifierNext')
driver.execute_script("arguments[0].click();", next_button)
sleep(3)
wait = WebDriverWait(driver, 5)
password_input = wait.until(
EC.presence_of_element_located(
(By.NAME,'password')
)
)
password_input.send_keys(self.password)
submit_button = driver.find_element_by_id('passwordNext')
driver.execute_script("arguments[0].click();", submit_button)
sleep(3)
class ReplIt(object):
def __init__(self, username, password, method=Methods.LOGIN, provider=None):
self.username = username
self.password = password
self.method = method
self.provider = provider
capabilities = DesiredCapabilities.SAFARI.copy()
capabilities["browserstack.safari.enablePopups"] = "true"
self.driver = webdriver.Safari(desired_capabilities=capabilities)
def login(self):
# go to the login link
self.driver.get('https://repl.it/login')
# check if we need to press the button or login
if self.method == Methods.OAUTH:
oa = OAuthProvider.factory(self.provider, self.username, self.password)
main_page = self.driver.current_window_handle
handles = self.driver.window_handles
oa.click(self.driver)
wait = WebDriverWait(self.driver, 5)
wait.until(
EC.new_window_is_opened(handles)
)
for handle in self.driver.window_handles:
if handle != main_page:
login_page = handle
self.driver.switch_to.window(login_page)
oa.login(self.driver)
self.driver.switch_to.window(main_page)
elif self.method == Methods.LOGIN:
wait = WebDriverWait(self.driver, 5)
username_input = wait.until(
EC.presence_of_element_located(
(By.NAME,'username')
)
)
username_input.send_keys(self.username)
password_input = self.driver.find_element_by_name('password')
password_input.send_keys(self.password)
submit_button = self.driver.find_element_by_link_text('Log in')
self.driver.execute_script("arguments[0].click();", submit_button)
sleep(10)
def goto_assignment(self, assignment_id):
sleep(3)
self.driver.get(f'https://repl.it/teacher/assignments/{assignment_id}')
sleep(3)
wait = WebDriverWait(self.driver, 5)
wait.until(
EC.presence_of_all_elements_located(
(By.XPATH, '//a[contains(@href,"/teacher/submissions/")]')
)
)
elements = self.driver.find_elements_by_xpath('//a[contains(@href,"/teacher/submissions/")]')
urls = [element.get_attribute("href") for element in elements]
for url in urls:
if url.endswith('undefined'):
continue
self.open_assignment(url)
def open_assignment(self, url):
self.driver.get(url)
wait = WebDriverWait(self.driver, 10)
name_container = wait.until(
EC.presence_of_element_located(
(By.CSS_SELECTOR, 'div.dynamic-header-center h3:nth-child(1)')
)
)
student_name = name_container.text.replace(' ', '_')
try:
mark_as_completed_button = self.driver.find_element_by_xpath('//button[text()="mark completed"]')
self.driver.execute_script("arguments[0].click();", mark_as_completed_button)
sleep(3)
except Exception as e:
print('MARK COMPLETED:: ', student_name, e)
pass
def download():
download_button = self.driver.find_element_by_xpath('//a[@download]')
self.driver.execute_script(f"arguments[0].download = '{student_name}_'+arguments[0].download;", download_button)
self.driver.execute_script("arguments[0].click();", download_button)
sleep(3)
tabs = self.driver.find_elements_by_class_name('react-tabs__tab')
if len(tabs) == 0:
download()
else:
for tab in tabs:
self.driver.execute_script("arguments[0].click();", tab)
sleep(1)
download()
def quit(self):
self.driver.quit()
if __name__ == '__main__':
import os
import shutil
b:ReplIt = ReplIt('email', 'password', Methods.OAUTH, OAuthProviders.GITHUB)
b.login()
tics100_202001 = [
{
'id': 'R',
'sections': [
{
'id': 1,
'replit_ids': [1, 2, 3],
'skip': False,
}
]
},
]
folder = '/Users/gohucan/Downloads'
for campus in tics100_202001:
campus_id = campus.get('id', 'NA')
for section in campus.get('sections', []):
section_id = section.get('id', -1)
if section.get('skip', False) is False:
questions = section.get('replit_ids', [])
if type(questions) == list:
questions = enumerate(questions)
idx = 1
elif type(questions) == dict:
questions = questions.items()
idx = 0
for question, replit_id in questions:
b.goto_assignment(replit_id)
files = os.listdir(folder)
try:
os.makedirs(f'{folder}/{campus_id}0{section_id}/p{question+idx}', exist_ok=True)
except FileExistsError:
pass
for f in files:
if f.endswith('.py'):
shutil.move(f'{folder}/{f}', f'{folder}/{campus_id}0{section_id}/p{question+idx}')
b.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment