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

switched from typer to click and added setup file

parent bfa6a3c5
No related branches found
No related tags found
No related merge requests found
...@@ -7,6 +7,7 @@ __version__ = "0.1.0" ...@@ -7,6 +7,7 @@ __version__ = "0.1.0"
from .Node import * from .Node import *
from .Utils.utils import * from .Utils.utils import *
from .Services import login from .Services import login
# from endpoints import get_user, get_course, get_assignments, get_course_contents
( (
SUCCESS, SUCCESS,
......
"""RP To-Do entry point script.""" from bbcli import __app_name__
# rptodo/__main__.py from .cli import entry_point
from bbcli import cli, __app_name__
def main(): def main():
cli.app(prog_name=__app_name__) entry_point()
if __name__ == "__main__": if __name__ == "__main__":
main() main()
\ No newline at end of file
from typing import Optional from typing import Optional
import typer from pkg_resources import EntryPoint
from bbcli import __app_name__, __version__, endpoints # import typer
from bbcli import __app_name__, __version__
from bbcli.endpoints import get_user, get_course, get_course_contents, get_assignments
import os import os
from dotenv import load_dotenv 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 from bbcli.Services import AuthorizationService
app = typer.Typer() @click.group()
app.add_typer(endpoints.app, name='endpoints', help='Call the endpoints') 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() 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 _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__}')
raise typer.Exit() # raise typer.Exit()
@app.callback() # @app.callback()
def main( # def main(
version: Optional[bool] = typer.Option( # version: Optional[bool] = typer.Option(
None, # None,
'--version', # '--version',
'-v', # '-v',
help='Show the applications version and exit.', # help='Show the applications version and exit.',
callback=_version_callback, # callback=_version_callback,
is_eager=True, # is_eager=True,
) # )
) -> None: # ) -> None:
if check_valid_date(cookies) == False: # if check_valid_date(cookies) == False:
AuthorizationService.login() # AuthorizationService.login()
# 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")
return # return
#----- AUTHORIZATION MODULE -----# #----- AUTHORIZATION MODULE -----#
@app.command(name='login', help='Authorize the user.') # @app.command(name='login', help='Authorize the user.')
def authorize_user(): def authorize_user():
AuthorizationService.login() if check_valid_date(cookies) == False:
AuthorizationService.login()
#----- COURSE MODULE -----# #----- COURSE MODULE -----#
@app.command(name='course') # @app.command(name='course')
def course( # def course(
course_id: Optional[str] = typer.Argument(None, help='The id of the course you want.'), # 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.')): # favorites: bool = typer.Option(False, help='List only your favorite courses.')):
if course_id != None and favorites == False: # if course_id != None and favorites == False:
# CODE FOR GETTING SPESIFIC COURSE # # CODE FOR GETTING SPESIFIC COURSE
print('getting spesific course...') # print('getting spesific course...')
elif course_id != None and favorites == True: # elif course_id != None and favorites == True:
# CODE FOR GETTING SPESIFIC FAVORITE COURSE # # CODE FOR GETTING SPESIFIC FAVORITE COURSE
print('getting spesific favorite course...') # print('getting spesific favorite course...')
else: # else:
# CODE FOR GETTING ALL COURSES # # CODE FOR GETTING ALL COURSES
print('getting all courses...') # print('getting all courses...')
\ No newline at end of file \ No newline at end of file
...@@ -2,31 +2,30 @@ import requests ...@@ -2,31 +2,30 @@ import requests
#from requests.auth import HTTPBasicAuth #from requests.auth import HTTPBasicAuth
# import json # import json
# import pprint # import pprint
import typer
import bbcli.cli as cli import bbcli.cli as cli
#from string_builder import StringBuilder #from string_builder import StringBuilder
import click import click
from typing import Optional # from typing import Optional
from dotenv import load_dotenv # from dotenv import load_dotenv
# from anytree import Node, RenderTree # from anytree import Node, RenderTree
import os import os
app = typer.Typer()
from bbcli import check_response 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/'
@app.command(name='get-user') @click.command(name='get-user')
def get_user(user_name: str = typer.Argument('', help='Name of the user'))-> None: @click.argument('user_name', default='')
def get_user(user_name: str):
''' '''
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
''' '''
if 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}' url = f'{base_url}users?userName={user_name}'
response = requests.get( response = requests.get(
url, url,
...@@ -35,22 +34,23 @@ def get_user(user_name: str = typer.Argument('', help='Name of the user'))-> Non ...@@ -35,22 +34,23 @@ def get_user(user_name: str = typer.Argument('', help='Name of the user'))-> Non
if check_response(response) == False: if check_response(response) == False:
return return
else: else:
data = x.json()['results'][0] data = response.json()['results'][0]
fn = data['name']['given'] fn = data['name']['given']
sn = data['name']['family'] sn = data['name']['family']
id = data['studentId'] id = data['studentId']
typer.echo(f'Name of the student: {fn} {sn}') click.echo(f'Name of the student: {fn} {sn}')
typer.echo(f'The student id: {id}') click.echo(f'The student id: {id}')
@app.command(name='get-course') @click.command(name='get-course')
def get_course(course_id: str = typer.Argument('', help='Id of the course')): @click.argument('course_id', default='_27251_1')
def get_course(course_id: str):
''' '''
Get the course Get the course
''' '''
if course_id == '': 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}' url = f'{base_url}courses?courseId={course_id}'
response = requests.get( response = requests.get(
url, url,
...@@ -58,35 +58,36 @@ def get_course(course_id: str = typer.Argument('', help='Id of the course')): ...@@ -58,35 +58,36 @@ def get_course(course_id: str = typer.Argument('', help='Id of the course')):
if check_response(response) == False: if check_response(response) == False:
return return
else: else:
data = x.json()['results'][0] data = response.json()['results'][0]
name = data['name'] name = data['name']
course_url = data['externalAccessUrl'] course_url = data['externalAccessUrl']
typer.echo(name) click.echo(name)
typer.echo(f'URL for the course: {course_url}') click.echo(f'URL for the course: {course_url}')
@app.command(name='get-course-contents') @click.command(name='get-course-contents')
def get_course_contents(course_id: str = '_27251_1'): @click.argument('course_id', default='_27251_1')
def get_course_contents(course_id: str):
''' '''
Get the course contents Get the course contents
''' '''
url = f'{base_url}courses/{course_id}/contents' url = f'{base_url}courses/{course_id}/contents'
typer.echo(url) click.echo(url)
response = requests.get(url, cookies=cli.cookies) response = requests.get(url, cookies=cli.cookies)
if check_response(response) == False: if check_response(response) == False:
return return
else: else:
data = response.json()['results'] data = response.json()['results']
typer.echo('Mapper:') click.echo('Mapper:')
map = dict() map = dict()
for i in range(len(data)): for i in range(len(data)):
title = data[i]['title'] title = data[i]['title']
map[i+1] = data[i]['id'] map[i+1] = data[i]['id']
typer.echo(f'{i+1} {title}') click.echo(f'{i+1} {title}')
typer.echo(map) click.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}') click.echo(f'kommer hit: {count}')
key = 'hasChildren' key = 'hasChildren'
if len(worklist) == 0: if len(worklist) == 0:
return acc return acc
...@@ -108,8 +109,9 @@ def get_children(worklist, url, acc, count: int = 0): ...@@ -108,8 +109,9 @@ def get_children(worklist, url, acc, count: int = 0):
@app.command(name='get-assignments') @click.command(name='get-assignments')
def get_assignments(course_id: str = typer.Argument('_27251_1', help='The course id')): @click.argument('course_id', default='_27251_1')
def get_assignments(course_id: str):
''' '''
Get the assignments 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 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