Skip to content
Snippets Groups Projects
Commit 9352c2e6 authored by Hans William Forbrigd's avatar Hans William Forbrigd
Browse files

Merge branch 'william_feature_getassignments' into 'main'

William feature getassignments

See merge request mattiaae/idatt2900-072!5
parents e166a6f3 a73c7760
No related branches found
No related tags found
No related merge requests found
import requests import requests
#from requests.auth import HTTPBasicAuth #from requests.auth import HTTPBasicAuth
import json import json
import pprint import pprint
import typer import typer
#from string_builder import StringBuilder #from string_builder import StringBuilder
import click import click
...@@ -9,7 +9,7 @@ from typing import Optional ...@@ -9,7 +9,7 @@ from typing import Optional
app = typer.Typer() app = typer.Typer()
cookies = {'BbRouter' :'expires:1645705759,id:CBD8467556E3976D9F7047190DC6F82D,signature:a80cb88001c675656aca564780763872482d35ca8715faab5bae0eb3393668fd,site:f4fe20be-98b0-4ecd-9039-d18ce2989292,timeout:10800,user:15bd75dd85af4f56b31283276eb8da7c,v:2,xsrf:fd102c30-042c-4135-a515-a293ed638ba5' } cookies = {'BbRouter' : 'expires:1645724548,id:CBD8467556E3976D9F7047190DC6F82D,signature:4a5b5bbf51712187e3c59b9374c42d4d0f62409b32504b200f5d49bc1636f7f1,site:f4fe20be-98b0-4ecd-9039-d18ce2989292,timeout:10800,user:15bd75dd85af4f56b31283276eb8da7c,v:2,xsrf:fd102c30-042c-4135-a515-a293ed638ba5'}
base_url = 'https://ntnu.blackboard.com/learn/api/public/v1/' base_url = 'https://ntnu.blackboard.com/learn/api/public/v1/'
...@@ -27,22 +27,39 @@ def get_user(user_name: str = typer.Argument('', help='Name of the user'))-> Non ...@@ -27,22 +27,39 @@ def get_user(user_name: str = typer.Argument('', help='Name of the user'))-> Non
cookies=cookies cookies=cookies
) )
data = x.json() data = x.json()['results'][0]
# print(data) # typer.echo(data)
print(pprint.pprint(data)) fn = data['name']['given']
sn = data['name']['family']
id = data['studentId']
typer.echo(f'Name of the student: {fn} {sn}')
typer.echo(f'The student id: {id}')
@app.command(name='get-course') @app.command(name='get-course')
def get_course(course_id: str = 'IDATT2900'): def get_course(course_id: str = typer.Argument('', help='Id of the course')):
''' '''
Get the course Get the course
''' '''
url = base_url + 'courses?courseId=%s' % course_id if course_id == '':
course_id = typer.prompt("What is the course id?")
url = f'{base_url}courses?courseId={course_id}'
x = requests.get( x = requests.get(
url, url,
cookies=cookies) cookies=cookies)
data = x.json() data = x.json()['results'][0]
print(pprint.pprint(data)) name = data['name']
course_url = data['externalAccessUrl']
typer.echo(name)
typer.echo(f'URL for the course: {course_url}')
# def open_folder(data, map):
# key = 'hasChildren'
# acc = []
# if key in data and data[key] == True:
# acc.append
@app.command(name='get-course-contents') @app.command(name='get-course-contents')
...@@ -51,35 +68,45 @@ def get_course_contents(course_id: str = '_27251_1'): ...@@ -51,35 +68,45 @@ def get_course_contents(course_id: str = '_27251_1'):
Get the course contents Get the course contents
''' '''
url = f'{base_url}courses/{course_id}/contents' url = f'{base_url}courses/{course_id}/contents'
typer.echo(url)
x = requests.get(url, cookies=cookies) x = requests.get(url, cookies=cookies)
data = x.json() data = x.json()['results']
print(pprint.pprint(data)) typer.echo('Mapper:')
map = dict()
for i in range(len(data)):
title = data[i]['title']
map[i+1] = data[i]['id']
typer.echo(f'{i+1} {title}')
# idx = typer.prompt("Open a folder by pressing a number: ")
typer.echo(map)
# for d in data:
# typer.echo(d['title'])
def get_children(d, url,acc, count: int = 0): def get_children(d, url,acc, count: int = 0):
#count = count + 1 #count = count + 1
#print(f'kommer hit: {count}') #typer.echo(f'kommer hit: {count}')
key = 'hasChildren' key = 'hasChildren'
if key not in d or d[key] == False: if key not in d or d[key] == False:
print('nei') typer.echo('nei')
return acc return acc
else: else:
print('ja') typer.echo('ja')
id = d['id'] id = d['id']
url = f'{url}/{id}/children' url = f'{url}/{id}/children'
print(url) typer.echo(url)
response = requests.get(url, cookies = cookies) response = requests.get(url, cookies = cookies)
child = response.json()['results'] child = response.json()['results']
#get_children(child, url, acc+child, count) get_children(child, url, acc+child, count)
return child # return child
def get_children(d, url): def get_children(d, url):
key = 'hasChildren' key = 'hasChildren'
while key in d and d[key] == True: while key in d and d[key] == True:
id = d['id'] id = d['id']
url = f'{url}/{id}/children' url = f'{url}/{id}/children'
print() typer.echo()
print(url) typer.echo(url)
print() typer.echo()
response = requests.get(url, cookies=cookies) response = requests.get(url, cookies=cookies)
child = response.json()['results'] child = response.json()['results']
return child return child
...@@ -96,10 +123,10 @@ def get_assignments(course_id: str = typer.Argument('_27251_1', help='The course ...@@ -96,10 +123,10 @@ def get_assignments(course_id: str = typer.Argument('_27251_1', help='The course
data = x.json()['results'] data = x.json()['results']
#res = get_children(data[2], url, []) #res = get_children(data[2], url, [])
res = get_children(data[2], url) res = get_children(data[2], url)
#print(pprint.pprint(res)) #typer.echo(ptyper.echo.ptyper.echo(res))
for o in res: for o in res:
print(o['title']) typer.echo(o['title'])
#print(pprint.pprint(data)) #typer.echo(ptyper.echo.ptyper.echo(data))
#for d in data: #for d in data:
#print() #typer.echo()
#print(get_children(d, url)) #typer.echo(get_children(d, url))
from typer.testing import CliRunner from typer.testing import CliRunner
from bbcli import __app_name__, __version__, cli, getuser, getcoursecontents from bbcli import __app_name__, __version__, cli, get_user
runner = CliRunner() runner = CliRunner()
...@@ -10,12 +10,10 @@ def test_version(): ...@@ -10,12 +10,10 @@ def test_version():
assert result.exit_code == 0 assert result.exit_code == 0
assert result.output == f"{__app_name__} v{__version__}\n" assert result.output == f"{__app_name__} v{__version__}\n"
#assert f"{__app_name__} v{__version__}\n" in result.stdout assert f"{__app_name__} v{__version__}\n" in result.stdout
def test_endpoint_getuser(): def test_endpoint_getuser():
result = runner.invoke(getuser, input='hanswf') result = runner.invoke(get_user, input='hanswf')
assert not result.exception assert not result.exception
def test_endpoint_getcoursecontents():
res = runner.invoke(getcoursecontents)
assert not res.exception
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment