From 728b87c6eaaed07929f13c981f41433e14f4e4cb Mon Sep 17 00:00:00 2001 From: williamforbrigd <hansw0701@gmail.com> Date: Wed, 23 Feb 2022 16:00:33 +0100 Subject: [PATCH] figure out assignments --- bbcli/cli.py | 8 ++--- bbcli/endpoints.py | 73 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 66 insertions(+), 15 deletions(-) diff --git a/bbcli/cli.py b/bbcli/cli.py index efd4ffb..874cd5b 100644 --- a/bbcli/cli.py +++ b/bbcli/cli.py @@ -5,17 +5,17 @@ from typing import Optional import typer -from bbcli import __app_name__, __version__ +from bbcli import __app_name__, __version__, endpoints + app = typer.Typer() +app.add_typer(endpoints.app, name='endpoints', help='Call the endpoints') def _version_callback(value: bool) -> None: if value: typer.echo(f"{__app_name__} v{__version__}") raise typer.Exit() - - @app.callback() def main( version: Optional[bool] = typer.Option( @@ -27,4 +27,4 @@ def main( is_eager=True, ) ) -> None: - return \ No newline at end of file + return diff --git a/bbcli/endpoints.py b/bbcli/endpoints.py index d1d2cfe..eb2919d 100644 --- a/bbcli/endpoints.py +++ b/bbcli/endpoints.py @@ -9,17 +9,19 @@ from typing import Optional app = typer.Typer() -cookies = {'BbRouter': 'expires:1645202601,id:2480855EED2012DA032F19DEE1610883,signature:81a13d5bdf4946a82a632a9035c0f35811f217671da872677b1d1c32e7a3c339,site:f4fe20be-98b0-4ecd-9039-d18ce2989292,timeout:10800,user:15bd75dd85af4f56b31283276eb8da7c,v:2,xsrf:1bea7fee-fa91-4197-991f-1d266fd1ceee'} +cookies = {'BbRouter' : 'expires:1645629198,id:B568F329FB52485B02F9D45307C27083,signature:e13c22f908162232cf18e5f4bbe698d5f7c904edca932f6a27263b374f5e7ac5,site:f4fe20be-98b0-4ecd-9039-d18ce2989292,timeout:10800,user:15bd75dd85af4f56b31283276eb8da7c,v:2,xsrf:03f9f512-620d-4f11-b84f-65f1daba0cfc'} base_url = 'https://ntnu.blackboard.com/learn/api/public/v1/' -@app.command() -def getuser(user_name: str = typer.Argument('hanswf', help='Name of the user'))-> None: +@app.command(name='get-user') +def get_user(user_name: str = typer.Argument('', help='Name of the user'))-> None: ''' - Get the user. + Get the user Specify the user_name as an option, or else it will use the default user_name ''' - url = base_url + 'users?userName=%s' % user_name + if user_name == '': + user_name = typer.prompt("What is your user name?") + url = f'{base_url}users?userName={user_name}' x = requests.get( url, cookies=cookies @@ -30,8 +32,11 @@ def getuser(user_name: str = typer.Argument('hanswf', help='Name of the user'))- print(pprint.pprint(data)) -@app.command() -def getcourse(course_id: str = 'IDATT2900'): +@app.command(name='get-course') +def get_course(course_id: str = 'IDATT2900'): + ''' + Get the course + ''' url = base_url + 'courses?courseId=%s' % course_id x = requests.get( url, @@ -40,13 +45,59 @@ def getcourse(course_id: str = 'IDATT2900'): print(pprint.pprint(data)) -@app.command() -def getcoursecontents(course_id: str = '_27251_1'): +@app.command(name='get-course-contents') +def get_course_contents(course_id: str = '_27251_1'): + ''' + Get the course contents + ''' url = f'{base_url}courses/{course_id}/contents' x = requests.get(url, cookies=cookies) data = x.json() print(pprint.pprint(data)) +def get_children(d, url,acc, count: int = 0): + #count = count + 1 + #print(f'kommer hit: {count}') + key = 'hasChildren' + if key not in d or d[key] == False: + print('nei') + return acc + else: + print('ja') + id = d['id'] + url = f'{url}/{id}/children' + print(url) + response = requests.get(url, cookies = cookies) + child = response.json()['results'] + #get_children(child, url, acc+child, count) + return child + +def get_children(d, url): + key = 'hasChildren' + while key in d and d[key] == True: + id = d['id'] + url = f'{url}/{id}/children' + print() + print(url) + print() + response = requests.get(url, cookies=cookies) + child = response.json()['results'] + return child + -if __name__ == "__main__": - app() \ No newline at end of file + +@app.command(name='get-assignments') +def get_assignments(course_id: str = typer.Argument('_27251_1', help='The course id')): + ''' + Get the assignments + ''' + url = f'{base_url}courses/{course_id}/contents' + x = requests.get(url, cookies=cookies) + data = x.json()['results'] + #res = get_children(data[2], url, []) + res = get_children(data[2], url) + print(pprint.pprint(res)) + #print(pprint.pprint(data)) + #for d in data: + #print() + #print(get_children(d, url)) -- GitLab