Zac_McDonald

Doubtfire Downloader for lazy CS Students - Ruby

Aug 7th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.48 KB | None | 0 0
  1. # Welcome lazy students, this program will download all the task PDFs and Resources from your units
  2. # Get your auth token by 'right-click > copy link location' of one of the download buttons (Download PDF/Resources)
  3. # From that link, copy the stuff after 'auth_token=' and paste it just here:
  4. $auth_token = 'AUTH TOKEN HERE'
  5. # From the same link, copy the unit ID here (it's the part after '/units/unit id is here'):
  6. $unit = 185
  7. # 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
  8. $target_dir = './Tasks/' # Don't forget the trailing '/'
  9. # You're welcome, love Zac
  10.  
  11. require 'json'
  12. require 'net/http'
  13.  
  14. # Doubtfire Documentation at: https://doubtfire.ict.swin.edu.au/api/docs/
  15. unit_url = URI("https://doubtfire.ict.swin.edu.au/api/units/#{$unit}.json?auth_token=#{$auth_token}")
  16. def file_url (task_id, type)
  17.     return URI("https://doubtfire.ict.swin.edu.au/api/units/#{$unit}/task_definitions/#{task_id}/#{type}.json?auth_token=#{$auth_token}")
  18. end
  19.  
  20. # Handles downloading and saving of our files
  21. def download_file (url, destination)
  22.     response = Net::HTTP.get_response(url)
  23.     f = File.new($target_dir + destination, 'wb')
  24.     f.write(response.body)
  25.     f.close()
  26. end
  27.  
  28. # Removes illegal characters from out filenames, will lead to some weird names, blame the tutors
  29. def remove_illegal_characters (string)
  30.     illegalChars = /[\/\?<>\\\\:\*\|\"]/
  31.     return string.gsub(illegalChars, ' ')
  32. end
  33.  
  34. # Handle a missing target directory
  35. if (!Dir.exists?($target_dir))
  36.     puts "Target Directory: \"#{$target_dir}\" does not exist, create it? (y/n)   "
  37.     if ('y' == gets.chomp)
  38.         Dir.mkdir($target_dir)
  39.     else
  40.         exit(0)
  41.     end
  42. end
  43.  
  44. # Download student data and iterate through tasks, downloading
  45. student_info = JSON.parse(Net::HTTP.get_response(unit_url).body)
  46. for task in student_info['task_definitions']
  47.     safe_dir_name = task['abbreviation'] + ' - ' + remove_illegal_characters(task['name'])
  48.    
  49.     if (!Dir.exists?($target_dir + safe_dir_name))
  50.         Dir.mkdir($target_dir + safe_dir_name)
  51.     end
  52.  
  53.     if (task['has_task_pdf'] == true)
  54.         destination = "#{safe_dir_name}/#{remove_illegal_characters(task['name'])} Brief.pdf"
  55.         download_file(file_url(task['id'], 'task_pdf'), destination)
  56.     end
  57.  
  58.     if (task['has_task_resources'] == true)
  59.         destination = "#{safe_dir_name}/#{remove_illegal_characters(task['name'])} Resources.zip"
  60.         download_file(file_url(task['id'], 'task_resources'), destination)
  61.     end
  62. end
Add Comment
Please, Sign In to add comment