Skip to content
Snippets Groups Projects
Commit 3ff442be authored by Hague Matthew UXAC009's avatar Hague Matthew UXAC009
Browse files

Update quiz-change-times to new cookie handling

parent cfa653e6
Branches
No related tags found
No related merge requests found
......@@ -14,30 +14,24 @@
# * --increment=dd:hh:mm: increment the open/close time by days hours mins
# * --set="yyyy-mm-dd hh:mm": set all open/close dates to given date
#
# Tries to save time by copying your cookies from your Firefox
# cookies.sqlite. Edit the USER_COOKIES variable to point to your
# cookies to enable this. Then if you're logged in on your normal
# firefox, login should be a single click with this script
# Tries to save time by copying your cookies from your Firefox profile.
# Then if you're logged in on your normal firefox, login should be a
# single click with this script
import browser_cookie3
import csv
import getopt
import os
import shutil
import re
import sys
import tempfile
from datetime import datetime, timedelta
from typing import Any, List, Optional
from typing import Any, Dict, List, Optional
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
USER_COOKIES \
= "/home/matt/.mozilla/firefox/g4q3fupz.default-release/cookies.sqlite"
MOODLE_URL = "https://moodle.royalholloway.ac.uk"
QUIZ_EDIT_URL \
= "https://moodle.royalholloway.ac.uk/course/modedit.php?update={}&return=1"
......@@ -46,6 +40,9 @@ QUIZ_ID_GRP = 1
WAIT_TIMEOUT = 60 # seconds
MOODLE_DOMAIN = "moodle.royalholloway.ac.uk"
MOODLE_SESSION_COOKIE = "MoodleSession"
class Browser:
"""Simple container of browser setup and teardown
......@@ -56,32 +53,33 @@ class Browser:
self.driver = None
def __enter__(self):
self._profile_dir = tempfile.TemporaryDirectory()
if os.path.isfile(USER_COOKIES):
shutil.copy(USER_COOKIES, self._profile_dir.name)
self.driver = webdriver.Firefox(
firefox_profile = webdriver.FirefoxProfile(self._profile_dir.name)
)
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(WAIT_TIMEOUT)
return self
def __exit__(self, *kwargs):
if self.driver is not None:
self.driver.quit()
if self._profile_dir is not None:
self._profile_dir.cleanup()
def login(self):
"""Login to the browser Moodle session"""
if self.driver:
self.driver.get(MOODLE_URL)
for cookie in self._get_moodle_cookies():
self.driver.add_cookie(cookie)
input("Press enter when logged in.")
else:
raise Exception("Cannot log in, no web driver initialised.")
def _get_moodle_cookies(self) -> List[Dict[str, Optional[str]]]:
"""Gets current moodle cookies from firefox"""
cookie_jar = browser_cookie3.firefox(domain_name=MOODLE_DOMAIN)
return [
{ "name" : c.name, "value" : c.value }
for c in cookie_jar
if c.name == MOODLE_SESSION_COOKIE
]
def get_quiz_edit_url(quiz_url : str) -> str:
"""Convert a quiz URL into the page URL for editing the quiz"""
if match := QUIZ_ID_RE.match(quiz_url):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment