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

made some utils classes

parent db1ac70d
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,7 @@ __version__ = "0.1.0"
# from .endpoints import *
from .Node import *
from .login import *
from .utils import *
(
SUCCESS,
......
......@@ -8,8 +8,7 @@ import typer
from bbcli import __app_name__, __version__, endpoints
import os
from dotenv import load_dotenv
from datetime import datetime
from bbcli import login
from bbcli import login, check_valid_date
app = typer.Typer()
......@@ -19,19 +18,6 @@ load_dotenv()
cookies = {'BbRouter' : os.getenv("BB_ROUTER")}
headers = {'X-Blackboard-XSRF': os.getenv('XSRF')}
def check_valid_date() -> bool:
tmp = cookies['BbRouter']
start = int(tmp.find('expires')) + len('expires') + 1
end = int(tmp.find(','))
timestmp = int(tmp[start : end])
print(timestmp)
expires = datetime.fromtimestamp(timestmp)
now = datetime.now()
if expires >= now:
return True
else:
return False
def _version_callback(value: bool) -> None:
if value:
typer.echo(f"{__app_name__} v{__version__}")
......@@ -48,7 +34,7 @@ def main(
is_eager=True,
)
) -> None:
if check_valid_date() == False:
if check_valid_date(cookies) == False:
login()
# load_dotenv()
# cookies['BbRouter'] = os.getenv("BB_ROUTER")
......
......@@ -5,7 +5,7 @@ import requests
import typer
import bbcli.cli as cli
app = typer.Typer()
from bbcli import check_response
base_url = 'https://ntnu.blackboard.com/learn/api/public/v1/'
......@@ -19,18 +19,20 @@ def get_user(user_name: str = typer.Argument('', help='Name of the user'))-> Non
if user_name == '':
user_name = typer.prompt("What is your user name?")
url = f'{base_url}users?userName={user_name}'
x = requests.get(
response = requests.get(
url,
cookies=cli.cookies
)
if check_response(response) == False:
return
else:
data = x.json()['results'][0]
fn = data['name']['given']
sn = data['name']['family']
id = data['studentId']
data = x.json()['results'][0]
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}')
typer.echo(f'Name of the student: {fn} {sn}')
typer.echo(f'The student id: {id}')
@app.command(name='get-course')
......@@ -41,14 +43,17 @@ def get_course(course_id: str = typer.Argument('', help='Id of the course')):
if course_id == '':
course_id = typer.prompt("What is the course id?")
url = f'{base_url}courses?courseId={course_id}'
x = requests.get(
response = requests.get(
url,
cookies=cli.cookies)
data = x.json()['results'][0]
name = data['name']
course_url = data['externalAccessUrl']
typer.echo(name)
typer.echo(f'URL for the course: {course_url}')
if check_response(response) == False:
return
else:
data = x.json()['results'][0]
name = data['name']
course_url = data['externalAccessUrl']
typer.echo(name)
typer.echo(f'URL for the course: {course_url}')
@app.command(name='get-course-contents')
def get_course_contents(course_id: str = '_27251_1'):
......@@ -57,23 +62,22 @@ def get_course_contents(course_id: str = '_27251_1'):
'''
url = f'{base_url}courses/{course_id}/contents'
typer.echo(url)
x = requests.get(url, cookies=cli.cookies)
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'])
response = requests.get(url, cookies=cli.cookies)
if check_response(response) == False:
return
else:
data = response.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}')
typer.echo(map)
def get_children(worklist, url, acc, count: int = 0):
count = count + 1
typer.echo(f'kommer hit: {count}')
# print("The acc is: ", acc)
key = 'hasChildren'
if len(worklist) == 0:
return acc
......@@ -81,11 +85,8 @@ def get_children(worklist, url, acc, count: int = 0):
data = worklist.pop()
id = data['id']
old = f'{url}/{id}/children'
# typer.echo(url)
response = requests.get(old, cookies = cli.cookies)
if response.status_code == 403 or response.status_code == 404:
typer.echo(response.json()['status'])
typer.echo(response.json()['message'])
if check_response(response) == False:
return acc
else:
child = response.json()['results']
......@@ -94,7 +95,6 @@ def get_children(worklist, url, acc, count: int = 0):
worklist.append(child[i])
else:
acc.append(child[i])
# parent = worklist.pop()
return get_children(worklist, url, acc, count)
......@@ -105,15 +105,18 @@ def get_assignments(course_id: str = typer.Argument('_27251_1', help='The course
Get the assignments
'''
url = f'{base_url}courses/{course_id}/contents'
x = requests.get(url, cookies=cli.cookies)
data = x.json()['results']
root = data[8]
# root = Node(data[8])
worklist = [root]
res = get_children(worklist, url, [])
for i in res:
print(i['title'])
response = requests.get(url, cookies=cli.cookies)
if check_response(response) == False:
return
else:
data = response.json()['results']
root = data[8]
# root = Node(data[8])
worklist = [root]
res = get_children(worklist, url, [])
for i in res:
print(i['title'])
......
from datetime import datetime
def check_valid_key(obj, key) -> bool:
# print("the keys are", obj.keys())
if key not in obj.keys():
print(f'The key: \"{key}\" is not in the object')
return False
else:
return True
def check_response(response) -> bool:
invalid_statuscodes = [401, 403, 404]
if response.status_code in invalid_statuscodes:
print(response.json()['status'])
print(response.json()['message'])
return False
else:
return True
def check_valid_date(cookies) -> bool:
tmp = cookies['BbRouter']
start = int(tmp.find('expires')) + len('expires') + 1
end = int(tmp.find(','))
timestmp = int(tmp[start : end])
print(timestmp)
expires = datetime.fromtimestamp(timestmp)
now = datetime.now()
if expires >= now:
return True
else:
return False
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