mirror of
https://github.com/peter-tanner/Blackboard-marks.git
synced 2024-11-30 11:40:16 +08:00
update selenium, 2023 version
This commit is contained in:
parent
234cd31bf4
commit
c97582bef2
1
.gitignore
vendored
Normal file → Executable file
1
.gitignore
vendored
Normal file → Executable file
|
@ -6,3 +6,4 @@ test*
|
|||
.vscode/
|
||||
*.7z
|
||||
*.tar
|
||||
.venv
|
18
README.md
Normal file → Executable file
18
README.md
Normal file → Executable file
|
@ -1,24 +1,28 @@
|
|||
## Blackboard marks downloader (UWA)
|
||||
|
||||
---
|
||||
|
||||
**Dependencies**:
|
||||
|
||||
- python
|
||||
- selenium
|
||||
- chromedriver, placed relative to this directory
|
||||
|
||||
Run the script with `py main.py` and enter your student number and password. I'm not taking your personal details, but *don't take my word for it* - always check the source if you don't trust it!
|
||||
Run the script with `py main.py` and enter your student number and password. I'm not taking your personal details, but _don't take my word for it_ - always check the source if you don't trust it!
|
||||
|
||||
---
|
||||
|
||||
Made this script to download my marks, receipts and all the stuff I uploaded for my first semester. It's a fucking mess of spaghetti python code because to be honest I really just wanted to get this out of the way and have some time for other stuff after the first round of exams. It's a mess of code, with some bits (the login) being picked from the scraper script and some of the scraper asset objects being translated from ruby to python here (in a quick and incomplete way). This will probably will break in some way when the UI is overhauled for next semester :/
|
||||
Made this script to download my marks, receipts and all the stuff I uploaded for my first semester.
|
||||
|
||||
There is no bulk marks download feature in the current lms, even though it seems other blackboard installations can give students this bulk download ability. It relies on a lot of js crap so I ended up using selenium all the way through. Doesn't download styles to save space, you'll have to download the css and js yourself and it has to be absolute because the script makes no effort to make the links relative.
|
||||
There is no bulk marks download feature in the current lms, even though it seems other blackboard installations can give students this bulk download ability. Saves visited pages to `URLS.txt` so you can use something like SingleFile extension and use their batch save url feature to save the list of urls visited (I recommend enabling scripts in the singlefile settings so that comments are saved)
|
||||
|
||||
This one was made for UWA but you may be able to tweak it for your institution (see constants.py).
|
||||
|
||||
Just made it able to download the graded results which may contain annotations. Using a really hacky method to do it so it doesn't create a metadata file for it.
|
||||
|
||||
## Note:
|
||||
* Does not download turnitin reports. You have to click the link manually to the feedback site.
|
||||
* Does not download multiple submission attempts - only downloads the last/graded attempt.
|
||||
* Check that the default page is the 'all' category for the marks instead of something else like the submitted category. The script should correct this but just to be safe click on all if it isn't already
|
||||
* Sometimes chromedriver closes after logging in, when not in headless mode. Try interacting with the page before logging in.
|
||||
|
||||
- Does not download turnitin reports. You have to click the link manually to the feedback site.
|
||||
- Does not download multiple submission attempts - only downloads the last/graded attempt.
|
||||
- Check that the default page is the 'all' category for the marks instead of something else like the submitted category. The script should correct this but just to be safe click on all if it isn't already
|
||||
- Sometimes chromedriver closes after logging in, when not in headless mode. Try interacting with the page before logging in.
|
||||
|
|
4
constants/constants.py
Normal file → Executable file
4
constants/constants.py
Normal file → Executable file
|
@ -5,3 +5,7 @@ BASE_URL = "https://lms.uwa.edu.au" # Include protocol.
|
|||
|
||||
DL_DIR = os.getcwd() + os.path.sep + "tmp" + os.path.sep
|
||||
Path(DL_DIR).mkdir(parents=True, exist_ok=True)
|
||||
|
||||
SAVE_DIR = "grades"
|
||||
|
||||
URL_LIST = SAVE_DIR + os.path.sep + "URLS.txt"
|
||||
|
|
64
main.py
Normal file → Executable file
64
main.py
Normal file → Executable file
|
@ -1,5 +1,7 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
from selenium.webdriver.remote.webdriver import WebDriver
|
||||
from typing import cast
|
||||
import requests
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.support import expected_conditions as EC
|
||||
|
@ -18,36 +20,43 @@ import argparse
|
|||
|
||||
from utils.asset import RequestStack
|
||||
from utils.wait import SwitchToIFrame, WaitClickable, WaitDiv
|
||||
from constants.constants import BASE_URL, DL_DIR
|
||||
from constants.constants import BASE_URL, DL_DIR, SAVE_DIR
|
||||
from utils.login import login
|
||||
from utils.utils import download_file, get_assignment_name, save_html
|
||||
from pathlib import Path
|
||||
from selenium.common.exceptions import ElementNotInteractableException
|
||||
|
||||
testing = False
|
||||
try:
|
||||
testing = True
|
||||
from utils.test import get_etc
|
||||
except:
|
||||
def get_etc(*args): return False
|
||||
# try:
|
||||
# testing = True
|
||||
# from utils.test import get_etc
|
||||
# except Exception:
|
||||
|
||||
|
||||
def get_etc(*args):
|
||||
return False
|
||||
|
||||
|
||||
# stupid bug
|
||||
def click_the_fing_button(driver, button):
|
||||
|
||||
|
||||
def click_the_fing_button(driver: WebDriver, button):
|
||||
try:
|
||||
ActionChains(driver).move_to_element(button)
|
||||
ActionChains(driver).click(button).perform()
|
||||
WebDriverWait(driver, 2).until(EC.number_of_windows_to_be(2))
|
||||
except:
|
||||
except Exception:
|
||||
# hack to wake selenium up when it doesnt want to click the button!
|
||||
driver.set_window_size(1024, 768)
|
||||
click_the_fing_button(driver, button)
|
||||
driver.maximize_window()
|
||||
|
||||
# You can probably replace this with a recursive method like in blackboard scraper but tbh i just want to get this script done so i can stop working for once.
|
||||
# You can probably replace this with a recursive method like in blackboard
|
||||
# scraper but tbh i just want to get this script done so i can stop working for
|
||||
# once.
|
||||
|
||||
|
||||
def scrape_further(driver, path, session):
|
||||
def scrape_further(driver: WebDriver, path, session):
|
||||
# attempts for bb-held tests
|
||||
attempts = driver.find_elements(
|
||||
By.XPATH, "//a[starts-with(@href, '/webapps/assessment')]")
|
||||
|
@ -59,7 +68,7 @@ def scrape_further(driver, path, session):
|
|||
driver.execute_script("window.open('" + BASE_URL + attempt + "')")
|
||||
WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(3))
|
||||
driver.switch_to.window(driver.window_handles[2])
|
||||
save_html(path, name, driver.page_source)
|
||||
save_html(path, name, driver, True)
|
||||
if testing:
|
||||
get_etc(driver, session, path)
|
||||
driver.close()
|
||||
|
@ -71,7 +80,7 @@ def scrape_further(driver, path, session):
|
|||
By.XPATH, "//a[contains(@href, '/bbcswebdav')]")
|
||||
etc_files = [x.get_attribute('href') for x in etc_files]
|
||||
for i, item in enumerate(etc_files):
|
||||
if (not item is None) and ("bbcswebdav" in item):
|
||||
if (item is not None) and ("bbcswebdav" in item):
|
||||
request_stack.add_file(item, path)
|
||||
|
||||
# submission file for assignment
|
||||
|
@ -90,7 +99,7 @@ def scrape_further(driver, path, session):
|
|||
driver, (By.XPATH, "//iframe[@class='docviewer_iframe_embed']"))
|
||||
SwitchToIFrame(driver, (By.XPATH, "//iframe[@title='PSPDFKit']"))
|
||||
get_feedback = True
|
||||
except:
|
||||
except Exception:
|
||||
print("No feedback to download")
|
||||
if get_feedback:
|
||||
dl_button = WaitClickable(
|
||||
|
@ -106,10 +115,11 @@ parser = argparse.ArgumentParser(description='Automated microsoft SSO login.')
|
|||
parser.add_argument("-u", "--username",
|
||||
help="Automatically use provided userID", default="")
|
||||
|
||||
path = ['grades']
|
||||
path = [SAVE_DIR]
|
||||
Path(SAVE_DIR).mkdir(parents=True, exist_ok=True)
|
||||
args = parser.parse_args()
|
||||
|
||||
CAPABILITIES = DesiredCapabilities.CHROME
|
||||
CAPABILITIES = cast("dict[str, object]", DesiredCapabilities.CHROME.copy())
|
||||
CAPABILITIES['goog:loggingPrefs'] = {
|
||||
'performance': 'ALL',
|
||||
}
|
||||
|
@ -142,12 +152,12 @@ for cookie in cookies:
|
|||
driver.get(BASE_URL + "/webapps/gradebook/do/student/viewCourses")
|
||||
try:
|
||||
WaitClickable(driver, (By.CLASS_NAME, "button-1")).click()
|
||||
except:
|
||||
except Exception:
|
||||
print("no tos warning - skipped")
|
||||
|
||||
driver.get(
|
||||
BASE_URL + "/webapps/streamViewer/streamViewer?cmd=view&streamName=mygrades")
|
||||
save_html(sep.join(path), 'entrypoint', driver.page_source)
|
||||
save_html(sep.join(path), 'entrypoint', driver, True)
|
||||
|
||||
WaitClickable(driver, (By.ID, "left_stream_mygrades"))
|
||||
# get courseIDs
|
||||
|
@ -194,7 +204,7 @@ for i, course in enumerate(course_details):
|
|||
block = assignment.find_element(
|
||||
By.XPATH, "./div[@class='cell gradable']/a[@onclick]")
|
||||
information_link = True
|
||||
except:
|
||||
except Exception:
|
||||
block = assignment.find_element(
|
||||
By.XPATH, "./div[@class='cell gradable']")
|
||||
assignment_name = get_assignment_name(driver, block)
|
||||
|
@ -207,7 +217,7 @@ for i, course in enumerate(course_details):
|
|||
print("Switched " + assignment_name)
|
||||
WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
|
||||
driver.switch_to.window(driver.window_handles[1])
|
||||
save_html(sep.join(path), "information", driver.page_source)
|
||||
save_html(sep.join(path), "information", driver, True)
|
||||
scrape_further(driver, sep.join(path), session)
|
||||
driver.close()
|
||||
driver.switch_to.window(driver.window_handles[0])
|
||||
|
@ -216,36 +226,36 @@ for i, course in enumerate(course_details):
|
|||
# download rubric if it exists.
|
||||
for button in buttons:
|
||||
action = button.get_attribute("onclick")
|
||||
if action != None and "showInLightBox" not in action:
|
||||
if action is not None and "showInLightBox" not in action:
|
||||
click_the_fing_button(driver, button)
|
||||
driver.execute_script(
|
||||
"window.scrollTo(0, document.body.scrollHeight)")
|
||||
driver.switch_to.window(driver.window_handles[1])
|
||||
WaitDiv(driver, (By.CLASS_NAME, "rubricControlContainer"))
|
||||
save_html(sep.join(path), "rubric", driver.page_source)
|
||||
save_html(sep.join(path), "rubric", driver, True)
|
||||
driver.find_element(
|
||||
By.XPATH, "//li[@id='listViewTab']/a").click()
|
||||
WaitDiv(driver, (By.CLASS_NAME, "rubricGradingList"))
|
||||
save_html(sep.join(path), "list", driver.page_source)
|
||||
save_html(sep.join(path), "list", driver, True)
|
||||
detailed_buttons = driver.find_elements(
|
||||
By.XPATH, "//div[@class='u_controlsWrapper']/input")
|
||||
detailed_buttons[1].click()
|
||||
detailed_buttons[0].click()
|
||||
save_html(sep.join(path), "list_detailed", driver.page_source)
|
||||
save_html(sep.join(path), "list_detailed", driver, True)
|
||||
driver.close()
|
||||
driver.switch_to.window(driver.window_handles[0])
|
||||
path.pop()
|
||||
save_html(sep.join(path), path[0], driver.page_source)
|
||||
save_html(sep.join(path), path[0], driver, True)
|
||||
WaitClickable(driver, (By.XPATH, "//a[@value='S']")).click()
|
||||
save_html(sep.join(path), "submitted", driver.page_source)
|
||||
save_html(sep.join(path), "submitted", driver, True)
|
||||
try:
|
||||
WaitClickable(
|
||||
driver, (By.XPATH, "//div[@id='submissionReceipts']//a")).click()
|
||||
WaitClickable(
|
||||
driver, (By.XPATH, "//div[@id='listContainer_itemcount']//a[@class='pagelink']")).click()
|
||||
except:
|
||||
except Exception:
|
||||
print('No items?')
|
||||
save_html(sep.join(path), "receipts", driver.page_source)
|
||||
save_html(sep.join(path), "receipts", driver, True)
|
||||
path.pop()
|
||||
|
||||
|
||||
|
|
BIN
requirements.txt
Executable file
BIN
requirements.txt
Executable file
Binary file not shown.
0
utils/__init__.py
Normal file → Executable file
0
utils/__init__.py
Normal file → Executable file
0
utils/asset.py
Normal file → Executable file
0
utils/asset.py
Normal file → Executable file
4
utils/login.py
Normal file → Executable file
4
utils/login.py
Normal file → Executable file
|
@ -28,7 +28,7 @@ def login(args, driver):
|
|||
WaitClickable(driver, Selectors.BOX_PASSWORD).send_keys(PASSWORD)
|
||||
WaitClickable(driver, Selectors.BUTTON_NEXT).click()
|
||||
print('Entered password.')
|
||||
except:
|
||||
except Exception:
|
||||
print(WebDriverWait(driver, 1).until(
|
||||
EC.visibility_of_element_located(Selectors.DIV_USERERROR)).text)
|
||||
driver.quit()
|
||||
|
@ -38,7 +38,7 @@ def login(args, driver):
|
|||
# WaitClickable(driver,BUTTON_NEXT).click() #IF you want to remember credentials, switch these comments
|
||||
|
||||
cookie = driver.get_cookies()
|
||||
if not cookie == None:
|
||||
if cookie is not None:
|
||||
return cookie
|
||||
|
||||
print('Could not get auth cookie - Invalid ID or password?', file=sys.stderr)
|
||||
|
|
0
utils/selectors.py
Normal file → Executable file
0
utils/selectors.py
Normal file → Executable file
17
utils/utils.py
Normal file → Executable file
17
utils/utils.py
Normal file → Executable file
|
@ -1,6 +1,8 @@
|
|||
from selenium.webdriver.remote.webdriver import WebDriver
|
||||
import pathlib
|
||||
import re
|
||||
from constants.constants import DL_DIR
|
||||
from typing import Union
|
||||
from constants.constants import DL_DIR, URL_LIST
|
||||
from selenium.webdriver.support import expected_conditions as EC
|
||||
import time
|
||||
import os
|
||||
|
@ -23,18 +25,21 @@ def friendly_dirname(name):
|
|||
return name.strip()
|
||||
|
||||
|
||||
def get_assignment_name(driver, block):
|
||||
def get_assignment_name(driver: WebDriver, block):
|
||||
s = friendly_filename(get_text_excluding_children(driver, block))
|
||||
print("Assesment: " + s)
|
||||
return s
|
||||
|
||||
|
||||
def save_html(dir, filename, page_source):
|
||||
def save_html(dir, filename, driver: WebDriver, page_log_file=False):
|
||||
if page_log_file:
|
||||
with open(URL_LIST, "a", encoding="utf-8") as f:
|
||||
f.write(driver.current_url + "\n")
|
||||
dir = pathlib.Path(friendly_dirname(dir))
|
||||
dir.mkdir(parents=True, exist_ok=True)
|
||||
file = dir.joinpath(friendly_filename(filename) + ".html")
|
||||
with open(file, "w", encoding="utf-8") as f:
|
||||
f.write(page_source)
|
||||
f.write(driver.page_source)
|
||||
|
||||
# NOTE: Switching to a "download" tab causes issues so we must use the in built
|
||||
# download in Chrome, which does not have etag or metadata information.
|
||||
|
@ -56,10 +61,10 @@ def download_file(dest):
|
|||
else:
|
||||
_dest = Path(dest).joinpath("MARKED__" + f)
|
||||
try:
|
||||
shutil.move(d.joinpath(f), _dest)
|
||||
shutil.move(str(d.joinpath(f)), _dest)
|
||||
except shutil.SameFileError:
|
||||
os.remove(_dest)
|
||||
shutil.move(d.joinpath(f), _dest)
|
||||
shutil.move(str(d.joinpath(f)), _dest)
|
||||
|
||||
if len(os.listdir(d)) == 0:
|
||||
downloading = False
|
||||
|
|
13
utils/wait.py
Normal file → Executable file
13
utils/wait.py
Normal file → Executable file
|
@ -1,16 +1,15 @@
|
|||
from selenium.webdriver.support.wait import WebDriverWait
|
||||
from selenium.webdriver.support import expected_conditions as EC
|
||||
timeout = 5
|
||||
# find_element_safe = lambda name,timeout=30:WebDriverWait(driver, timeout).until(lambda x: x.find_element(By.ID, name))
|
||||
|
||||
|
||||
def WaitClickable(driver, locator): return WebDriverWait(
|
||||
driver, timeout).until(EC.element_to_be_clickable(locator))
|
||||
def WaitClickable(driver, locator):
|
||||
return WebDriverWait(driver, timeout).until(EC.element_to_be_clickable(locator))
|
||||
|
||||
|
||||
def WaitDiv(driver, locator): return WebDriverWait(
|
||||
driver, timeout).until(EC.presence_of_element_located(locator))
|
||||
def WaitDiv(driver, locator):
|
||||
return WebDriverWait(driver, timeout).until(EC.presence_of_element_located(locator))
|
||||
|
||||
|
||||
def SwitchToIFrame(driver, locator): return WebDriverWait(
|
||||
driver, timeout).until(EC.frame_to_be_available_and_switch_to_it(locator))
|
||||
def SwitchToIFrame(driver, locator):
|
||||
return WebDriverWait(driver, timeout).until(EC.frame_to_be_available_and_switch_to_it(locator))
|
||||
|
|
Loading…
Reference in New Issue
Block a user