Created
August 13, 2019 03:05
-
-
Save svict4/c238db5e8f74f50206fa331a1a1fc131 to your computer and use it in GitHub Desktop.
Exports Canvas LMS discussion forums and comments/replies/entries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I'm surprised this doesn't already exist | |
# Just add Institute, Course ID and your bearer token | |
# check settings, default only selects threads which are 'closed for comments' | |
import requests | |
import json | |
import urllib.request | |
# Define the Canvas Discussion Variable | |
INSTITUTE = "" | |
COURSE_ID = "" | |
# OAUTH token https://canvas.instructure.com/doc/api/file.oauth_endpoints.html#post-login-oauth2-token | |
headers = { | |
"Authorization": "" | |
} | |
# Settings | |
# closed for comments | |
# https://github.com/instructure/canvas-lms/blob/master/app/controllers/discussion_topics_controller.rb#L149 | |
data = {"scope": "locked"} #"per_page": 100 | |
CANVAS_URL = "https://%s.instructure.com/api/v1/courses/%s/discussion_topics/" | |
END_OF_URL = "/view?include_new_entries=1" | |
itemsdone = 0 | |
all_discussions = [] | |
response = requests.get(CANVAS_URL % (INSTITUTE, COURSE_ID), data=data, headers=headers) | |
while True: | |
raw = response.json() | |
for discussion in raw: | |
# comments | |
discussion['ENTRIES'] = requests.get(CANVAS_URL % (INSTITUTE, COURSE_ID) + str(discussion['id']) + END_OF_URL, headers=headers).json() | |
all_discussions.append(discussion) | |
print("Processed", itemsdone += 1, "discussions") | |
if (response.links["current"]["url"] == response.links["last"]["url"]): | |
break | |
response = requests.get(response.links["next"]["url"], data=data, headers=headers) | |
print("Done processing... writing to file") | |
with open("%s_discussion_scraper-course_%s.json" % (INSTITUTE, COURSE_ID), "w", encoding="utf-8") as f: | |
json.dump(all_discussions, f, ensure_ascii=False, indent=4) | |
print("Complete! %s" % f.name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment