Skip to content
Snippets Groups Projects
Commit 860b4496 authored by Hans William Forbrigd's avatar Hans William Forbrigd
Browse files

Merge branch 'william_feature_click' into 'main'

switched from typer to click and added setup file

See merge request mattiaae/idatt2900-072!10
parents bfa6a3c5 54f63432
No related branches found
No related tags found
No related merge requests found
......@@ -7,6 +7,7 @@ __version__ = "0.1.0"
from .Node import *
from .Utils.utils import *
from .Services import login
# from endpoints import get_user, get_course, get_assignments, get_course_contents
(
SUCCESS,
......
"""RP To-Do entry point script."""
# rptodo/__main__.py
from bbcli import cli, __app_name__
from bbcli import __app_name__
from .cli import entry_point
def main():
cli.app(prog_name=__app_name__)
entry_point()
if __name__ == "__main__":
main()
\ No newline at end of file
from typing import Optional
import typer
from bbcli import __app_name__, __version__, endpoints
from pkg_resources import EntryPoint
# import typer
from bbcli import __app_name__, __version__
from bbcli.endpoints import get_user, get_course, get_course_contents, get_assignments
import os
from dotenv import load_dotenv
from bbcli import login, check_valid_date
from bbcli import check_valid_date
import click
from bbcli.Services import AuthorizationService
app = typer.Typer()
app.add_typer(endpoints.app, name='endpoints', help='Call the endpoints')
@click.group()
def entry_point():
authorize_user()
pass
entry_point.add_command(get_user)
entry_point.add_command(get_course)
entry_point.add_command(get_course_contents)
entry_point.add_command(get_assignments)
# 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 _version_callback(value: bool) -> None:
if value:
typer.echo(f'{__app_name__} v{__version__}')
raise typer.Exit()
@app.callback()
def main(
version: Optional[bool] = typer.Option(
None,
'--version',
'-v',
help='Show the applications version and exit.',
callback=_version_callback,
is_eager=True,
)
) -> None:
if check_valid_date(cookies) == False:
AuthorizationService.login()
# load_dotenv()
# cookies['BbRouter'] = os.getenv("BB_ROUTER")
# headers['X-Blackboard-XSRF'] = os.getenv("XSRF")
return
# def _version_callback(value: bool) -> None:
# if value:
# typer.echo(f'{__app_name__} v{__version__}')
# raise typer.Exit()
# @app.callback()
# def main(
# version: Optional[bool] = typer.Option(
# None,
# '--version',
# '-v',
# help='Show the applications version and exit.',
# callback=_version_callback,
# is_eager=True,
# )
# ) -> None:
# if check_valid_date(cookies) == False:
# AuthorizationService.login()
# # load_dotenv()
# # cookies['BbRouter'] = os.getenv("BB_ROUTER")
# # headers['X-Blackboard-XSRF'] = os.getenv("XSRF")
# return
#----- AUTHORIZATION MODULE -----#
@app.command(name='login', help='Authorize the user.')
# @app.command(name='login', help='Authorize the user.')
def authorize_user():
AuthorizationService.login()
if check_valid_date(cookies) == False:
AuthorizationService.login()
#----- COURSE MODULE -----#
@app.command(name='course')
def course(
course_id: Optional[str] = typer.Argument(None, help='The id of the course you want.'),
favorites: bool = typer.Option(False, help='List only your favorite courses.')):
if course_id != None and favorites == False:
# CODE FOR GETTING SPESIFIC COURSE
# @app.command(name='course')
# def course(
# course_id: Optional[str] = typer.Argument(None, help='The id of the course you want.'),
# favorites: bool = typer.Option(False, help='List only your favorite courses.')):
# if course_id != None and favorites == False:
# # CODE FOR GETTING SPESIFIC COURSE
print('getting spesific course...')
elif course_id != None and favorites == True:
# CODE FOR GETTING SPESIFIC FAVORITE COURSE
# print('getting spesific course...')
# elif course_id != None and favorites == True:
# # CODE FOR GETTING SPESIFIC FAVORITE COURSE
print('getting spesific favorite course...')
else:
# CODE FOR GETTING ALL COURSES
# print('getting spesific favorite course...')
# else:
# # CODE FOR GETTING ALL COURSES
print('getting all courses...')
\ No newline at end of file
# print('getting all courses...')
\ No newline at end of file
......@@ -2,31 +2,30 @@ import requests
#from requests.auth import HTTPBasicAuth
# import json
# import pprint
import typer
import bbcli.cli as cli
#from string_builder import StringBuilder
import click
from typing import Optional
from dotenv import load_dotenv
# from typing import Optional
# from dotenv import load_dotenv
# from anytree import Node, RenderTree
import os
app = typer.Typer()
from bbcli import check_response
base_url = 'https://ntnu.blackboard.com/learn/api/public/v1/'
@app.command(name='get-user')
def get_user(user_name: str = typer.Argument('', help='Name of the user'))-> None:
@click.command(name='get-user')
@click.argument('user_name', default='')
def get_user(user_name: str):
'''
Get the user
Specify the user_name as an option, or else it will use the default user_name
'''
if user_name == '':
user_name = typer.prompt("What is your user name?")
user_name = click.prompt("What is your user name?")
url = f'{base_url}users?userName={user_name}'
response = requests.get(
url,
......@@ -35,22 +34,23 @@ def get_user(user_name: str = typer.Argument('', help='Name of the user'))-> Non
if check_response(response) == False:
return
else:
data = x.json()['results'][0]
data = response.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}')
click.echo(f'Name of the student: {fn} {sn}')
click.echo(f'The student id: {id}')
@app.command(name='get-course')
def get_course(course_id: str = typer.Argument('', help='Id of the course')):
@click.command(name='get-course')
@click.argument('course_id', default='_27251_1')
def get_course(course_id: str):
'''
Get the course
'''
if course_id == '':
course_id = typer.prompt("What is the course id?")
course_id = click.prompt("What is the course id?")
url = f'{base_url}courses?courseId={course_id}'
response = requests.get(
url,
......@@ -58,35 +58,36 @@ def get_course(course_id: str = typer.Argument('', help='Id of the course')):
if check_response(response) == False:
return
else:
data = x.json()['results'][0]
data = response.json()['results'][0]
name = data['name']
course_url = data['externalAccessUrl']
typer.echo(name)
typer.echo(f'URL for the course: {course_url}')
click.echo(name)
click.echo(f'URL for the course: {course_url}')
@app.command(name='get-course-contents')
def get_course_contents(course_id: str = '_27251_1'):
@click.command(name='get-course-contents')
@click.argument('course_id', default='_27251_1')
def get_course_contents(course_id: str):
'''
Get the course contents
'''
url = f'{base_url}courses/{course_id}/contents'
typer.echo(url)
click.echo(url)
response = requests.get(url, cookies=cli.cookies)
if check_response(response) == False:
return
else:
data = response.json()['results']
typer.echo('Mapper:')
click.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)
click.echo(f'{i+1} {title}')
click.echo(map)
def get_children(worklist, url, acc, count: int = 0):
count = count + 1
typer.echo(f'kommer hit: {count}')
click.echo(f'kommer hit: {count}')
key = 'hasChildren'
if len(worklist) == 0:
return acc
......@@ -108,8 +109,9 @@ def get_children(worklist, url, acc, count: int = 0):
@app.command(name='get-assignments')
def get_assignments(course_id: str = typer.Argument('_27251_1', help='The course id')):
@click.command(name='get-assignments')
@click.argument('course_id', default='_27251_1')
def get_assignments(course_id: str):
'''
Get the assignments
'''
......
setup.py 0 → 100644
from setuptools import setup
setup(
name='cli',
# version='0.1.0',
# py_modules=['yourscript'],
install_requires=[
'Click',
],
entry_points={
'console_scripts': [
'bb=bbcli.__main__:main', # the cli() function runs inside the bbcli.py. cli is the command that is used to run the command
],
},
)
\ No newline at end of file
home = /usr/bin
home = /Library/Frameworks/Python.framework/Versions/3.9/bin
include-system-site-packages = false
version = 3.8.10
version = 3.9.10
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