Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Welcome lazy students, this program will download all the task PDFs and Resources from your units
- # Get your auth token by 'right-click > copy link location' of one of the download buttons (Download PDF/Resources)
- # From that link, copy the stuff after 'auth_token=' and paste it just here:
- $auth_token = 'AUTH TOKEN HERE'
- # From the same link, copy the unit ID here (it's the part after '/units/unit id is here'):
- $unit = 185
- # Run the script and it'll download and sort all the files in target_dir, heads up that it WILL update all files, so if the tutors update a file, make sure you re-run this
- $target_dir = './Tasks/' # Don't forget the trailing '/'
- # You're welcome, love Zac
- require 'json'
- require 'net/http'
- # Doubtfire Documentation at: https://doubtfire.ict.swin.edu.au/api/docs/
- unit_url = URI("https://doubtfire.ict.swin.edu.au/api/units/#{$unit}.json?auth_token=#{$auth_token}")
- def file_url (task_id, type)
- return URI("https://doubtfire.ict.swin.edu.au/api/units/#{$unit}/task_definitions/#{task_id}/#{type}.json?auth_token=#{$auth_token}")
- end
- # Handles downloading and saving of our files
- def download_file (url, destination)
- response = Net::HTTP.get_response(url)
- f = File.new($target_dir + destination, 'wb')
- f.write(response.body)
- f.close()
- end
- # Removes illegal characters from out filenames, will lead to some weird names, blame the tutors
- def remove_illegal_characters (string)
- illegalChars = /[\/\?<>\\\\:\*\|\"]/
- return string.gsub(illegalChars, ' ')
- end
- # Handle a missing target directory
- if (!Dir.exists?($target_dir))
- puts "Target Directory: \"#{$target_dir}\" does not exist, create it? (y/n) "
- if ('y' == gets.chomp)
- Dir.mkdir($target_dir)
- else
- exit(0)
- end
- end
- # Download student data and iterate through tasks, downloading
- student_info = JSON.parse(Net::HTTP.get_response(unit_url).body)
- for task in student_info['task_definitions']
- safe_dir_name = task['abbreviation'] + ' - ' + remove_illegal_characters(task['name'])
- if (!Dir.exists?($target_dir + safe_dir_name))
- Dir.mkdir($target_dir + safe_dir_name)
- end
- if (task['has_task_pdf'] == true)
- destination = "#{safe_dir_name}/#{remove_illegal_characters(task['name'])} Brief.pdf"
- download_file(file_url(task['id'], 'task_pdf'), destination)
- end
- if (task['has_task_resources'] == true)
- destination = "#{safe_dir_name}/#{remove_illegal_characters(task['name'])} Resources.zip"
- download_file(file_url(task['id'], 'task_resources'), destination)
- end
- end
Add Comment
Please, Sign In to add comment