Created
January 15, 2014 20:40
-
-
Save gbishop/8444177 to your computer and use it in GitHub Desktop.
Simple python module to download iPython Notebooks and supporting files for my students in Comp116
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Comp116 support module with tools for downloading assignments, lecture notes, and exams''' | |
import urllib | |
import os | |
import os.path as osp | |
import urlparse | |
import json | |
import time | |
ATTEMPTS = 10 | |
def fetchFile(url, filename): | |
'''Download files to the student's working directory''' | |
# make sure the destination folder (if any) exists | |
dirname = osp.dirname(filename) | |
if dirname and not osp.exists(dirname): | |
try: | |
os.makedirs(dirname) | |
except: | |
print 'making folder for %s failed', filename | |
return False | |
for i in range(ATTEMPTS): | |
try: | |
filename, headers = urllib.urlretrieve(url, filename) | |
except IOError: | |
if i < ATTEMPTS: | |
print 'Cannot connect to the server, retrying' | |
time.sleep(0.1 * i) | |
else: | |
print 'Too many attempts to contact server, failing' | |
return False | |
else: | |
break | |
return True | |
def fetchAllFiles(siteURL, listname): | |
'''Make sure the student has all the files listed''' | |
listURL = urlparse.urljoin(siteURL, listname) | |
try: | |
fp = urllib.urlopen(listURL) | |
except: | |
print 'Fetching the file list failed, are you connected to a network?' | |
return False | |
if fp.getcode() != 200: | |
print 'The url for the file list is not valid' | |
print 'code is', fp.getcode() | |
print 'url is', listURL | |
return False | |
data = json.load(fp) | |
count = 0 | |
for filename in data['files']: | |
if not osp.exists(filename): | |
fileURL = urlparse.urljoin(siteURL, filename) | |
print 'fetching', filename | |
if not fetchFile(fileURL, filename): | |
print 'fetching files failed' | |
return False | |
else: | |
count += 1 | |
if count > 0: | |
print count, 'files fetched' | |
else: | |
print 'You have all the files that have been released' | |
return True | |
def fetch(): | |
#print fetchAllFiles('http://gb.cs.unc.edu:8000/media', 'downloads.json') | |
print fetchAllFiles('http://www.cs.unc.edu/Courses/comp116-s14/media/', 'downloads.json') | |
if __name__ == '__main__': | |
fetch() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from distutils.core import setup | |
setup(name="comp116", | |
version='1.03', | |
description='Support tools for UNC Comp 116 Spring 2014', | |
author='Gary Bishop', | |
author_email='[email protected]', | |
url='https://www.cs.unc.edu/Courses/comp116-s14/', | |
py_modules=['comp116'], | |
) |
I think you might be able to install it directly from this gist too, if it helps.
pip install git+https://gist.github.com/8444177.git#comp116
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To run it, paste the following lines into an empty notebook and run them.