This repository was archived by the owner on Mar 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathcli.py
More file actions
161 lines (149 loc) · 5.12 KB
/
Copy pathcli.py
File metadata and controls
161 lines (149 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import os
import sys
import logging
import argparse
from humblebundle_downloader._version import __version__
logger = logging.getLogger(__name__)
LOG_LEVEL = os.environ.get('HBD_LOGLEVEL', 'INFO').upper()
logging.basicConfig(
level=LOG_LEVEL,
format='%(message)s',
)
# Ignore unwanted logs from the requests lib when debuging
logging.getLogger('urllib3.connectionpool').setLevel(logging.WARNING)
# convert a string representing size to an integer
def parse_size(size):
# Base10 unit definitions
# K=x1000 M=x10000000 G=x1000000000
# units = {"K": 10**3, "M": 10**6, "G": 10**9, "T": 10**12}
# Binary unit definitions:
# K=x1024 M=x1024x1024 etc
units = {"K": 2**10, "M": 2**20, "G": 2**30, "T": 2**40}
try:
return int(size) # if it's an int already just return it
except ValueError: # it wasn't an int
size=size.upper()
if size.endswith('B'):
return parse_size(size[:-1])
unit=size[-1:]
number=size[:-1]
if unit not in units.keys():
raise ValueError(f'Invalid Unit: {unit}')
return int(float(number)*units[unit])
# convert a string represting time to an integer number of seconds
def parse_seconds(size):
# convert parameter to number of seconds
units = {"S": 1, "M": 60, "H": 60*60, "D": 60*60*24, 'W': 60*60*24*7}
try:
return int(size) # if it's an int already just return it
except ValueError: # it wasn't an int
size=size.upper()
unit=size[-1:]
number=size[:-1]
if unit not in units.keys():
raise ValueError(f'Invalid Unit: {unit}')
return int(float(number)*units[unit])
def parse_args(args):
if ((len(args)>0) and (args[0].lower() == 'download')):
args = args[1:]
raise DeprecationWarning("`download` argument is no longer used")
parser = argparse.ArgumentParser(description='Download purchases from Humble Bundle',
epilog=f'Version: {__version__}')
cookie = parser.add_mutually_exclusive_group(required=True)
cookie.add_argument(
'-c', '--cookie-file', type=str,
help="Location of the cookies file",
)
cookie.add_argument(
'-s', '--session-auth', type=str,
help="Value of the cookie _simpleauth_sess. WRAP IN QUOTES",
)
parser.add_argument(
'-l', '--library-path', type=str,
help="Folder to download all content to",
required=True,
)
parser.add_argument(
'-t', '--trove', action='store_true',
help="Only check and download Humble Trove content",
)
parser.add_argument(
'-u', '--update', action='store_true',
help=("Check to see if products have been updated "
"(still get new products)"),
)
parser.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.add_argument(
'--progress',
action='store_true',
help="Display progress bar for downloads",
)
filter_ext = parser.add_mutually_exclusive_group()
filter_ext.add_argument(
'-e', '--exclude',
type=str, nargs='*',
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",
)
parser.add_argument(
'-k', '--keys',
type=str, nargs='*',
help=("The purchase download key. Find in the url on the "
"products/bundle download page. Can set multiple"),
)
parser.add_argument(
'-b', '--write-buffer',
type=str, default=1024*1024,
help="Size of file buffer to use"
)
parser.add_argument(
'--chunk_size','--chunk',
type=str,
help='Download Chunk Size'
)
parser.add_argument(
'--timeout',
type=str,
help='Timeout (in seconds) for get requests'
)
parser.add_argument(
'--debug',
action='store_true',
help='Run in Debug Mode. Stops on Exceptions'
)
parser.add_argument(
'--keep',
action='store_true',
help='Keep files that fail download (ie: do not delete on failure)'
)
return parser.parse_args(args)
def cli():
cli_args = parse_args(sys.argv[1:])
from .download_library import DownloadLibrary
DownloadLibrary(
cli_args.library_path,
cookie_path=cli_args.cookie_file,
cookie_auth=cli_args.session_auth,
progress_bar=cli_args.progress,
ext_include=cli_args.include,
ext_exclude=cli_args.exclude,
platform_include=cli_args.platform,
purchase_keys=cli_args.keys,
trove=cli_args.trove,
update=cli_args.update,
write_buffer=parse_size(cli_args.write_buffer),
chunk_size=parse_size(cli_args.chunk_size),
timeout=parse_seconds(cli_args.timeout),
debug=cli_args.debug,
keep=cli_args.keep,
).start()