Skip to content
Snippets Groups Projects
Commit 96cae568 authored by Mattias Eggen's avatar Mattias Eggen
Browse files

tmp create tree

parent cbdcd3c1
No related branches found
No related tags found
No related merge requests found
class Node(object): class Node(object):
def __init__(self, data, children=None): def __init__(self, data, children, parent=None):
self.data = data self.data = data
self.children = children self.children = children
if children is not None: self.parent = parent
for child in children: self.children = children #bool
self.add_child(child) # if children is not None:
# for child in children:
# self.add_child(child)
def add_child(self, node): def add_child(self, node):
assert isinstance(node, Node) assert isinstance(node, Node)
......
from venv import create
import requests import requests
#from requests.auth import HTTPBasicAuth #from requests.auth import HTTPBasicAuth
import json import json
...@@ -10,19 +11,20 @@ from dotenv import load_dotenv ...@@ -10,19 +11,20 @@ from dotenv import load_dotenv
from bbcli.Node import Node from bbcli.Node import Node
import os import os
from anytree import Node as Nd, RenderTree
app = typer.Typer() app = typer.Typer()
load_dotenv() 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')}
base_url = 'https://ntnu.blackboard.com/learn/api/public/v1/' base_url = 'https://ntnu.blackboard.com/learn/api/public/v1/'
@app.command(name='get-user') @app.command(name='get-user')
def get_user(user_name: str = typer.Argument('', help='Name of the user'))-> None: def get_user(user_name: str = typer.Argument('', help='Name of the user')) -> None:
''' '''
Get the user Get the user
Specify the user_name as an option, or else it will use the default user_name Specify the user_name as an option, or else it will use the default user_name
...@@ -69,7 +71,6 @@ def get_course(course_id: str = typer.Argument('', help='Id of the course')): ...@@ -69,7 +71,6 @@ def get_course(course_id: str = typer.Argument('', help='Id of the course')):
# acc.append # acc.append
@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'):
''' '''
...@@ -88,35 +89,65 @@ def get_course_contents(course_id: str = '_27251_1'): ...@@ -88,35 +89,65 @@ def get_course_contents(course_id: str = '_27251_1'):
# idx = typer.prompt("Open a folder by pressing a number: ") # idx = typer.prompt("Open a folder by pressing a number: ")
typer.echo(map) typer.echo(map)
# for d in data: # for d in data:
# typer.echo(d['title']) # typer.echo(d['title'])
def get_children(worklist, url, acc, count: int = 0): def get_children(worklist, url, acc):
count = count + 1
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
else: else:
data = worklist.pop() node = worklist.pop()
id = data['id'] id = node.data['id']
old = f'{url}/{id}/children' old = f'{url}/{id}/children'
# typer.echo(url) response = requests.get(old, cookies=cookies)
response = requests.get(old, cookies = cookies)
if response.status_code == 403 or response.status_code == 404: if response.status_code == 403 or response.status_code == 404:
typer.echo(response.json()['status']) typer.echo(response.json()['status'])
typer.echo(response.json()['message']) typer.echo(response.json()['message'])
return acc return acc
else: else:
child = response.json()['results'] children = response.json()['results']
for i in range(len(child)): for i in range(len(children)):
if key in child[i] and child[i][key] == True: # TODO: Add list of children instead of bool
worklist.append(child[i]) if key in children[i] and children[i][key] == True:
child = Node(children[i], True, node)
worklist.append(child)
acc.append(child)
else: else:
acc.append(child[i]) child = Node(children[i], False, node)
# parent = worklist.pop() acc.append(child)
return get_children(worklist, url, acc, count) return get_children(worklist, url, acc)
def create_tree(nodes):
parents = []
root = nodes.pop(0)
root_node = Nd(root.data['title'])
parent = root_node
parents.append(parent)
for i in range(len(nodes)):
if (nodes[i].children and nodes[i] not in parents):
for parent in parents:
if (parent == nodes[i].parent.data['title']):
node = Nd(nodes[i].data['title'], parent)
parents.append(node)
continue
node = Nd(nodes[i].data['title'], root_node)
parents.append(node)
elif (nodes[i].children):
for parent in parents:
if (nodes[i].parent.data['title'] == parent):
node = Nd(node.data['title'], parent)
if (nodes[i].children is False):
for parent in parents:
if (parent.name == nodes[i].parent.data['title']):
node = Nd(nodes[i].data['title'], parent)
for pre, fill, node in RenderTree(root_node):
print("%s%s" % (pre, node.name))
@app.command(name='get-assignments') @app.command(name='get-assignments')
...@@ -127,14 +158,9 @@ def get_assignments(course_id: str = typer.Argument('_27251_1', help='The course ...@@ -127,14 +158,9 @@ def get_assignments(course_id: str = typer.Argument('_27251_1', help='The course
url = f'{base_url}courses/{course_id}/contents' url = f'{base_url}courses/{course_id}/contents'
x = requests.get(url, cookies=cookies) x = requests.get(url, cookies=cookies)
data = x.json()['results'] data = x.json()['results']
root = data[8] # root = data[8]
# root = Node(data[8]) for root in data:
worklist = [root] root = Node(root, True)
res = get_children(worklist, url, []) worklist = [root]
res = get_children(worklist, url, [])
for i in res: create_tree(res)
print(i['title'])
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