Skip to content
Snippets Groups Projects
Commit 9bdf91cb authored by williamforbrigd's avatar williamforbrigd
Browse files

more endpoints stuff

parent 9c4b9f33
No related branches found
No related tags found
No related merge requests found
import requests
#from requests.auth import HTTPBasicAuth
import json
import pprint
import ptyper.echo
import typer
#from string_builder import StringBuilder
import click
......@@ -9,7 +9,7 @@ from typing import Optional
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/'
......@@ -27,22 +27,39 @@ def get_user(user_name: str = typer.Argument('', help='Name of the user'))-> Non
cookies=cookies
)
data = x.json()
# print(data)
print(pprint.pprint(data))
data = x.json()['results'][0]
# typer.echo(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')
def get_course(course_id: str = 'IDATT2900'):
def get_course(course_id: str = typer.Argument('', help='Id of 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(
url,
cookies=cookies)
data = x.json()
print(pprint.pprint(data))
data = x.json()['results'][0]
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')
......@@ -51,35 +68,45 @@ def get_course_contents(course_id: str = '_27251_1'):
Get the course contents
'''
url = f'{base_url}courses/{course_id}/contents'
typer.echo(url)
x = requests.get(url, cookies=cookies)
data = x.json()
print(pprint.pprint(data))
data = x.json()['results']
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):
#count = count + 1
#print(f'kommer hit: {count}')
#typer.echo(f'kommer hit: {count}')
key = 'hasChildren'
if key not in d or d[key] == False:
print('nei')
typer.echo('nei')
return acc
else:
print('ja')
typer.echo('ja')
id = d['id']
url = f'{url}/{id}/children'
print(url)
typer.echo(url)
response = requests.get(url, cookies = cookies)
child = response.json()['results']
#get_children(child, url, acc+child, count)
return child
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()
typer.echo()
typer.echo(url)
typer.echo()
response = requests.get(url, cookies=cookies)
child = response.json()['results']
return child
......@@ -96,10 +123,10 @@ def get_assignments(course_id: str = typer.Argument('_27251_1', help='The course
data = x.json()['results']
#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:
print(o['title'])
#print(pprint.pprint(data))
typer.echo(o['title'])
#typer.echo(ptyper.echo.ptyper.echo(data))
#for d in data:
#print()
#print(get_children(d, url))
#typer.echo()
#typer.echo(get_children(d, url))
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()
......@@ -10,12 +10,10 @@ def test_version():
assert result.exit_code == 0
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():
result = runner.invoke(getuser, input='hanswf')
result = runner.invoke(get_user, input='hanswf')
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