Make BB root configurable
authorJakob <jakob@jcornell.net>
Tue, 10 Dec 2019 17:09:10 +0000 (11:09 -0600)
committerJakob <jakob@jcornell.net>
Tue, 10 Dec 2019 17:09:10 +0000 (11:09 -0600)
api.py
auth.py
common.py
main.py

diff --git a/api.py b/api.py
index fdee4c7531202cd85692a7fb1b19d65c2d52497e..c865b76c77193c1ff10a2547080d1861ad4bab73 100644 (file)
--- 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 b99719d668f942ff2840f65148632461a6b3f0fa..3ba22202a639ac62c67debc1593a48337b11ccc7 100644 (file)
--- 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"]')
index e3830c2cd34cbdf40bbf4850b895697d0b5bfeec..244928b0d4b7a9bb89e70edbf3f8300301a46161 100644 (file)
--- 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 37651632841ba679c7be3412ec31fda4598e1eb2..0db63bc7dd6ade623fda63345bb6a0657f8cef2c 100644 (file)
--- 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")