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