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'), default is Intro To Programming:
- unit = 168
- # 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
- # 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)
- import requests
- import json
- import os
- import re
- unitUrl = 'https://doubtfire.ict.swin.edu.au/api/units/{unit}.json?auth_token={auth}'.format(unit = unit, auth = auth_token)
- 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)
- # Handles downloading and saving of our files
- def downloadFile (url, destination):
- r = requests.get(url, allow_redirects=True)
- open(target_dir + destination, 'wb').write(r.content)
- # Removes illegal characters from out filenames, will lead to some weird names, blame the tutors
- def removeIllegalChars (string):
- illegalChars = '[/\?<>\\\\:\*\|\"]'
- return re.sub(illegalChars, ' ', string)
- # Handle a missing target directory
- if not os.path.exists(target_dir):
- if ('y' == input('Target Directory: \"{}\" does not exist, create it? (y/n) '.format(target_dir))):
- os.mkdir(target_dir)
- else:
- exit(0)
- # Download student data and iterate through tasks, downloading
- studentInfo = json.loads(requests.get(unitUrl.format(unit = unit, auth = auth_token), allow_redirects=True).content)
- for task in studentInfo['task_definitions']:
- safeDirName = task['abbreviation'] + ' - ' + removeIllegalChars(task['name'])
- try:
- os.mkdir(target_dir + safeDirName)
- except FileExistsError:
- pass
- if (task['has_task_pdf'] == True):
- downloadFile(fileUrl.format(task = task['id'], type = 'task_pdf'), '{dir}/{name} Brief.pdf'.format(dir = safeDirName, name = removeIllegalChars(task['name'])))
- if (task['has_task_resources'] == True):
- 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