This commit is contained in:
Eddy Hintze 2020-01-19 18:39:28 -05:00
parent 4fc463bc34
commit e93ff0628e
6 changed files with 61 additions and 26 deletions

6
CHANGELOG.md Normal file
View File

@ -0,0 +1,6 @@
# Change log
### 0.0.6
- Started change log
- Added more detail to readme
- Removed the use of f-strings to support more python versions

View File

@ -3,26 +3,47 @@
[![PyPI](https://img.shields.io/pypi/l/humblebundle-downloader.svg)](https://pypi.python.org/pypi/humblebundle-downloader)
Download all of you content from your Humble Bundle Library.
**Download all of your content from your Humble Bundle Library!**
The very first time this runs it may take a while to download everything, but after that it will only download the content that is missing.
The first time this runs it may take a while because it will download everything. After that it will only download the content that has been updated or is missing.
## Features
- Download new or updated content in your Library
- cli command for easy use
- Progress bar for each download _(with the `--progress` flag)_
- Easy cookie generation script
- downloads new and updated content from your Humble Bundle Library on each run
- cli command for easy use (downloading will also work on a headless system)
- optional progress bar for each item downloaded _(using the `--progress` flag)_
- optional cookie generation script
## Install
`pip install humblebundle-downloader`
## Getting started
First thing to do is generate cookies, this will open up a chrome window, just login and a cookie will be saved to a file to be used later to download the files.
`hbd gen-cookies -h`
## Instructions
Now download your library:
`hbd download -h`
### 1. Getting cookies
First thing to do is get your account cookies, they will be used later to download the files.
There are 2 ways to get your cookies: manual or scripted.
Inside your library folder a file called `.cache.json` is saved and keeps track of the files that have been downloaded, so running the download command pointing to the same directory will only download new files or update files if needed.
#### Method 1: Manual
Use this method if you know how to get cookies from your browser after you are logged in.
Once you have your cookies, save them to a text file named `hbd-cookies.txt` in this format:
`hbflash=None;_fbp=fb.1.000000.000000;__ssid=XXXXXXXX;_gat=1;_gid=GA1.2.1111111.11111111;hbreqsec=True;_ga=GA1.2.1111111.111111;_simpleauth_sess=XXXXXXXXXXXXX;csrf_cookie=XXXXXXXXX`
#### Method 2: Scripted
**WARNING: This method may not work on all systems!**
Requires: Chrome and a desktop-like environment (not headless).
Run the command below to open a chrome window. After you login, the cookies will automatically be saved to a text file and the window will close.
`hbd gen-cookies --cookie-file hbd-cookies.txt`
### 2. Downloading your library
Use the following command to download your Humble Bundle Library:
`hbd download --cookie-file hbd-cookies.txt --library-path "Downloaded Library" --progress`
This directory structure will be used:
`Downloaded Library/Bundle Name/Bundle Item.ext`
## Notes
* Inside your library folder a file named `.cache.json` is saved and keeps track of the files that have been downloaded. This way running the download command again pointing to the same directory will only download new or updated files.
* Use `--help` with all `hbd` commands to see available options

View File

@ -1 +1 @@
__version__ = '0.0.5'
__version__ = '0.0.6'

View File

@ -26,7 +26,7 @@ def cli():
parser_gencookie.add_argument(
'-c', '--cookie-file', type=str,
help="Location of the file to store the cookie",
default="hbd-cookies.txt",
required=True,
)
###
@ -40,8 +40,8 @@ def cli():
)
parser_download.add_argument(
'-c', '--cookie-file', type=str,
help="Location of the file to store the cookie",
default="hbd-cookies.txt",
help="Location of the cookies file",
required=True,
)
parser_download.add_argument(
'-l', '--library-path', type=str,

View File

@ -32,19 +32,19 @@ def download_library(cookie_path, library_path, progress_bar=False):
library_r = requests.get('https://www.humblebundle.com/home/library',
headers={'cookie': account_cookies})
logger.debug(f"Library request: {library_r}")
logger.debug("Library request: " + str(library_r))
library_page = parsel.Selector(text=library_r.text)
orders_json = json.loads(library_page.css('#user-home-json-data')
.xpath('string()').extract_first())
for order_id in orders_json['gamekeys']:
order_url = f'https://www.humblebundle.com/api/v1/order/{order_id}?all_tpkds=true' # noqa: E501
order_url = 'https://www.humblebundle.com/api/v1/order/{order_id}?all_tpkds=true'.format(order_id=order_id) # noqa: E501
order_r = requests.get(order_url,
headers={'cookie': account_cookies})
logger.debug(f"Order request: {order_r}")
logger.debug("Order request: " + str(order_r))
order = order_r.json()
bundle_title = _clean_name(order['product']['human_name'])
logger.info(f"Checking bundle: {bundle_title}")
logger.info("Checking bundle: " + str(bundle_title))
for item in order['subproducts']:
item_title = _clean_name(item['human_name'])
# Get all types of download for a product
@ -63,9 +63,11 @@ def download_library(cookie_path, library_path, progress_bar=False):
for file_type in download_type['download_struct']:
url = file_type['url']['web']
ext = url.split('?')[0].split('.')[-1]
filename = os.path.join(item_folder, f"{item_title}.{ext}")
file_title = item_title + "." + ext
filename = os.path.join(item_folder, file_title)
item_r = requests.get(url, stream=True)
logger.debug(f"Item request: {item_r}, Url: {url}")
logger.debug("Item request: {item_r}, Url: {url}"
.format(item_r=item_r, url=url))
# Not sure which value will be best to use, so use them all
file_info = {
'md5': file_type.get('md5'),
@ -76,7 +78,8 @@ def download_library(cookie_path, library_path, progress_bar=False):
}
if file_info != cache_data.get(filename, {}):
if not progress_bar:
logger.info(f"Downloading: {item_title}.{ext}")
logger.info("Downloading: {file_title}"
.format(file_title=file_title))
with open(filename, 'wb') as outfile:
total_length = item_r.headers.get('content-length')
@ -91,7 +94,12 @@ def download_library(cookie_path, library_path, progress_bar=False):
pb_width = 50
done = int(pb_width * dl / total_length)
if progress_bar:
print(f"Downloading: {item_title}.{ext}: {int(done * (100 / pb_width))}% [{'=' * done}{' ' * (pb_width-done)}]", end='\r') # noqa: E501, E701
print("Downloading: {file_title}: {percent}% [{filler}{space}]" # noqa E501
.format(file_title=file_title,
percent=int(done * (100 / pb_width)), # noqa E501
filler='=' * done,
space=' ' * (pb_width - done), # noqa E501
), end='\r')
if progress_bar:
# print new line so next progress bar

View File

@ -11,7 +11,7 @@ def _get_cookie_str(driver):
raw_cookies = driver.get_cookies()
baked_cookies = ''
for cookie in raw_cookies:
baked_cookies += f"{cookie['name']}={cookie['value']};"
baked_cookies += cookie['name'] + "=" + {cookie['value'] + ";"
# Remove the trailing ;
return baked_cookies[:-1]
@ -34,6 +34,6 @@ def generate_cookie(cookie_path):
with open(cookie_path, 'w') as f:
f.write(cookie_str)
logger.info(f"Saved cookie to {cookie_path}")
logger.info("Saved cookies to " + cookie_path)
driver.quit()