diff --git a/bbcli/Node.py b/bbcli/Node.py index 0a1ce936f62b769b955850038cc0956e45ab35ee..ada168bb27d35b5fe621f22b52ac6814efab8a5f 100644 --- a/bbcli/Node.py +++ b/bbcli/Node.py @@ -1,5 +1,26 @@ -class Node: - def __init__(self, data, children=[]): - self.root = root +class Node(object): + def __init__(self, data, children=None): + self.data = data self.children = children + if children is not None: + for child in children: + self.add_child(child) + + def add_child(self, node): + assert isinstance(node, Node) + self.chilren.append(node) +# class Node(object): +# def __init__(self, data, children=None): +# self.data = data +# self.children = [] +# if children is not None: +# for child in children: +# self.add_child(child) + +# def __repr__(self): +# return self.name + +# def add_child(self, node): +# assert isinstance(node, Node) +# self.children.append(node) diff --git a/bbcli/__init__.py b/bbcli/__init__.py index 75ad5238c94f0c056f59538c911d95d14e5a20ca..c6993c641dc1e28fb8675465b5474d4ad5e6a3f8 100644 --- a/bbcli/__init__.py +++ b/bbcli/__init__.py @@ -3,7 +3,9 @@ __app_name__ = "bbcli" __version__ = "0.1.0" -from .endpoints import * +# from .endpoints import * +from .Node import * +from .login import * ( SUCCESS, diff --git a/bbcli/cli.py b/bbcli/cli.py index 874cd5b3c6b016cd5b1b3204b9335a326ca6d0fd..bc5aae4db690f110bcc2009504c0e9d800faee2b 100644 --- a/bbcli/cli.py +++ b/bbcli/cli.py @@ -6,11 +6,32 @@ from typing import Optional import typer from bbcli import __app_name__, __version__, endpoints +import os +from dotenv import load_dotenv +from datetime import datetime +from bbcli import login app = typer.Typer() app.add_typer(endpoints.app, name='endpoints', help='Call the endpoints') +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__}") @@ -27,4 +48,9 @@ def main( is_eager=True, ) ) -> None: + if check_valid_date() == False: + login() + # load_dotenv() + # cookies['BbRouter'] = os.getenv("BB_ROUTER") + # headers['X-Blackboard-XSRF'] = os.getenv("XSRF") return diff --git a/bbcli/endpoints.py b/bbcli/endpoints.py index 124faefb3be7d8a7e4511490ef20f3e1833916c8..e20865d701c2b8f597335631d198f3677671dc86 100644 --- a/bbcli/endpoints.py +++ b/bbcli/endpoints.py @@ -1,23 +1,12 @@ import requests #from requests.auth import HTTPBasicAuth -import json -import pprint +# import json +# import pprint import typer -#from string_builder import StringBuilder -import click -from typing import Optional -from dotenv import load_dotenv -from anytree import Node, RenderTree -import os - - - +import bbcli.cli as cli app = typer.Typer() -load_dotenv() -cookies = {'BbRouter' : os.getenv("BB_ROUTER")} -headers = {'X-Blackboard-XSRF': os.getenv('XSRF')} base_url = 'https://ntnu.blackboard.com/learn/api/public/v1/' @@ -32,11 +21,10 @@ def get_user(user_name: str = typer.Argument('', help='Name of the user'))-> Non url = f'{base_url}users?userName={user_name}' x = requests.get( url, - cookies=cookies + cookies=cli.cookies ) data = x.json()['results'][0] - # typer.echo(data) fn = data['name']['given'] sn = data['name']['family'] id = data['studentId'] @@ -55,21 +43,13 @@ def get_course(course_id: str = typer.Argument('', help='Id of the course')): url = f'{base_url}courses?courseId={course_id}' x = requests.get( url, - cookies=cookies) + 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}') -# def open_folder(data, map): -# key = 'hasChildren' -# acc = [] -# if key in data and data[key] == True: -# acc.append - - - @app.command(name='get-course-contents') def get_course_contents(course_id: str = '_27251_1'): ''' @@ -77,7 +57,7 @@ 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=cookies) + x = requests.get(url, cookies=cli.cookies) data = x.json()['results'] typer.echo('Mapper:') map = dict() @@ -90,40 +70,32 @@ def get_course_contents(course_id: str = '_27251_1'): # for d in data: # typer.echo(d['title']) -def get_children(data, url, acc, count: int = 0): +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 key not in data or data[key] == False: - typer.echo('nei') + if len(worklist) == 0: return acc else: - typer.echo('ja') + data = worklist.pop() id = data['id'] old = f'{url}/{id}/children' - typer.echo(url) - response = requests.get(old, cookies = cookies) + # 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']) return acc else: child = response.json()['results'] - acc = acc + child - return get_children(child, url, acc, count) - -def get_children2(d, url): - key = 'hasChildren' - while key in d and d[key] == True: - id = d['id'] - url = f'{url}{id}/children' - typer.echo() - typer.echo(url) - typer.echo() - response = requests.get(url, cookies=cookies) - child = response.json()['results'] - return child + for i in range(len(child)): + if key in child[i] and child[i][key] == True: + worklist.append(child[i]) + else: + acc.append(child[i]) + # parent = worklist.pop() + return get_children(worklist, url, acc, count) @@ -133,24 +105,16 @@ 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=cookies) + x = requests.get(url, cookies=cli.cookies) data = x.json()['results'] - # parent_id = data[8]['id'] - # name = data[8]['title'] - # parent = Node(parent_id+name) - # print(parent['id']) - res = get_children(data[8], url, []) - # res = get_children2(data[2], url) + root = data[8] + # root = Node(data[8]) + worklist = [root] + res = get_children(worklist, url, []) for i in res: print(i['title']) - #typer.echo(ptyper.echo.ptyper.echo(res)) - # for data in res: - # typer.echo(d['title']) - #typer.echo(ptyper.echo.ptyper.echo(data)) - #for d in data: - #typer.echo() - #typer.echo(get_children(d, url)) + diff --git a/bbcli/login.py b/bbcli/login.py index 9b764a3125999c73e71b83c2475d8cbc2ab7caf9..c25fa3fd89d447e49b446a43de07fef46042a9c7 100644 --- a/bbcli/login.py +++ b/bbcli/login.py @@ -1,7 +1,8 @@ from urllib import request import os from dotenv import load_dotenv -from RequestData import RequestData +# from RequestData import RequestData +from .RequestData import RequestData import requests import json from bs4 import BeautifulSoup