Zac_McDonald

Doubtfire Downloader for lazy CS Students - Python

Mar 5th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 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'), default is Intro To Programming:
  6. unit = 168
  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. # Make sure you get 'requests' before trying to get this to work: Open cmd (or whatever) and type 'pip install requests' (might need Admin rights depending on where Python install is)
  12. import requests
  13. import json
  14. import os
  15. import re
  16.  
  17. unitUrl = 'https://doubtfire.ict.swin.edu.au/api/units/{unit}.json?auth_token={auth}'.format(unit = unit, auth = auth_token)
  18. fileUrl = 'https://doubtfire.ict.swin.edu.au/api/units/{unit}/task_definitions/{task}/{type}.json?auth_token={auth}'.format(unit = unit, task = '{task}', type = '{type}', auth = auth_token)
  19.  
  20. # Handles downloading and saving of our files
  21. def downloadFile (url, destination):
  22.     r = requests.get(url, allow_redirects=True)
  23.     open(target_dir + destination, 'wb').write(r.content)
  24.  
  25. # Removes illegal characters from out filenames, will lead to some weird names, blame the tutors
  26. def removeIllegalChars (string):
  27.     illegalChars = '[/\?<>\\\\:\*\|\"]'
  28.     return re.sub(illegalChars, ' ', string)
  29.  
  30. # Handle a missing target directory
  31. if not os.path.exists(target_dir):
  32.     if ('y' == input('Target Directory: \"{}\" does not exist, create it? (y/n)  '.format(target_dir))):
  33.         os.mkdir(target_dir)
  34.     else:
  35.         exit(0)
  36.  
  37. # Download student data and iterate through tasks, downloading
  38. studentInfo = json.loads(requests.get(unitUrl.format(unit = unit, auth = auth_token), allow_redirects=True).content)
  39. for task in studentInfo['task_definitions']:
  40.     safeDirName = task['abbreviation'] + ' - ' + removeIllegalChars(task['name'])
  41.     try:
  42.         os.mkdir(target_dir + safeDirName)
  43.     except FileExistsError:
  44.         pass
  45.  
  46.     if (task['has_task_pdf'] == True):
  47.         downloadFile(fileUrl.format(task = task['id'], type = 'task_pdf'), '{dir}/{name} Brief.pdf'.format(dir = safeDirName, name = removeIllegalChars(task['name'])))
  48.  
  49.     if (task['has_task_resources'] == True):
  50.         downloadFile(fileUrl.format(task = task['id'], type = 'task_resources'), '{dir}/{name} Resources.zip'.format(dir = safeDirName, name = removeIllegalChars(task['name'])))
Add Comment
Please, Sign In to add comment