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
bb37389a
Commit
bb37389a
authored
3 years ago
by
williamforbrigd
Browse files
Options
Downloads
Patches
Plain Diff
made some utils classes
parent
db1ac70d
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
bbcli/__init__.py
+1
-0
1 addition, 0 deletions
bbcli/__init__.py
bbcli/cli.py
+2
-16
2 additions, 16 deletions
bbcli/cli.py
bbcli/endpoints.py
+45
-42
45 additions, 42 deletions
bbcli/endpoints.py
bbcli/utils.py
+33
-0
33 additions, 0 deletions
bbcli/utils.py
with
81 additions
and
58 deletions
bbcli/__init__.py
+
1
−
0
View file @
bb37389a
...
...
@@ -6,6 +6,7 @@ __version__ = "0.1.0"
# from .endpoints import *
from
.Node
import
*
from
.login
import
*
from
.utils
import
*
(
SUCCESS
,
...
...
This diff is collapsed.
Click to expand it.
bbcli/cli.py
+
2
−
16
View file @
bb37389a
...
...
@@ -8,8 +8,7 @@ import typer
from
bbcli
import
__app_name__
,
__version__
,
endpoints
import
os
from
dotenv
import
load_dotenv
from
datetime
import
datetime
from
bbcli
import
login
from
bbcli
import
login
,
check_valid_date
app
=
typer
.
Typer
()
...
...
@@ -19,19 +18,6 @@ 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__
}
"
)
...
...
@@ -48,7 +34,7 @@ def main(
is_eager
=
True
,
)
)
->
None
:
if
check_valid_date
()
==
False
:
if
check_valid_date
(
cookies
)
==
False
:
login
()
# load_dotenv()
# cookies['BbRouter'] = os.getenv("BB_ROUTER")
...
...
This diff is collapsed.
Click to expand it.
bbcli/endpoints.py
+
45
−
42
View file @
bb37389a
...
...
@@ -5,7 +5,7 @@ import requests
import
typer
import
bbcli.cli
as
cli
app
=
typer
.
Typer
()
from
bbcli
import
check_response
base_url
=
'
https://ntnu.blackboard.com/learn/api/public/v1/
'
...
...
@@ -19,18 +19,20 @@ def get_user(user_name: str = typer.Argument('', help='Name of the user'))-> Non
if
user_name
==
''
:
user_name
=
typer
.
prompt
(
"
What is your user name?
"
)
url
=
f
'
{
base_url
}
users?userName=
{
user_name
}
'
x
=
requests
.
get
(
response
=
requests
.
get
(
url
,
cookies
=
cli
.
cookies
)
if
check_response
(
response
)
==
False
:
return
else
:
data
=
x
.
json
()[
'
results
'
][
0
]
fn
=
data
[
'
name
'
][
'
given
'
]
sn
=
data
[
'
name
'
][
'
family
'
]
id
=
data
[
'
studentId
'
]
data
=
x
.
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
}
'
)
typer
.
echo
(
f
'
Name of the student:
{
fn
}
{
sn
}
'
)
typer
.
echo
(
f
'
The student id:
{
id
}
'
)
@app.command
(
name
=
'
get-course
'
)
...
...
@@ -41,14 +43,17 @@ def get_course(course_id: str = typer.Argument('', help='Id of the course')):
if
course_id
==
''
:
course_id
=
typer
.
prompt
(
"
What is the course id?
"
)
url
=
f
'
{
base_url
}
courses?courseId=
{
course_id
}
'
x
=
requests
.
get
(
response
=
requests
.
get
(
url
,
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
}
'
)
if
check_response
(
response
)
==
False
:
return
else
:
data
=
x
.
json
()[
'
results
'
][
0
]
name
=
data
[
'
name
'
]
course_url
=
data
[
'
externalAccessUrl
'
]
typer
.
echo
(
name
)
typer
.
echo
(
f
'
URL for the course:
{
course_url
}
'
)
@app.command
(
name
=
'
get-course-contents
'
)
def
get_course_contents
(
course_id
:
str
=
'
_27251_1
'
):
...
...
@@ -57,23 +62,22 @@ 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
=
cli
.
cookies
)
data
=
x
.
json
()[
'
results
'
]
typer
.
echo
(
'
Mapper:
'
)
map
=
dict
()
for
i
in
range
(
len
(
data
)):
t
itle
=
data
[
i
][
'
title
'
]
map
[
i
+
1
]
=
data
[
i
][
'
id
'
]
typer
.
echo
(
f
'
{
i
+
1
}
{
title
}
'
)
# idx = typer.prompt("Open a folder by pressing a number: ")
typer
.
echo
(
map
)
# for d in data:
#
typer.echo(
d['title']
)
response
=
requests
.
get
(
url
,
cookies
=
cli
.
cookies
)
if
check_response
(
response
)
==
False
:
return
else
:
data
=
response
.
json
()[
'
results
'
]
t
yper
.
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
)
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
len
(
worklist
)
==
0
:
return
acc
...
...
@@ -81,11 +85,8 @@ def get_children(worklist, url, acc, count: int = 0):
data
=
worklist
.
pop
()
id
=
data
[
'
id
'
]
old
=
f
'
{
url
}
/
{
id
}
/children
'
# 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
'
])
if
check_response
(
response
)
==
False
:
return
acc
else
:
child
=
response
.
json
()[
'
results
'
]
...
...
@@ -94,7 +95,6 @@ def get_children(worklist, url, acc, count: int = 0):
worklist
.
append
(
child
[
i
])
else
:
acc
.
append
(
child
[
i
])
# parent = worklist.pop()
return
get_children
(
worklist
,
url
,
acc
,
count
)
...
...
@@ -105,15 +105,18 @@ 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
=
cli
.
cookies
)
data
=
x
.
json
()[
'
results
'
]
root
=
data
[
8
]
# root = Node(data[8])
worklist
=
[
root
]
res
=
get_children
(
worklist
,
url
,
[])
for
i
in
res
:
print
(
i
[
'
title
'
])
response
=
requests
.
get
(
url
,
cookies
=
cli
.
cookies
)
if
check_response
(
response
)
==
False
:
return
else
:
data
=
response
.
json
()[
'
results
'
]
root
=
data
[
8
]
# root = Node(data[8])
worklist
=
[
root
]
res
=
get_children
(
worklist
,
url
,
[])
for
i
in
res
:
print
(
i
[
'
title
'
])
...
...
This diff is collapsed.
Click to expand it.
bbcli/utils.py
0 → 100644
+
33
−
0
View file @
bb37389a
from
datetime
import
datetime
def
check_valid_key
(
obj
,
key
)
->
bool
:
# print("the keys are", obj.keys())
if
key
not
in
obj
.
keys
():
print
(
f
'
The key:
\"
{
key
}
\"
is not in the object
'
)
return
False
else
:
return
True
def
check_response
(
response
)
->
bool
:
invalid_statuscodes
=
[
401
,
403
,
404
]
if
response
.
status_code
in
invalid_statuscodes
:
print
(
response
.
json
()[
'
status
'
])
print
(
response
.
json
()[
'
message
'
])
return
False
else
:
return
True
def
check_valid_date
(
cookies
)
->
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
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