Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
IDATT2900-072
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mattias Agentoft Eggen
IDATT2900-072
Commits
db1ac70d
Commit
db1ac70d
authored
3 years ago
by
williamforbrigd
Browse files
Options
Downloads
Patches
Plain Diff
login if user has not done it yet, cannot login and rund command at the simultaneously
parent
82d0872a
No related branches found
No related tags found
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
bbcli/__init__.py
+3
-2
3 additions, 2 deletions
bbcli/__init__.py
bbcli/__main__.py
+1
-23
1 addition, 23 deletions
bbcli/__main__.py
bbcli/cli.py
+26
-0
26 additions, 0 deletions
bbcli/cli.py
bbcli/endpoints.py
+8
-28
8 additions, 28 deletions
bbcli/endpoints.py
bbcli/login.py
+2
-1
2 additions, 1 deletion
bbcli/login.py
with
40 additions
and
54 deletions
bbcli/__init__.py
+
3
−
2
View file @
db1ac70d
...
...
@@ -3,8 +3,9 @@
__app_name__
=
"
bbcli
"
__version__
=
"
0.1.0
"
from
.endpoints
import
*
from
.Node
import
Node
# from .endpoints import *
from
.Node
import
*
from
.login
import
*
(
SUCCESS
,
...
...
This diff is collapsed.
Click to expand it.
bbcli/__main__.py
+
1
−
23
View file @
db1ac70d
"""
RP To-Do entry point script.
"""
# rptodo/__main__.py
import
os
from
dotenv
import
load_dotenv
from
bbcli
import
cli
,
__app_name__
,
login
from
datetime
import
datetime
load_dotenv
()
cookies
=
{
'
BbRouter
'
:
os
.
getenv
(
"
BB_ROUTER
"
)}
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
from
bbcli
import
cli
,
__app_name__
def
main
():
if
check_valid_date
()
==
False
:
login
()
cli
.
app
(
prog_name
=
__app_name__
)
if
__name__
==
"
__main__
"
:
...
...
This diff is collapsed.
Click to expand it.
bbcli/cli.py
+
26
−
0
View file @
db1ac70d
...
...
@@ -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
This diff is collapsed.
Click to expand it.
bbcli/endpoints.py
+
8
−
28
View file @
db1ac70d
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
bbcli.Node
import
Node
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
()
...
...
@@ -102,7 +82,7 @@ def get_children(worklist, url, acc, count: int = 0):
id
=
data
[
'
id
'
]
old
=
f
'
{
url
}
/
{
id
}
/children
'
# typer.echo(url)
response
=
requests
.
get
(
old
,
cookies
=
cookies
)
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
'
])
...
...
@@ -125,7 +105,7 @@ 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
'
]
root
=
data
[
8
]
# root = Node(data[8])
...
...
This diff is collapsed.
Click to expand it.
bbcli/login.py
+
2
−
1
View file @
db1ac70d
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
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment