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
4c181fb9
Commit
4c181fb9
authored
3 years ago
by
magnus2142
Browse files
Options
Downloads
Patches
Plain Diff
Finished course services
parent
c041eb43
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
bbcli/Services/CourseService.py
+36
-9
36 additions, 9 deletions
bbcli/Services/CourseService.py
with
36 additions
and
9 deletions
bbcli/Services/CourseService.py
+
36
−
9
View file @
4c181fb9
import
json
from
typing
import
Dict
,
Any
import
requests
from
datetime
import
date
def
take_start_date
(
elem
):
return
date
.
fromisoformat
(
elem
[
'
availability
'
][
'
duration
'
][
'
start
'
].
split
(
'
T
'
)[
0
])
# TODO: use /term endpoint to determine whether the course is an active term
def
list_courses
(
cookies
:
Dict
,
user_name
:
str
)
->
Any
:
response
=
requests
.
get
(
'
https://ntnu.blackboard.com/learn/api/public/v1/users/userName:{}/courses
'
.
format
(
user_name
),
cookies
=
cookies
)
courses
=
json
.
loads
(
response
.
text
)[
'
results
'
]
detailed_course_list
=
[]
for
course
in
courses
:
detailed_course
=
list_course
(
cookies
=
cookies
,
course_id
=
course
[
'
courseId
'
])
detailed_course_list
.
append
(
detailed_course
)
return
detailed_course_list
session
=
requests
.
Session
()
terms
=
session
.
get
(
'
https://ntnu.blackboard.com/learn/api/public/v1/terms
'
,
cookies
=
cookies
)
terms
=
json
.
loads
(
terms
.
text
)[
'
results
'
]
# Sort terms by start date to get the two most recent semesters to determine which courses to show
for
term
in
terms
:
if
term
[
'
availability
'
][
'
duration
'
][
'
type
'
]
!=
'
DateRange
'
:
terms
.
remove
(
term
)
terms
.
sort
(
key
=
take_start_date
)
term_1
=
terms
[
len
(
terms
)
-
1
]
term_2
=
terms
[
len
(
terms
)
-
2
]
course_memberships
=
session
.
get
(
'
https://ntnu.blackboard.com/learn/api/public/v1/users/userName:{}/courses
'
.
format
(
user_name
),
cookies
=
cookies
)
course_memberships
=
json
.
loads
(
course_memberships
.
text
)[
'
results
'
]
course_list
=
[]
# Get courses from the correct terms
for
course
in
course_memberships
:
response
=
session
.
get
(
'
https://ntnu.blackboard.com/learn/api/public/v3/courses/{}
'
.
format
(
course
[
'
courseId
'
]),
cookies
=
cookies
,
params
=
{
'
fields
'
:
'
id, name, termId
'
})
response
=
json
.
loads
(
response
.
text
)
if
response
[
'
termId
'
]
==
term_1
[
'
id
'
]
or
response
[
'
termId
'
]
==
term_2
[
'
id
'
]:
course_list
.
append
({
'
id
'
:
response
[
'
id
'
],
'
name
'
:
response
[
'
name
'
]
})
else
:
break
return
course_list
def
list_course
(
cookies
:
Dict
,
course_id
:
str
)
->
Any
:
response
=
requests
.
get
(
'
https://ntnu.blackboard.com/learn/api/public/v3/courses/{}
'
.
format
(
course_id
),
cookies
=
cookies
)
...
...
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