Last active
August 18, 2016 20:54
-
-
Save follower46/b1acd3a666fcb0a4df87dff2aace1f0f to your computer and use it in GitHub Desktop.
Downloads all your owned content from Humble Bundle
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 urllib2 | |
import json | |
import os | |
import sys | |
from pprint import pprint | |
def download_files (type='MP3'): | |
""" Download the files on account of a specific type | |
Arguments: | |
type: MP3/FLAC | |
Download/Download Mobile | |
.deb/.rpm/.tar.gz/64-bit .rpm | |
CBZ/EPUB/PDF | |
""" | |
if not os.path.exists("Files/%s" % type): | |
os.makedirs("Files/%s" % type) | |
items = get_game_keys() | |
for item in items: | |
details = get_item_details(item['gamekey']) | |
print("Checking files in %s :: %s" % (details['product']['human_name'], item['gamekey'])) | |
for sub_product in details['subproducts']: | |
if 'downloads' in sub_product: | |
for download in sub_product['downloads']: | |
if download['platform'] == 'asmjs': | |
continue | |
files = download['download_struct'] | |
for struct in files: | |
if 'name' in struct and struct['name'] == type: | |
base_path = "Files/%s/%s" % (type, clean_name(details['product']['human_name'])) | |
url = struct['url']['web'] | |
if not os.path.exists(base_path): | |
os.makedirs(base_path) | |
print("Downloading %s from %s" % (type, sub_product['human_name'])) | |
print(struct['human_size']) | |
filename = "%s/%s" % (base_path, os.path.basename(url[:url.find('?')])) | |
if os.path.isfile(filename): | |
print("File already downloaded.") | |
else: | |
download_file(url, filename) | |
def clean_name (name): | |
name = name.replace('<', '') | |
name = name.replace('>', '') | |
name = name.replace(':', '') | |
name = name.replace('\\', '') | |
name = name.replace('/', '') | |
name = name.replace('|', '') | |
name = name.replace('?', '') | |
name = name.replace('*', '') | |
name = name.replace('""', '') | |
return name | |
def get_game_keys (): | |
request = urllib2.Request('https://www.humblebundle.com/api/v1/user/order?ajax=true', headers=headers) | |
return json.loads(urllib2.urlopen(request).read()) | |
def get_item_details (game_key): | |
request = urllib2.Request('https://www.humblebundle.com/api/v1/order/%s?ajax=true' % game_key, headers=headers) | |
return json.loads(urllib2.urlopen(request).read()) | |
def download_file (url, path): | |
try: | |
f = urllib2.urlopen(url) | |
print("downloading " + url) | |
# Open our local file for writing | |
with open(path, "wb") as local_file: | |
while True: | |
chunk = f.read(64 * 1024) | |
if not chunk: break | |
local_file.write(chunk) | |
#handle errors | |
except urllib2.HTTPError, e: | |
print("HTTP Error:", e.code, url) | |
except urllib2.URLError, e: | |
print("URL Error:", e.reason, url) | |
auth_token = '' | |
type = 'MP3' | |
if len(sys.argv) >= 2: | |
auth_token = sys.argv[1] | |
if len(sys.argv) >= 3: | |
type = sys.argv[2] | |
headers = { | |
'Accept': 'application/json', | |
'Accept-Charset': 'utf-8', | |
'Keep-Alive': 'true', | |
'Cookie': '_simpleauth_sess="' + auth_token + '";' | |
} | |
download_files(type) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment