Created
July 14, 2016 07:05
-
-
Save killertilapia/cd8e86c3033d07f3431c9777e5dfc088 to your computer and use it in GitHub Desktop.
Downloads MSFTFreeEbooks as Eric Ligman
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
import sys | |
import urllib | |
import requests | |
def unshorten_url2(url): | |
r = requests.head(url, allow_redirects=True) | |
return r.url | |
def download(url): | |
full_url = unshorten_url2(url) | |
print 'Getting %s' % full_url | |
webfile = urllib.urlopen(full_url) | |
localfile = open(full_url.split('/')[-1],'w') | |
localfile.write(webfile.read()) | |
webfile.close() | |
localfile.close() | |
def main_loop(file_to_read): | |
with open(file_to_read) as f: | |
next(f) # skip first line | |
for line in f: | |
download(line) | |
if __name__ == '__main__': | |
if len(sys.argv) == 2: | |
try: | |
main_loop(sys.argv[1]) | |
except IOError: | |
print 'Filename not found' | |
else: | |
print 'Usage: %s [path_to_file]' % os.path.basename(sys.argv[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment