Add --retry cmdarg

This commit is contained in:
Ethan Dalool 2017-03-30 15:55:22 -07:00
parent 35cef050c2
commit b738dd3d41

View file

@ -27,6 +27,8 @@ TEMP_EXTENSION = '.downloadytemp'
PRINT_LIMITER = ratelimiter.Ratelimiter(allowance=5, mode='reject') PRINT_LIMITER = ratelimiter.Ratelimiter(allowance=5, mode='reject')
class NotEnoughBytes(Exception):
pass
def download_file( def download_file(
url, url,
@ -131,7 +133,7 @@ def download_plan(plan):
if plan['raise_for_undersized'] and undersized: if plan['raise_for_undersized'] and undersized:
message = 'File does not contain expected number of bytes. Received {size} / {total}' message = 'File does not contain expected number of bytes. Received {size} / {total}'
message = message.format(size=localsize, total=plan['remote_total_bytes']) message = message.format(size=localsize, total=plan['remote_total_bytes'])
raise Exception(message) raise NotEnoughBytes(message)
if localname != plan['real_localname']: if localname != plan['real_localname']:
os.rename(localname, plan['real_localname']) os.rename(localname, plan['real_localname'])
@ -406,6 +408,13 @@ def download_argparse(args):
if args.range is not None: if args.range is not None:
headers['range'] = 'bytes=%s' % args.range headers['range'] = 'bytes=%s' % args.range
retry = args.retry
if not retry:
retry = 1
while retry != 0:
# Negative numbers permit infinite retries.
try:
download_file( download_file(
url=url, url=url,
localname=args.localname, localname=args.localname,
@ -414,9 +423,15 @@ def download_argparse(args):
do_head=args.no_head is False, do_head=args.no_head is False,
headers=headers, headers=headers,
overwrite=args.overwrite, overwrite=args.overwrite,
timeout=int(args.timeout), timeout=args.timeout,
verbose=True, verbose=True,
) )
except (NotEnoughBytes, requests.exceptions.ConnectionError):
retry -= 1
if retry == 0:
raise
else:
break
if __name__ == '__main__': if __name__ == '__main__':
@ -428,7 +443,8 @@ if __name__ == '__main__':
parser.add_argument('-bps', '--bytespersecond', dest='bytespersecond', default=None) parser.add_argument('-bps', '--bytespersecond', dest='bytespersecond', default=None)
parser.add_argument('-ow', '--overwrite', dest='overwrite', action='store_true') parser.add_argument('-ow', '--overwrite', dest='overwrite', action='store_true')
parser.add_argument('-r', '--range', dest='range', default=None) parser.add_argument('-r', '--range', dest='range', default=None)
parser.add_argument('--timeout', dest='timeout', default=TIMEOUT) parser.add_argument('--timeout', dest='timeout', type=int, default=TIMEOUT)
parser.add_argument('--retry', dest='retry', const=-1, nargs='?', type=int, default=1)
parser.add_argument('--no-head', dest='no_head', action='store_true') parser.add_argument('--no-head', dest='no_head', action='store_true')
parser.set_defaults(func=download_argparse) parser.set_defaults(func=download_argparse)