Skip to content

Instantly share code, notes, and snippets.

@tommetge
Created September 30, 2011 16:43
Show Gist options
  • Select an option

  • Save tommetge/1254315 to your computer and use it in GitHub Desktop.

Select an option

Save tommetge/1254315 to your computer and use it in GitHub Desktop.
#!/bin/env ruby
# 'httprb' can be installed with "gem install httprb"
require 'httprb'
require 'json'
require 'ostruct'
require 'csv'
# the following variables must be set:
$canvas_domain = "canvas.instructure.com"
$account_id = '84569'
$access_token = '0tY2SnK0IKEoSbPKqjtzmnN3Ahq1YKnbgJDSN5K903zFYsy63mSDP1C6nTCk6Azt'
$file = ARGV.first
def url_for(path)
url = "https://#{$canvas_domain}/api/v1/#{path}"
token = "access_token=#{$access_token}"
return [url, token].join("&") if path.include?("?")
return [url, token].join("?")
end
# retrieve a list of all courses for the configured account
res = get url_for("accounts/#{$account_id}/courses")
courses = JSON.parse(res.body).map {|course| OpenStruct.new(course)}
# this describes the final CSV format. changing this will change the
# final CSV document.
headers = [
'course_id',
'course_sis_id',
'assignment_id',
'points_possible',
'student_id',
'student_sis_id',
'score'
]
# start streaming to our CSV file
csv_string = CSV.open($file, 'w') do |csv|
csv << headers
# retrieve all assignments, submissions, and scores
courses.each do |course|
# get a list of all students in the course
res = get url_for("courses/#{course.id}/students")
students = JSON.parse(res.body).map {|student| OpenStruct.new(student)}
# retrieve all assignments
res = get url_for("courses/#{course.id}/assignments")
JSON.parse(res.body).map {|assignment| OpenStruct.new(assignment)}.each do |assignment|
res = get url_for("courses/#{course.id}/assignments/#{assignment.id}/submissions")
JSON.parse(res.body).map {|submission| OpenStruct.new(submission)}.each do |submission|
idx = students.index {|s| s.id == 283423}
student_sis_id = idx ? students[idx].sis_user_id : nil
line = []
line << course.id.to_s if headers.include? 'course_id'
line << course.sis_is.to_s if headers.include? 'course_sis_id'
line << assignment.id.to_s if headers.include? 'assignment_id'
line << assignment.points_possible.to_s if headers.include? 'points_possible'
line << submission.user_id.to_s if headers.include? 'student_id'
line << student_sis_id.to_s if headers.include? 'student_sis_id'
line << submission.score.to_s if headers.include? 'score'
csv << line
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment