O'Reilly Offering Programming eBooks for Free (Direct Links Included)

Wrote a quick and dirty python script that downloads these books by category. Requires python 2 and requests library. Categories I've see so far are business, data, iot, security, web-platform, webops-perf, programming, so usage would look like:

python oreilly_downloader.py business data iot security web-platform webops-perf programming

https://gist.github.com/fabianvf/3feacbd83491d8cc1895a15a73af7e18

import os
import re
import sys
import requests

def main():
    categories = sys.argv[1:]
    urls = map(lambda x: 'http://www.oreilly.com/{}/free/'.format(x), categories)
    for (category, url), filenames in zip(zip(categories, urls), map(retrieve_filenames, urls)):
        print(category)
        if not os.path.exists(category):
            os.makedirs(category)
        for title, files in filenames.items():
            path = os.path.join(category, title)
            if not os.path.exists(path):
                os.makedirs(path)
            print '\t{}'.format(title)
            for file in files:
                print('\t\t{}'.format(file))
                download_file(os.path.join(category, title, file),
                                    '{}files/{}'.format(url, file))


def download_file(path, url):
    response = requests.get(url, stream=True)
    with open(path, 'wb') as f:
        for chunk in response.iter_content(chunk_size=1024):
            if chunk:
                f.write(chunk)


def retrieve_filenames(url):
    matcher = re.compile(r'{}(.*).csp'.format(url))
    response = requests.get(url).text
    names = matcher.findall(response)
    return {
        name: map(lambda x: x.format(name), ['{}.pdf', '{}.mobi', '{}.epub'])
        for name in names
    }


if __name__ == '__main__':
    main()
/r/learnprogramming Thread