From d820469dcfa12a5582219c87d58f17e7a8ac246c Mon Sep 17 00:00:00 2001 From: Jakob Date: Tue, 10 Dec 2019 11:09:10 -0600 Subject: [PATCH] Make BB root configurable --- api.py | 16 +++++++--------- auth.py | 2 +- common.py | 7 ------- main.py | 14 +++++++++----- 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/api.py b/api.py index fdee4c7..c865b76 100644 --- a/api.py +++ b/api.py @@ -16,6 +16,7 @@ import json from pathlib import PurePosixPath import urllib.request +import urllib.parse import util @@ -31,18 +32,15 @@ def walk_pages(opener, url): class ApiInterface: def __init__(self, bb_root, auth_handler): - self.bb_root = bb_root + self.bb_root = urllib.parse.urlparse(bb_root) self.opener = urllib.request.build_opener(auth_handler) def build_api_url(self, endpoint, query = ''): - return urllib.parse.urlunparse(urllib.parse.ParseResult( - scheme = 'https', - netloc = self.bb_root.host, - path = str(self.bb_root.path.joinpath('learn/api/public', endpoint)), - query = query, - params = '', - fragment = '', - )) + (sch, nl, path, params, qs, frag) = self.bb_root + path = str(PurePosixPath(path).joinpath('learn/api/public', endpoint)) + return urllib.parse.urlunparse( + urllib.parse.ParseResult(sch, nl, path, params, qs, frag) + ) def get_content_path(self, course_id, content_id): from collections import deque diff --git a/auth.py b/auth.py index b99719d..3ba2220 100644 --- a/auth.py +++ b/auth.py @@ -284,7 +284,7 @@ class CookieAuthHandler(urllib.request.HTTPCookieProcessor): username = self.ui.ask_username() password = self.ui.ask_password() - url = 'https://' + self.bb_root.host + str(self.bb_root.path) + url = self.bb_root with self.parent.open(url) as resp: soup = bs4.BeautifulSoup(resp, 'lxml') [form] = soup.select('#login-form > form[name="login"]') diff --git a/common.py b/common.py index e3830c2..244928b 100644 --- a/common.py +++ b/common.py @@ -18,13 +18,6 @@ import json from pathlib import PurePosixPath -BlackboardRoot = namedtuple('BlackboardRoot', ['host', 'path']) -BB_ROOT = BlackboardRoot( - host = 'oberlin-test.blackboard.com', - path = PurePosixPath('/'), -) - - def log(level, message, indent = 0): import sys print(level.upper().rjust(7) + ': ' + indent * ' ' + message, file = sys.stderr) diff --git a/main.py b/main.py index 3765163..0db63bc 100644 --- a/main.py +++ b/main.py @@ -35,11 +35,13 @@ Example configuration file (Windows): [config] base_path: C:\Users\Jakob\blackboard\math_course auth_type: cookie + bb_host: https://blackboard.oberlin.edu/ Schema: - base_path: string - auth_type: string, "cookie" or "oauth" + base_path: string + auth_type: string; "cookie" or "oauth" + bb_host: string; base URL for the Blackboard instance (protocol and authority/host required) """ @@ -70,6 +72,8 @@ with open('config.ini') as f: cfg_parser.read_file(f) cfg_section = cfg_parser['config'] +bb_root = cfg_section['bb_root'] + with StorageManager(Path('auth_cache')) as storage_mgr: ui = Cli @@ -77,15 +81,15 @@ with StorageManager(Path('auth_cache')) as storage_mgr: if auth_type == 'cookie': auth_handler = auth.CookieAuthHandler( storage_mgr, - BB_ROOT, + bb_root, ui, auth.StorageMgrCookieJar(storage_mgr), ) - api_iface = api.ApiInterface(BB_ROOT, auth_handler) + api_iface = api.ApiInterface(bb_root, auth_handler) elif auth_type == 'oauth': # TODO dependency loop auth_handler = auth.OauthHandler(storage_mgr, None) - api_iface = api.ApiInterface(BB_ROOT, auth_handler) + api_iface = api.ApiInterface(bb_root, auth_handler) auth_handler.api_iface = api_iface else: raise ValueError("Invalid auth_type in config") -- 2.30.2