Skip to content

Instantly share code, notes, and snippets.

@tkan tkan/
Last active Aug 29, 2018

What would you like to do?
Get all files of humble book bundles via python
# -*- coding: utf-8 -*-
import sys
import urllib, json, time
Download your order from Humble Bundle.
* Usage:
python [your_order_key] [desired formats seperated by a space]
* Example:
python k____________XvN epub pdf
* Limitations:
- be aware of the directory you're using; usually the files will be downloaded to where you call the script from
- all existing files with the same name will be overwritten
args = []
if len(args[0]) < 3:
print "Too few arguments."
# reporthook taken from
def reporthook(count, block_size, total_size):
global start_time
if count == 0:
start_time = time.time()
duration = time.time() - start_time
progress_size = int(count * block_size)
speed = int(progress_size / (1024 * duration))
percent = min(int(count * block_size * 100 / total_size), 100)
sys.stdout.write("\r...%d%%, %d MB, %d KB/s, %d seconds passed" %
(percent, progress_size / (1024 * 1024), speed, duration))
url = "{0}".format(args[0][1])
print url
response = urllib.urlopen(url)
data = json.loads(
products_count = len(data["subproducts"])
i = 0
while i < products_count:
human_name = data["subproducts"][i]["human_name"]
download_options = len(data["subproducts"][i]["downloads"][0]["download_struct"])
print "\n Non-file item. Skipping. \n"
#j += 1
i += 1
j = 0
while j < download_options:
dl_type = (data["subproducts"][i]["downloads"][0]["download_struct"][j]["name"]).lower()
arg_iter = 2
while arg_iter < len(args[0]):
if ((args[0][arg_iter]).lower() in dl_type):
dl_url = data["subproducts"][i]["downloads"][0]["download_struct"][j]["url"]["web"]
dl_file = urllib.URLopener()
print "\n" + human_name + " ({0})".format(dl_type.upper())
dl_file.retrieve(dl_url, "{0}.{1}".format(human_name,dl_type), reporthook)
arg_iter += 1
except Exception as e:
print e
j += 1
i += 1
print "\n"

This comment has been minimized.

Copy link

commented Dec 1, 2017

I called the script with:
python2 <my-16-digits-order-key> PDF

Unfortunally I get this error message:

Traceback (most recent call last): File "", line 47, in <module> response = urllib.urlopen(url) File "c:\Python27\lib\", line 87, in urlopen return File "c:\Python27\lib\", line 213, in open return getattr(self, name)(url) File "c:\Python27\lib\", line 457, in open_https return self.http_error(url, fp, errcode, errmsg, headers) File "c:\Python27\lib\", line 377, in http_error result = method(url, fp, errcode, errmsg, headers) File "c:\Python27\lib\", line 691, in http_error_401 errcode, errmsg, headers) File "c:\Python27\lib\", line 386, in http_error_default raise IOError, ('http error', errcode, errmsg, headers) IOError: ('http error', 401, 'Unauthorized', <httplib.HTTPMessage instance at 0x0000000002BFDA48>)
Do I have to Change the URL?


This comment has been minimized.

Copy link

commented Jan 3, 2018

I'm pretty sure that's because the API requires you to log in before downloading anything.


This comment has been minimized.

Copy link
Owner Author

commented Jan 10, 2018

The URL looks fine to me; downloaded some books via this URL a few minutes ago. Can you access<my-16-digits-order-key> with your browser? (Some JSON should pop up...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.