From d2d73dcccb3aa8e58880a153e0a3a6257aa6d468 Mon Sep 17 00:00:00 2001 From: Kentaro Wada Date: Thu, 1 Feb 2024 22:42:07 +0900 Subject: [PATCH] Refactor res -> response in download.py --- gdown/download.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/gdown/download.py b/gdown/download.py index d3d17f8f..73981843 100644 --- a/gdown/download.py +++ b/gdown/download.py @@ -59,8 +59,8 @@ def _get_url_from_gdrive_confirmation(contents): return url -def _get_filename_from_response(res): - content_disposition = urllib.parse.unquote(res.headers["Content-Disposition"]) +def _get_filename_from_response(response): + content_disposition = urllib.parse.unquote(response.headers["Content-Disposition"]) m = re.search(r"filename\*=UTF-8''(.*)", content_disposition) if m: @@ -177,18 +177,18 @@ def download( is_gdrive_download_link = True while True: - res = sess.get(url, stream=True, verify=verify) + response = sess.get(url, stream=True, verify=verify) if not (gdrive_file_id and is_gdrive_download_link): break - if url == url_origin and res.status_code == 500: + if url == url_origin and response.status_code == 500: # The file could be Google Docs or Spreadsheets. url = "https://drive.google.com/open?id={id}".format(id=gdrive_file_id) continue - if res.headers["Content-Type"].startswith("text/html"): - m = re.search("(.+)", res.text) + if response.headers["Content-Type"].startswith("text/html"): + m = re.search("(.+)", response.text) if m and m.groups()[0].endswith(" - Google Docs"): url = ( "https://docs.google.com/document/d/{id}/export" @@ -217,8 +217,8 @@ def download( ) continue elif ( - "Content-Disposition" in res.headers - and res.headers["Content-Disposition"].endswith("pptx") + "Content-Disposition" in response.headers + and response.headers["Content-Disposition"].endswith("pptx") and format not in {None, "pptx"} ): url = ( @@ -236,13 +236,13 @@ def download( cookie_jar.set_cookie(cookie) cookie_jar.save() - if "Content-Disposition" in res.headers: + if "Content-Disposition" in response.headers: # This is the file break # Need to redirect with confirmation try: - url = _get_url_from_gdrive_confirmation(res.text) + url = _get_url_from_gdrive_confirmation(response.text) except FileURLRetrievalError as e: message = ( "Failed to retrieve file url:\n\n{}\n\n" @@ -257,7 +257,7 @@ def download( filename_from_url = None if gdrive_file_id and is_gdrive_download_link: - filename_from_url = _get_filename_from_response(res) + filename_from_url = _get_filename_from_response(response=response) if filename_from_url is None: filename_from_url = osp.basename(url) @@ -307,7 +307,7 @@ def download( if tmp_file is not None and f.tell() != 0: headers = {"Range": "bytes={}-".format(f.tell())} - res = sess.get(url, headers=headers, stream=True, verify=verify) + response = sess.get(url, headers=headers, stream=True, verify=verify) if not quiet: print("Downloading...", file=sys.stderr) @@ -325,13 +325,13 @@ def download( ) try: - total = res.headers.get("Content-Length") + total = response.headers.get("Content-Length") if total is not None: total = int(total) if not quiet: pbar = tqdm.tqdm(total=total, unit="B", unit_scale=True) t_start = time.time() - for chunk in res.iter_content(chunk_size=CHUNK_SIZE): + for chunk in response.iter_content(chunk_size=CHUNK_SIZE): f.write(chunk) if not quiet: pbar.update(len(chunk))