Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra pattern to extract url from download-form returned by Google Drive for a large file #308

Merged
merged 6 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions gdown/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import tempfile
import textwrap
import time
import urllib
import urllib.parse
from http.cookiejar import MozillaCookieJar

import bs4
import requests
import tqdm

Expand All @@ -28,10 +29,16 @@ def get_url_from_gdrive_confirmation(contents):
url = "https://docs.google.com" + m.groups()[0]
url = url.replace("&", "&")
break
m = re.search('id="download-form" action="(.+?)"', line)
if m:
url = m.groups()[0]
url = url.replace("&", "&")
soup = bs4.BeautifulSoup(line, features="html.parser")
form = soup.select_one("#download-form")
if form is not None:
url = form["action"].replace("&", "&")
url_components = urllib.parse.urlsplit(url)
query_params = urllib.parse.parse_qs(url_components.query)
for param in form.findChildren("input", attrs={"type": "hidden"}):
query_params[param["name"]] = param["value"]
query = urllib.parse.urlencode(query_params, doseq=True)
url = urllib.parse.urlunsplit(url_components._replace(query=query))
break
m = re.search('"downloadUrl":"([^"]+)', line)
if m:
Expand All @@ -52,6 +59,22 @@ def get_url_from_gdrive_confirmation(contents):
return url


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:
filename = m.groups()[0]
return filename.replace(osp.sep, "_")

m = re.search('attachment; filename="(.*?)"', content_disposition)
if m:
filename = m.groups()[0]
return filename

return None


def _get_session(proxy, use_cookies, user_agent, return_cookies_file=False):
sess = requests.session()

Expand Down Expand Up @@ -232,12 +255,10 @@ def download(
)
raise FileURLRetrievalError(message)

filename_from_url = None
if gdrive_file_id and is_gdrive_download_link:
content_disposition = urllib.parse.unquote(res.headers["Content-Disposition"])
m = re.search(r"filename\*=UTF-8''(.*)", content_disposition)
filename_from_url = m.groups()[0]
filename_from_url = filename_from_url.replace(osp.sep, "_")
else:
filename_from_url = _get_filename_from_response(response=res)
if filename_from_url is None:
filename_from_url = osp.basename(url)

if output is None:
Expand Down
2 changes: 1 addition & 1 deletion gdown/parse_url.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import re
import urllib
import urllib.parse
import warnings


Expand Down
Loading