commit 510eaa81f4594ea87c2838fefdbf971a6cf45ebb Author: Sean Corrigan Date: Tue Feb 8 23:47:38 2022 -0500 init commit diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..34d5df0 --- /dev/null +++ b/readme.md @@ -0,0 +1,18 @@ +## Strategic Transfer of Resources Incase (K)anvas Extinct +STRIKE + +Very simple to use, install dependencies, generate your canvas API token and then run + +**Make Sure you install the module requests with ```pip3 install requests```** + +#### Generate your canvas API token + +Login to canvas @ https://learn.ontariotechu.net + +- Go to the sidebar and click on your profile. +- Then goto settings and scroll down to approved integrations and click "+ New Access Token" +(For the purpose enter whatever you want) and then click "Generate Token" + +#### Run the script + +When running the script it will prompt for your api key, just paste it in and hit enter/return \ No newline at end of file diff --git a/strike.py b/strike.py new file mode 100644 index 0000000..38c27f8 --- /dev/null +++ b/strike.py @@ -0,0 +1,82 @@ +import requests, json, os +from pathlib import Path + +# STRIKE +# Strategic Transfer of Resources Incase (K)anvas Extinct + +token = input('Paste your token here: ').replace(' ','') # the whole thing to the right of token, including 13377~ + +apikey = {'Authorization' : f'Bearer {token}'} + + +canvasURL = 'https://learn.ontariotechu.ca/' + + +filecount = 0 + +def getCourses(): + url = f'{canvasURL}api/v1/courses/?per_page=100' + re = requests.get(url, headers=apikey) + courses = json.loads(re.content.decode()) + courseids = {} + for course in courses: + if len(course) > 4: + CourseName = course['name'].replace("\\", ' ').replace("/", ' ') + courseids[CourseName] = course['id'] + return courseids + +def saveAnnouncements(courseid, name): ## IDK kinda broken seems to not + try: + os.makedirs(str(name)) + except: pass + file = open(f'{name}/announcements.txt', 'w+') + url = f'{canvasURL}/api/v1/courses/{courseid}/activity_stream' + re = requests.get(url, headers=apikey) + announcements = json.loads(re.content.decode()) + for a in announcements: + if a['type'] == 'Announcement': + file.write(f"{a['type']}{a['title']} @ {a['created_at']}\n{a['message']}\n\n") + file.close() + +### https://learn.ontariotechu.ca/api/v1/courses/16750/assignments/95412/ submissions API ??? + +def getModules(course, name): + global filecount + try: + os.makedirs(str(name)) + except: pass + try: + os.makedirs(str(f'{name}/files/')) + except: pass + url = f'{canvasURL}api/v1/courses/{course}/modules/' + re = requests.get(url, headers=apikey).content + modules = json.loads(re.decode()) + for module in modules: ## Each Module + items = json.loads(requests.get(module['items_url'], headers=apikey).content) + for item in items: ## For each item attached + if item['type'] == 'File': + if not os.path.isfile(f"{name}/files/{item['title']}"): + filecount += 1 + print(f"\t \t Downloading {item['title']}") + ## Grab the attachment for the module + attachment = json.loads(requests.get(item['url'], headers=apikey).content) + ## Write File that was at the URL + try: + file = requests.get(attachment['url']).content + Path(f"{name}/files/{attachment['display_name']}").write_bytes(file) + except: + print('****Download Failed****') + else: + filecount += 1 + print(f"File {item['title']} Exists SKIPPING") +def main(): + courses = getCourses() + for course in courses: + print('+'*20) + print(f'Downloading Content for {course}...') + print('+'*20) + saveAnnouncements(courses[course], course) + getModules(courses[course],course) + print(f'Archive Completed, downloaded {str(filecount)} files for {str(len(courses))} Courses') + +main() \ No newline at end of file