Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Download Moodle activity data.
#!/usr/bin/env ruby
# usage: ruby download_activity.rb https://www.example.com/ app user passWd\! 12 result.csv
# caution : if your password includes bash's special character ,put a '\' before that char (ex: '!' is '\!').
require "tempfile"
require "uri"
moodle_site = ARGV[0]
app_root = ARGV[1]
username = ARGV[2]
password = ARGV[3]
course_id = ARGV[4]
output_filepath = ARGV[5]
Tempfile.create("cookie") do |cookie|
login_cmd =<<-EOC
curl '#{moodle_site}#{app_root}/login/index.php' \
-L \
-b #{cookie.path} -c #{cookie.path} \
-H 'Connection: keep-alive' \
-H 'Cache-Control: max-age=0' \
-H 'Upgrade-Insecure-Requests: 1' \
-H 'Origin: #{moodle_site}' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36' \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' \
-H 'Sec-Fetch-Site: same-origin' \
-H 'Sec-Fetch-Mode: navigate' \
-H 'Sec-Fetch-User: ?1' \
-H 'Sec-Fetch-Dest: document' \
-H 'Referer: #{moodle_site}#{app_root}/login/index.php' \
-H 'Accept-Language: ja,en-US;q=0.9,en;q=0.8' \
--data-raw 'username=#{username}&password=#{URI.encode_www_form_component(password, enc=nil)}&anchor=' \
--compressed \
> /dev/null
EOC
puts login_cmd
system(login_cmd)
cmd_line =<<-EOC
curl '#{moodle_site}#{app_root}/report/progress/index.php?course=#{course_id}&format=excelcsv' \
-b #{cookie.path} -c #{cookie.path} \
-H 'Connection: keep-alive' \
-H 'Upgrade-Insecure-Requests: 1' \
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36' \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' \
-H 'Sec-Fetch-Site: same-origin' \
-H 'Sec-Fetch-Mode: navigate' \
-H 'Sec-Fetch-User: ?1' \
-H 'Sec-Fetch-Dest: document' \
-H 'Referer: #{moodle_site}#{app_root}/report/progress/index.php?course=#{course_id}' \
-H 'Accept-Language: ja,en-US;q=0.9,en;q=0.8' \
--compressed \
--output #{output_filepath}
EOC
system(cmd_line)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment