Skip to content

Commit

Permalink
Add FileURLRetrievalError
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Apr 13, 2023
1 parent 58bd2ee commit 4689fac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
4 changes: 4 additions & 0 deletions gdown/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .download import indent
from .download_folder import MAX_NUMBER_FILES
from .download_folder import download_folder
from .exceptions import FileURLRetrievalError
from .exceptions import FolderContentsMaximumLimitError


Expand Down Expand Up @@ -170,6 +171,9 @@ def main():
resume=args.continue_,
format=args.format,
)
except FileURLRetrievalError as e:
print(e, file=sys.stderr)
sys.exit(1)
except FolderContentsMaximumLimitError as e:
print(
"Failed to retrieve folder contents:\n\n{}\n\n"
Expand Down
25 changes: 13 additions & 12 deletions gdown/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import six
import tqdm

from .exceptions import FileURLRetrievalError
from .parse_url import parse_url

CHUNK_SIZE = 512 * 1024 # 512KB
Expand Down Expand Up @@ -51,9 +52,9 @@ def get_url_from_gdrive_confirmation(contents):
m = re.search('<p class="uc-error-subcaption">(.*)</p>', line)
if m:
error = m.groups()[0]
raise RuntimeError(error)
raise FileURLRetrievalError(error)
if not url:
raise RuntimeError(
raise FileURLRetrievalError(
"Cannot retrieve the public link of the file. "
"You may need to change the permission to "
"'Anyone with the link', or have had many accesses."
Expand Down Expand Up @@ -230,17 +231,17 @@ def download(
# Need to redirect with confirmation
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,
except FileURLRetrievalError as e:
message = (
"Failed to retrieve file url:\n\n{}\n\n"
"You may still be able to access the file from the browser:"
"\n\n\t{}\n\n"
"but Gdown can't. Please check connections and permissions."
).format(
indent("\n".join(textwrap.wrap(str(e))), prefix="\t"),
url_origin,
)
print("\n\t", url_origin, "\n", file=sys.stderr)
return
raise FileURLRetrievalError(message)

if gdrive_file_id and is_gdrive_download_link:
content_disposition = six.moves.urllib_parse.unquote(
Expand Down
4 changes: 4 additions & 0 deletions gdown/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
class FileURLRetrievalError(Exception):
pass


class FolderContentsMaximumLimitError(Exception):
pass

0 comments on commit 4689fac

Please sign in to comment.