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
2121eed9
Commit
2121eed9
authored
2 years ago
by
williamforbrigd
Browse files
Options
Downloads
Patches
Plain Diff
making controller
parent
78714d17
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
bbcli/cli.py
+1
-2
1 addition, 2 deletions
bbcli/cli.py
bbcli/controllers/content_controller.py
+160
-0
160 additions, 0 deletions
bbcli/controllers/content_controller.py
with
161 additions
and
2 deletions
bbcli/cli.py
+
1
−
2
View file @
2121eed9
...
...
@@ -3,7 +3,7 @@ from typing import Optional
from
pkg_resources
import
EntryPoint
# import typer
from
bbcli
import
__app_name__
,
__version__
from
bbcli.endpoints
import
get_user
,
get_course_contents
,
get_assignments
,
get_contents
from
bbcli.endpoints
import
get_user
,
get_course_contents
,
get_contents
import
os
from
dotenv
import
load_dotenv
from
bbcli
import
check_valid_date
,
check_response
...
...
@@ -19,7 +19,6 @@ def entry_point():
entry_point
.
add_command
(
get_user
)
entry_point
.
add_command
(
get_course_contents
)
entry_point
.
add_command
(
get_assignments
)
entry_point
.
add_command
(
get_contents
)
load_dotenv
()
...
...
This diff is collapsed.
Click to expand it.
bbcli/controllers/content_controller.py
0 → 100644
+
160
−
0
View file @
2121eed9
import
time
import
requests
import
bbcli.cli
as
cli
import
click
from
anytree
import
Node
as
Nd
,
RenderTree
from
bbcli
import
check_response
from
bbcli.entities.Node
import
Node
from
bbcli.entities.Node2
import
Node2
from
bbcli.utils.URL_builder
import
URLBuilder
url_builder
=
URLBuilder
()
base_url
=
'
https://ntnu.blackboard.com/learn/api/public/v1/
'
def
get_children
(
session
,
worklist
,
url
,
acc
,
count
:
int
=
0
):
count
=
count
+
1
key
=
'
hasChildren
'
if
len
(
worklist
)
==
0
:
return
acc
else
:
node
=
worklist
.
pop
()
id
=
node
.
data
[
'
id
'
]
tmp
=
url
[:
url
.
index
(
'
contents
'
)
+
len
(
'
contents
'
)]
old
=
f
'
{
tmp
}
/
{
id
}
/children
'
response
=
session
.
get
(
old
,
cookies
=
cli
.
cookies
)
if
check_response
(
response
)
==
False
:
return
acc
else
:
children
=
response
.
json
()[
'
results
'
]
for
i
in
range
(
len
(
children
)):
# TODO: Add list of children instead of bool
if
key
in
children
[
i
]
and
children
[
i
][
key
]
==
True
:
# if children[i]['contentHandler'] == content_types['folder']:
child
=
Node
(
children
[
i
],
True
,
node
)
worklist
.
append
(
child
)
acc
.
append
(
child
)
else
:
child
=
Node
(
children
[
i
],
False
,
node
)
acc
.
append
(
child
)
return
get_children
(
session
,
worklist
,
url
,
acc
)
def
get_children2
(
session
,
worklist
,
url
,
acc
,
count
:
int
=
0
):
count
=
count
+
1
key
=
'
hasChildren
'
if
len
(
worklist
)
==
0
:
return
else
:
node
=
worklist
.
pop
()
id
=
node
.
data
[
'
id
'
]
tmp
=
url
[:
url
.
index
(
'
contents
'
)
+
len
(
'
contents
'
)]
old
=
f
'
{
tmp
}
/
{
id
}
/children
'
response
=
session
.
get
(
old
,
cookies
=
cli
.
cookies
)
if
check_response
(
response
)
==
False
:
return
acc
else
:
children
=
response
.
json
()[
'
results
'
]
parent
=
Node2
(
node
)
for
i
in
range
(
len
(
children
)):
# TODO: Add list of children instead of bool
if
key
in
children
[
i
]
and
children
[
i
][
key
]
==
True
:
# if children[i]['contentHandler'] == content_types['folder']:
# child = Node(children[i], True, parent)
child
=
Node2
(
children
[
i
])
parent
.
children
.
append
(
child
)
worklist
.
append
(
child
)
# acc.append(child)
else
:
child
=
Node2
(
children
[
i
])
parent
.
children
.
append
(
child
)
# child = Node(children[i], False, parent)
# acc.append(child)
return
get_children
(
session
,
worklist
,
url
,
acc
)
@click.command
(
name
=
'
get-contents
'
)
@click.argument
(
'
course_id
'
,
default
=
'
_27251_1
'
)
@click.option
(
'
--folder-id
'
)
def
get_contents
(
course_id
:
str
,
folder_id
=
None
):
'''
Get the contents
\n
Folders are blue and have an id
\n
Files are white
'''
session
=
requests
.
Session
()
if
folder_id
is
not
None
:
url
=
f
'
{
base_url
}
courses/
{
course_id
}
/contents/
{
folder_id
}
'
else
:
url
=
f
'
{
base_url
}
courses/
{
course_id
}
/contents
'
start
=
time
.
time
()
response
=
session
.
get
(
url
,
cookies
=
cli
.
cookies
)
if
check_response
(
response
)
==
False
:
return
else
:
if
folder_id
is
not
None
:
data
=
response
.
json
()
root
=
Node
(
data
,
True
)
worklist
=
[
root
]
res
=
get_children
(
session
,
worklist
,
url
,
[])
create_tree
(
root
,
res
)
else
:
folders
=
response
.
json
()[
'
results
'
]
root
=
None
for
folder
in
folders
:
root
=
Node
(
folder
,
True
)
worklist
=
[
root
]
get_children
(
session
,
worklist
,
url
,
[])
print
(
root
.
data
[
'
title
'
])
for
child
in
root
.
children
:
print
(
child
.
data
[
'
title
'
])
end
=
time
.
time
()
print
(
f
'
\n
download time:
{
end
-
start
}
seconds
'
)
def
list_tree
(
root
,
contents
):
for
content
in
contents
:
parent
=
Nd
(
content
.
parent
.
data
[
'
title
'
])
this
=
Nd
(
content
.
data
[
'
title
'
],
parent
)
for
pre
,
fill
,
node
in
RenderTree
(
root_node
):
print
(
"
%s%s
"
%
(
pre
,
node
.
name
))
def
create_tree
(
root
,
nodes
):
parents
=
[]
root_node
=
Nd
(
root
.
data
[
'
title
'
])
parent
=
root_node
parents
.
append
(
parent
)
folders
=
dict
()
folders
[
root
.
data
[
'
title
'
]]
=
root
.
data
[
'
id
'
]
for
i
in
range
(
len
(
nodes
)):
id
=
nodes
[
i
].
data
[
'
id
'
]
title
=
nodes
[
i
].
data
[
'
title
'
]
if
(
nodes
[
i
].
has_children
):
for
parent
in
parents
:
if
(
parent
.
name
==
nodes
[
i
].
parent
.
data
[
'
title
'
]):
node
=
Nd
(
title
,
parent
)
folders
[
title
]
=
id
parents
.
append
(
node
)
continue
node
=
Nd
(
title
,
root_node
)
folders
[
title
]
=
id
parents
.
append
(
node
)
else
:
for
parent
in
parents
:
if
(
parent
.
name
==
nodes
[
i
].
parent
.
data
[
'
title
'
]):
node
=
Nd
(
title
,
parent
)
folders
[
title
]
=
''
for
pre
,
fill
,
node
in
RenderTree
(
root_node
):
folder_id
=
folders
[
node
.
name
]
if
folder_id
==
''
:
print
(
"
%s%s
"
%
(
pre
,
node
.
name
))
else
:
print
(
f
'
{
pre
}{
Fore
.
BLUE
}{
folder_id
}
{
node
.
name
}
{
Style
.
RESET_ALL
}
'
)
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