Added ability to download all submissiosn from students and load credentials from file #1
52
export.py
52
export.py
|
@ -15,8 +15,8 @@ API_KEY = ""
|
||||||
USER_ID = 0000000
|
USER_ID = 0000000
|
||||||
# Directory in which to download course information to (will be created if not present)
|
# Directory in which to download course information to (will be created if not present)
|
||||||
DL_LOCATION = "./output"
|
DL_LOCATION = "./output"
|
||||||
# List of Course IDs that should be skipped
|
# List of Course IDs that should be skipped (need to be integers)
|
||||||
COURSES_TO_SKIP = []
|
COURSES_TO_SKIP = [288290, 512033]
|
||||||
|
|
||||||
class moduleItemView():
|
class moduleItemView():
|
||||||
title = ""
|
title = ""
|
||||||
|
@ -160,7 +160,7 @@ def findCourseModules(course, course_view):
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
module_views.append(module_view)
|
module_views.append(module_view)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Skipping entire module that gave the following error:")
|
print("Skipping entire module that gave the following error:")
|
||||||
print(e)
|
print(e)
|
||||||
|
@ -182,6 +182,7 @@ def downloadCourseFiles(course, course_view):
|
||||||
|
|
||||||
# Download file if it doesn't already exist
|
# Download file if it doesn't already exist
|
||||||
if not os.path.exists(dl_path):
|
if not os.path.exists(dl_path):
|
||||||
|
print('Downloading: {}'.format(dl_path))
|
||||||
file.download(dl_path)
|
file.download(dl_path)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Skipping file download that gave the following error:")
|
print("Skipping file download that gave the following error:")
|
||||||
|
@ -201,7 +202,7 @@ def getCoursePageUrls(course):
|
||||||
if e.message != "Not Found":
|
if e.message != "Not Found":
|
||||||
print("Skipping page that gave the following error:")
|
print("Skipping page that gave the following error:")
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
return page_urls
|
return page_urls
|
||||||
|
|
||||||
def findCoursePages(course):
|
def findCoursePages(course):
|
||||||
|
@ -229,7 +230,7 @@ def findCoursePages(course):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Skipping page download that gave the following error:")
|
print("Skipping page download that gave the following error:")
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
return page_views
|
return page_views
|
||||||
|
|
||||||
def findCourseAssignments(course):
|
def findCourseAssignments(course):
|
||||||
|
@ -240,6 +241,7 @@ def findCourseAssignments(course):
|
||||||
assignments = course.get_assignments()
|
assignments = course.get_assignments()
|
||||||
|
|
||||||
for assignment in assignments:
|
for assignment in assignments:
|
||||||
|
print(assignment)
|
||||||
# Create a new assignment view
|
# Create a new assignment view
|
||||||
assignment_view = assignmentView()
|
assignment_view = assignmentView()
|
||||||
|
|
||||||
|
@ -252,6 +254,23 @@ def findCourseAssignments(course):
|
||||||
# Due date
|
# Due date
|
||||||
assignment_view.due_date = assignment.due_at_date.strftime("%B %d, %Y %I:%M %p") if hasattr(assignment, "due_at_date") else ""
|
assignment_view.due_date = assignment.due_at_date.strftime("%B %d, %Y %I:%M %p") if hasattr(assignment, "due_at_date") else ""
|
||||||
|
|
||||||
|
# Download all submissions
|
||||||
|
try:
|
||||||
|
submissions = assignment.get_submissions()
|
||||||
|
except:
|
||||||
|
print("Got no submissions for this assignment")
|
||||||
|
else:
|
||||||
|
print(submissions)
|
||||||
|
for submission in submissions:
|
||||||
|
print(submission)
|
||||||
|
try:
|
||||||
|
submission.attachments
|
||||||
|
except AttributeError:
|
||||||
|
print('No attachements')
|
||||||
|
else:
|
||||||
|
for attachment in submission.attachments:
|
||||||
|
print(attachment["url"])
|
||||||
|
|
||||||
# Get my user"s submission object
|
# Get my user"s submission object
|
||||||
submission = assignment.get_submission(USER_ID)
|
submission = assignment.get_submission(USER_ID)
|
||||||
|
|
||||||
|
@ -268,6 +287,9 @@ def findCourseAssignments(course):
|
||||||
assignment_view.submission.submission_comments = str(submission.submission_comments) if hasattr(submission, "submission_comments") else ""
|
assignment_view.submission.submission_comments = str(submission.submission_comments) if hasattr(submission, "submission_comments") else ""
|
||||||
|
|
||||||
assignment_views.append(assignment_view)
|
assignment_views.append(assignment_view)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Skipping assignment that gave the following error:")
|
print("Skipping assignment that gave the following error:")
|
||||||
print(e)
|
print(e)
|
||||||
|
@ -287,7 +309,7 @@ def findCourseAnnouncements(course):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Skipping announcement that gave the following error:")
|
print("Skipping announcement that gave the following error:")
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
return announcement_views
|
return announcement_views
|
||||||
|
|
||||||
def getDiscussionView(discussion_topic):
|
def getDiscussionView(discussion_topic):
|
||||||
|
@ -312,7 +334,7 @@ def getDiscussionView(discussion_topic):
|
||||||
for topic_entry in discussion_topic_entries:
|
for topic_entry in discussion_topic_entries:
|
||||||
# Create new discussion view for the topic_entry
|
# Create new discussion view for the topic_entry
|
||||||
topic_entry_view = topicEntryView()
|
topic_entry_view = topicEntryView()
|
||||||
|
|
||||||
# Author
|
# Author
|
||||||
topic_entry_view.author = str(topic_entry.user_name) if hasattr(topic_entry, "user_name") else ""
|
topic_entry_view.author = str(topic_entry.user_name) if hasattr(topic_entry, "user_name") else ""
|
||||||
# Posted date
|
# Posted date
|
||||||
|
@ -416,20 +438,20 @@ def main():
|
||||||
# Canvas API URL
|
# Canvas API URL
|
||||||
print("We will need your organization's Canvas Base URL. This is probably something like https://{schoolName}.instructure.com)")
|
print("We will need your organization's Canvas Base URL. This is probably something like https://{schoolName}.instructure.com)")
|
||||||
global API_URL
|
global API_URL
|
||||||
API_URL = input("Enter your organization's Canvas Base URL: ")
|
#API_URL = input("Enter your organization's Canvas Base URL: ")
|
||||||
|
|
||||||
# Canvas API key
|
# Canvas API key
|
||||||
print("\nWe will need a valid API key for your user. You can generate one in Canvas once you are logged in.")
|
print("\nWe will need a valid API key for your user. You can generate one in Canvas once you are logged in.")
|
||||||
global API_KEY
|
global API_KEY
|
||||||
API_KEY = input("Enter a valid API key for your user: ")
|
#API_KEY = input("Enter a valid API key for your user: ")
|
||||||
|
|
||||||
# My Canvas User ID
|
# My Canvas User ID
|
||||||
print("\nWe will need your Canvas User ID. You can find this by logging in to canvas and then going to this URL in the same browser {yourCanvasBaseUrl}/api/v1/users/self")
|
print("\nWe will need your Canvas User ID. You can find this by logging in to canvas and then going to this URL in the same browser {yourCanvasBaseUrl}/api/v1/users/self")
|
||||||
global USER_ID
|
global USER_ID
|
||||||
USER_ID = input("Enter your Canvas User ID: ")
|
#USER_ID = input("Enter your Canvas User ID: ")
|
||||||
|
|
||||||
print("\nConnecting to canvas\n")
|
print("\nConnecting to canvas\n")
|
||||||
|
|
||||||
# Initialize a new Canvas object
|
# Initialize a new Canvas object
|
||||||
canvas = Canvas(API_URL, API_KEY)
|
canvas = Canvas(API_URL, API_KEY)
|
||||||
|
|
||||||
|
@ -437,7 +459,7 @@ def main():
|
||||||
# Create directory if not present
|
# Create directory if not present
|
||||||
if not os.path.exists(DL_LOCATION):
|
if not os.path.exists(DL_LOCATION):
|
||||||
os.makedirs(DL_LOCATION)
|
os.makedirs(DL_LOCATION)
|
||||||
|
|
||||||
all_courses_views = []
|
all_courses_views = []
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -466,7 +488,7 @@ def main():
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Skipping entire course that gave the following error:")
|
print("Skipping entire course that gave the following error:")
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
print("Exporting data from all courses combined as one file: all_output.json")
|
print("Exporting data from all courses combined as one file: all_output.json")
|
||||||
# Awful hack to make the JSON pretty. Decode it with Python stdlib json module then re-encode with indentation
|
# Awful hack to make the JSON pretty. Decode it with Python stdlib json module then re-encode with indentation
|
||||||
json_str = json.dumps(json.loads(jsonpickle.encode(all_courses_views, unpicklable = False)), indent = 4)
|
json_str = json.dumps(json.loads(jsonpickle.encode(all_courses_views, unpicklable = False)), indent = 4)
|
||||||
|
@ -484,4 +506,4 @@ if __name__ == "__main__":
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Exiting due to uncaught exception:")
|
print("Exiting due to uncaught exception:")
|
||||||
print(e)
|
print(e)
|
||||||
print(traceback.format_exc())
|
print(traceback.format_exc())
|
||||||
|
|
Loading…
Reference in New Issue