Skip to content

Commit

Permalink
only exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Skitionek committed Mar 1, 2023
1 parent e4cb896 commit 217e5c8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 69 deletions.
7 changes: 2 additions & 5 deletions gdown/cached_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,8 @@ def cached_download(
print("File exists: {}".format(path))
return path
elif osp.exists(path) and md5:
try:
assert_md5sum(path, md5, quiet=quiet)
return path
except AssertionError as e:
print(e, file=sys.stderr)
assert_md5sum(path, md5, quiet=quiet)
return path

# download
lock_path = osp.join(cache_root, "_dl_lock")
Expand Down
58 changes: 31 additions & 27 deletions gdown/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,33 +134,37 @@ def main():
url = None
id = args.url_or_id

if args.folder:
filenames = download_folder(
url=url,
id=id,
output=args.output,
quiet=args.quiet,
proxy=args.proxy,
speed=args.speed,
use_cookies=not args.no_cookies,
verify=not args.no_check_certificate,
remaining_ok=args.remaining_ok,
)
success = filenames is not None
else:
filename = download(
url=url,
output=args.output,
quiet=args.quiet,
proxy=args.proxy,
speed=args.speed,
use_cookies=not args.no_cookies,
verify=not args.no_check_certificate,
id=id,
fuzzy=args.fuzzy,
resume=args.continue_,
)
success = filename is not None
try:
if args.folder:
filenames = download_folder(
url=url,
id=id,
output=args.output,
quiet=args.quiet,
proxy=args.proxy,
speed=args.speed,
use_cookies=not args.no_cookies,
verify=not args.no_check_certificate,
remaining_ok=args.remaining_ok,
)
success = filenames is not None
else:
filename = download(
url=url,
output=args.output,
quiet=args.quiet,
proxy=args.proxy,
speed=args.speed,
use_cookies=not args.no_cookies,
verify=not args.no_check_certificate,
id=id,
fuzzy=args.fuzzy,
resume=args.continue_,
)
success = filename is not None
except Exception as e:
print(e, file=sys.stderr)
success = False

if not success:
sys.exit(1)
Expand Down
45 changes: 16 additions & 29 deletions gdown/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import requests
import six
import tqdm
from requests import HTTPError, RequestException
from requests.exceptions import ProxyError

from .parse_url import parse_url

Expand Down Expand Up @@ -153,10 +155,8 @@ def download(
while True:
try:
res = sess.get(url, headers=headers, stream=True, verify=verify)
except requests.exceptions.ProxyError as e:
print("An error has occurred using proxy:", proxy, file=sys.stderr)
print(e, file=sys.stderr)
return
except ProxyError as e:
raise ProxyError(f"An error has occurred using proxy: ${proxy}", code=407) from e

if use_cookies:
if not osp.exists(osp.dirname(cookies_file)):
Expand All @@ -180,16 +180,14 @@ def download(
try:
url = get_url_from_gdrive_confirmation(res.text)
except RuntimeError as e:
print("Access denied with the following error:")
error = "\n".join(textwrap.wrap(str(e)))
error = indent(error, "\t")
print("\n", error, "\n", file=sys.stderr)
print(
"You may still be able to access the file from the browser:",
file=sys.stderr,
)
print("\n\t", url_origin, "\n", file=sys.stderr)
return
raise HTTPError(
"Access denied with the following error:\n" +
indent("\n".join(textwrap.wrap(str(e))), "\t") +
"\n" +
"You may still be able to access the file from the browser:" +
"\n\t" + url_origin + "\n",
code=403
) from e

if gdrive_file_id and is_gdrive_download_link:
content_disposition = six.moves.urllib_parse.unquote(
Expand Down Expand Up @@ -217,19 +215,11 @@ def download(
existing_tmp_files.append(osp.join(osp.dirname(output), file))
if resume and existing_tmp_files:
if len(existing_tmp_files) != 1:
print(
"There are multiple temporary files to resume:",
file=sys.stderr,
)
print("\n")
for file in existing_tmp_files:
print("\t", file, file=sys.stderr)
print("\n")
print(
"Please remove them except one to resume downloading.",
file=sys.stderr,
raise RequestException(
"There are multiple temporary files to resume:\n" +
"\t".join(existing_tmp_files) + "\n" +
"Please remove them except one to resume downloading."
)
return
tmp_file = existing_tmp_files[0]
else:
resume = False
Expand Down Expand Up @@ -281,9 +271,6 @@ def download(
if tmp_file:
f.close()
shutil.move(tmp_file, output)
except IOError as e:
print(e, file=sys.stderr)
return
finally:
sess.close()

Expand Down
14 changes: 6 additions & 8 deletions gdown/download_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import warnings

import bs4
from requests import RequestException, HTTPError

from .download import _get_session
from .download import download
Expand Down Expand Up @@ -262,11 +263,10 @@ def download_folder(
verify=verify,
)
except RuntimeError as e:
print("Failed to retrieve folder contents:", file=sys.stderr)
error = "\n".join(textwrap.wrap(str(e)))
error = indent(error, "\t")
print("\n", error, "\n", file=sys.stderr)
return
raise RequestException(
"Failed to retrieve folder contents:" +
indent("\n".join(textwrap.wrap(str(e))), "\t")
) from e

if not return_code:
return return_code
Expand Down Expand Up @@ -303,9 +303,7 @@ def download_folder(
)

if filename is None:
if not quiet:
print("Download ended unsuccessfully", file=sys.stderr)
return
raise HTTPError("Download ended unsuccessfully")
filenames.append(filename)
if not quiet:
print("Download completed", file=sys.stderr)
Expand Down

0 comments on commit 217e5c8

Please sign in to comment.