Created
May 24, 2013 16:46
-
-
Save sanketsudake/5644833 to your computer and use it in GitHub Desktop.
Python script to download Lecture notes for machine learning
from http://cs229.stanford.edu/materials.html
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
""" | |
Description: | |
Get Lecture notes for machine learning | |
from http://cs229.stanford.edu/materials.html | |
Usage: | |
python ml_notes.py | |
""" | |
import urllib2 | |
import os | |
def getfile(url): | |
file_name = url.split('/')[-1] | |
u = urllib2.urlopen(url) | |
f = open(file_name, 'wb') | |
meta = u.info() | |
file_size = int(meta.getheaders("Content-Length")[0]) | |
print "Downloading: %s Bytes: %s" % (file_name, file_size) | |
file_size_dl = 0 | |
block_sz = 8192 | |
while True: | |
buffer = u.read(block_sz) | |
if not buffer: | |
break | |
file_size_dl += len(buffer) | |
f.write(buffer) | |
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size) | |
status = status + chr(8)*(len(status)+1) | |
print status, | |
f.close() | |
print "\n" | |
if __name__ == '__main__': | |
initurl = 'http://cs229.stanford.edu/notes/cs229-notes1.pdf' | |
temp = initurl.split("1") | |
urls = [] | |
for i in range(1, 13): | |
if i == 7: | |
urls.append(temp[0] + "7a" + temp[1]) | |
urls.append(temp[0] + "7b" + temp[1]) | |
continue | |
urls.append(temp[0] + str(i) + temp[1]) | |
if not os.path.isdir("lecture_notes"): | |
print "Creating directory lecture_notes." | |
os.mkdir("lecture_notes") | |
else: | |
print "lecture_notes directory already exists." | |
print "Going to directory lecture_notes." | |
os.chdir("lecture_notes") | |
for url in urls: | |
fname = url.split("/")[-1] | |
if not os.path.isfile(fname): | |
print "Get file => " + fname | |
getfile(url) | |
print "\nDownload finished." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment