Skip to content

Commit

Permalink
Refactor res -> response in download.py
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Feb 1, 2024
1 parent 7b4caf2 commit d2d73dc
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions gdown/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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("<title>(.+)</title>", res.text)
if response.headers["Content-Type"].startswith("text/html"):
m = re.search("<title>(.+)</title>", response.text)
if m and m.groups()[0].endswith(" - Google Docs"):
url = (
"https://docs.google.com/document/d/{id}/export"
Expand Down Expand Up @@ -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 = (
Expand All @@ -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"
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand Down

0 comments on commit d2d73dc

Please sign in to comment.