Created
September 12, 2015 10:23
-
-
Save anonymous/a1a3602a10ab8658430b to your computer and use it in GitHub Desktop.
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
from __future__ import print_function | |
import json | |
import re | |
import requests | |
from os import makedirs, path, remove | |
from pyquery import PyQuery as pq | |
from urlparse import urlparse | |
def download_file(url, filename): | |
if path.exists(filename): | |
return | |
print("[ ] %s" % filename, end="\r") | |
r = requests.get(url, stream=True) | |
if r.status_code == 200: | |
expected_size = 0 | |
if "content-length" in r.headers: | |
expected_size = float(r.headers["content-length"]) | |
loaded_size = 0.0 | |
with open(filename, "wb") as f: | |
for chunk in r.iter_content(1024 * 4): | |
if expected_size > 0: | |
percent = 100.0 * (loaded_size / expected_size) | |
print("[%2d%%]" % int(percent), end="\r") | |
f.write(chunk) | |
loaded_size += len(chunk) | |
print("[ v ]") | |
def parse_download(download, product): | |
for struct in download["download_struct"]: | |
if "url" in struct: | |
identifier = product["human_name"].lower() | |
identifier = re.sub(r"[^a-z 0-9]+", "", identifier) | |
identifier = re.sub(r"[ ]+", " ", identifier) | |
web_url = struct["url"]["web"] | |
parsed_url = urlparse(web_url) | |
target_path = path.join("humble", download["platform"], identifier) | |
target_filename = path.basename(parsed_url.path) | |
if not path.exists(target_path): | |
makedirs(target_path) | |
final_filename = path.join(target_path, target_filename) | |
try: | |
download_file(web_url, final_filename) | |
except KeyboardInterrupt as interrupt: | |
remove(final_filename) | |
print("") | |
exit() | |
def parse_product(product): | |
if "downloads" in product: | |
for download in product["downloads"]: | |
parse_download(download, product) | |
pre_login_url = "https://www.humblebundle.com/" | |
login_url = "https://www.humblebundle.com/processlogin" | |
library_url = "https://www.humblebundle.com/home/library" | |
api_url = "https://www.humblebundle.com/api/v1/order/%s" | |
session = requests.Session() | |
response = session.get(pre_login_url) | |
document = pq(response.text) | |
login_document = pq(document("#account-login").html()) | |
login_data = { | |
"_le_csrf_token": pq(login_document(".csrftoken")).val(), | |
"goto": "#", | |
"qs": "", | |
"script-wrapper": "login_callback", | |
"username": None, | |
"password": None, | |
"authy-token": "", | |
"submit-data": "", | |
} | |
with open("login.json", "r") as login_file: | |
login = json.load(login_file) | |
for key, value in login.items(): | |
login_data[key] = value | |
session.post(login_url, login_data) | |
response = session.get(library_url) | |
gamekey_finder = re.compile(ur"var\s+gamekeys\s*=\s*(.+);") | |
game_keys = json.loads(re.search(gamekey_finder, response.text).group(1)) | |
for game_key in game_keys: | |
response = session.get(api_url % game_key) | |
data = response.json() | |
if not path.exists("humble/data"): | |
makedirs("humble/data") | |
with open("humble/data/%s.json" % game_key, "w") as data_file: | |
json.dump(data, data_file) | |
parse_product(data["product"]) | |
for product in data["subproducts"]: | |
parse_product(product) |
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
{ | |
"username": "[email protected]", | |
"password": "dontstealmygamespls" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment