import json
from pathlib import PurePosixPath
import urllib.request
+import urllib.parse
import util
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
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"]')
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)
[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)
"""
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
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")