-
Notifications
You must be signed in to change notification settings - Fork 0
/
getBlackboardDirectory.py
121 lines (111 loc) · 4.95 KB
/
getBlackboardDirectory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import os
import base64
import json
import requests
import urllib3
import time
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def downloadLink(input_id, input_path):
r = requests.get(f'{school_url}/learn/api/public/v1/courses/{course_id}/contents/{input_id}/attachments', headers=headers, verify=False)
attachments_json = r.json()
if attachments_json['results'] == []:
pass
else:
for item in attachments_json['results']:
attachment_id = item['id']
title = item['fileName']
r = requests.get(f'{school_url}/learn/api/public/v1/courses/{course_id}/contents/{input_id}/attachments/{attachment_id}/download', headers=headers, allow_redirects=True, verify=False)
with open(f'{input_path}{title}', 'wb') as file:
file.write(r.content)
def downloadFile(input_id, input_path):
r = requests.get(f'{school_url}/learn/api/public/v1/courses/{course_id}/contents/{input_id}/attachments', headers=headers, verify=False)
attachments_json = r.json()
if attachments_json['results'] == []:
pass
else:
for item in attachments_json['results']:
attachment_id = item['id']
title = item['fileName']
r = requests.get(f'{school_url}/learn/api/public/v1/courses/{course_id}/contents/{input_id}/attachments/{attachment_id}/download', headers=headers, allow_redirects=True, verify=False)
url = r.url.rsplit("?", 1)[0] + " "
extension = url[-4:]
with open(f'{input_path}{title}.{extension}', 'wb') as file:
file.write(r.content)
def iterateDirectory(input_json, input_path):
root_path = input_path
for item in input_json['results']:
if item['availability']['allowGuests'] == False:
continue
if item['contentHandler']['id'] == 'resource/x-bb-document':
layer_id = item['id']
downloadFile(layer_id, root_path)
if item['contentHandler']['id'] == 'resource/x-bb-externallink':
url = item['contentHandler']['url']
title = item['id']
with open(f'{root_path}{title}.txt', 'wb') as file:
file.write(url.encode())
if item['contentHandler']['id'] == 'resource/x-bb-file':
layer_id = item['id']
downloadLink(layer_id, root_path)
if item['contentHandler']['id'] == 'resource/x-bb-assignment':
layer_id = item['id']
downloadLink(layer_id, root_path)
if item['contentHandler']['id'] == 'resource/x-bb-courselink':
bug = item['id']
layer_id = item['contentHandler']['targetId']
r = requests.get(f'{school_url}/learn/api/public/v1/courses/{course_id}/contents/{layer_id}', headers=headers, verify=False)
verify_json = r.json()
if verify_json['contentHandler']['id'] == 'resource/x-bb-file':
downloadLink(layer_id, root_path)
else:
continue
if item['contentHandler']['id'] == 'resource/x-bb-folder':
layer_id = item['id']
title = item['title']
if '/' in title:
title = title.replace('/', '-')
if ':' in title:
title = title.replace(':', '')
if '"' in title:
title = title.replace('"', '')
if '"' in title:
title = title.replace('\\', '-')
if '*' in title:
title = title.replace('*', '')
if '?' in title:
title = title.replace('?', '')
layer_path = f'{root_path}{title}/'
os.mkdir(layer_path)
r = requests.get(f'{school_url}/learn/api/public/v1/courses/{course_id}/contents/{layer_id}/children', headers=headers, verify=False)
layer_JSON = r.json()
iterateDirectory(layer_JSON, layer_path)
print('\n')
time.sleep(1)
print('BLACKBOARD COURSE DOWNLOADER: https://github.com/katurian/getBlackboardDirectory')
time.sleep(1)
print('Buy me a coffee: https://ko-fi.com/katski')
time.sleep(1)
print('Hire me: https://katski.org/')
print('-----------------------------------------------')
time.sleep(.5)
print('\n')
school_url = input("Enter your school's Blackboard site in this format: blackboard.MYSCHOOL.edu \n")
school_url = f'https://{school_url}'
print('\n')
s_session_id = input("Enter the s_session_id associated with your Blackboard account: \n")
print('\n')
course_id = input("Enter the ID for the course whose directory you want to download: \n")
print('\n')
content_id = input("Enter the content ID for the folder or section to download: \n")
print('\n')
headers = {
'Cookie': f's_session_id={s_session_id}',
}
r = requests.get(f'{school_url}/learn/api/public/v1/courses/{course_id}/contents/{content_id}/children', headers=headers, verify=False)
root_json = r.json()
root_path = ''
print("Working...")
print('\n')
iterateDirectory(root_json, root_path)
print('\n')
print('Done!')