Fixed #6. Support filtering by platform

This commit is contained in:
Eddy Hintze 2020-01-19 23:11:35 -05:00
parent 7168258191
commit 043f53b6dc
3 changed files with 36 additions and 9 deletions

View File

@ -10,8 +10,11 @@ The first time this runs it may take a while because it will download everything
## Features
- 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)_
- works for SSO and 2FA accounts
- optional progress bar for each item downloaded _(`--progress` flag)_
- optional cookie generation script
- optional filter by file types using an include _or_ exclude list _(`--include/--exclude` flag)_
- optional filter by platform types like video, ebook, etc... _(`--platform` flag)_
## Install
@ -47,3 +50,4 @@ This directory structure will be used:
## 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
* Find supported platforms for the `--platform` flag by visiting your Humble Bundle Library and look under the **Platform** dropdown

View File

@ -48,6 +48,12 @@ def cli():
help="Folder to download all content to",
required=True,
)
parser_download.add_argument(
'-p', '--platform',
type=str, nargs='*',
help=("Only get content in a platform. Values can be seen in your "
"humble bundle's library dropdown. Ex: -p ebook video"),
)
parser_download.add_argument(
'--progress',
action='store_true',
@ -57,12 +63,13 @@ def cli():
filter_ext.add_argument(
'-e', '--exclude',
type=str, nargs='*',
help="File extensions to ignore when downloading files. Ex: -e pdf mobi"
help=("File extensions to ignore when downloading files. "
"Ex: -e pdf mobi"),
)
filter_ext.add_argument(
'-i', '--include',
type=str, nargs='*',
help="Only download files with these extensions. Ex: -i pdf mobi"
help="Only download files with these extensions. Ex: -i pdf mobi",
)
cli_args = parser.parse_args()
@ -78,5 +85,6 @@ def cli():
cli_args.library_path,
progress_bar=cli_args.progress,
ext_include=cli_args.include,
ext_exclude=cli_args.exclude
ext_exclude=cli_args.exclude,
platform_include=cli_args.platform,
)

View File

@ -18,11 +18,19 @@ def _clean_name(dirty_str):
def download_library(cookie_path, library_path, progress_bar=False,
ext_include=None, ext_exclude=None):
ext_include=None, ext_exclude=None, platform_include=None):
if ext_include is None:
ext_include = []
ext_include = list(map(str.lower, ext_include))
if ext_exclude is None:
ext_exclude = []
ext_exclude = list(map(str.lower, ext_exclude))
if platform_include is None or 'all' in platform_include:
# if all then we do not even need to use this feature
platform_include = []
platform_include = list(map(str.lower, platform_include))
# Load cookies
with open(cookie_path, 'r') as f:
@ -56,7 +64,13 @@ def download_library(cookie_path, library_path, progress_bar=False,
# Get all types of download for a product
for download_type in item['downloads']:
# Type of product, ebook, videos, etc...
# platform = download_type['platform']
platform = download_type['platform'].lower()
if (platform_include and platform not in platform_include):
logger.info("Do not want " + platform
+ ", Skipping " + item_title)
continue
item_folder = os.path.join(
library_path, bundle_title, item_title
)
@ -70,14 +84,15 @@ def download_library(cookie_path, library_path, progress_bar=False,
try:
url = file_type['url']['web']
except KeyError:
logger.info("No url found for " + item_title)
logger.info("No url found for " + bundle_title
+ "/" + item_title)
continue
ext = url.split('?')[0].split('.')[-1]
file_title = item_title + "." + ext
# Only get the file types we care about
if ((ext_include and ext not in ext_include)
or (ext_exclude and ext in ext_exclude)):
if ((ext_include and ext.lower() not in ext_include)
or (ext_exclude and ext.lower() in ext_exclude)):
logger.info("Skipping the file " + file_title)
continue