Skip to content

Instantly share code, notes, and snippets.

@killertilapia
Last active July 14, 2016 07:38
Show Gist options
  • Save killertilapia/d2bd99693d0ec38fd337383ababe77b7 to your computer and use it in GitHub Desktop.
Save killertilapia/d2bd99693d0ec38fd337383ababe77b7 to your computer and use it in GitHub Desktop.
Downloads MSFTFreeEbooks as per Eric Ligman's blog
# Downloads MSFTFreeEbooks as per Eric Ligman's blog
# https://blogs.msdn.microsoft.com/mssmallbiz/2016/07/10/how-to-download-all-of-the-free-ebooks-and-resources-in-my-free-ebooks-giveaway/
# You'll probably want to do a pip install requests first before you can run the script
import os
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