Skip to content

Commit

Permalink
Set default user-agent with giving option to override it
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Jan 16, 2024
1 parent 44cd4df commit 975310b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
6 changes: 6 additions & 0 deletions gdown/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ def main():
help="Format of Google Docs, Spreadsheets and Slides. "
"Default is Google Docs: 'docx', Spreadsheet: 'xlsx', Slides: 'pptx'.",
)
parser.add_argument(
"--user-agent",
help="User-Agent to use for downloading file.",
)

args = parser.parse_args()

Expand Down Expand Up @@ -159,6 +163,7 @@ def main():
use_cookies=not args.no_cookies,
verify=not args.no_check_certificate,
remaining_ok=args.remaining_ok,
user_agent=args.user_agent,
)
else:
download(
Expand All @@ -173,6 +178,7 @@ def main():
fuzzy=args.fuzzy,
resume=args.continue_,
format=args.format,
user_agent=args.user_agent,
)
except FileURLRetrievalError as e:
print(e, file=sys.stderr)
Expand Down
17 changes: 12 additions & 5 deletions gdown/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,10 @@ def get_url_from_gdrive_confirmation(contents):
return url


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

sess.headers.update(
{"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)"}
)
sess.headers.update({"User-Agent": user_agent})

if proxy is not None:
sess.proxies = {"http": proxy, "https": proxy}
Expand Down Expand Up @@ -91,6 +89,7 @@ def download(
fuzzy=False,
resume=False,
format=None,
user_agent=None,
):
"""Download file from URL.
Expand Down Expand Up @@ -124,6 +123,8 @@ def download(
- Google Docs: 'docx'
- Google Spreadsheet: 'xlsx'
- Google Slides: 'pptx'
user_agent: str, optional
User-agent to use in the HTTP request.
Returns
-------
Expand All @@ -134,11 +135,17 @@ def download(
raise ValueError("Either url or id has to be specified")
if id is not None:
url = "https://drive.google.com/uc?id={id}".format(id=id)
if user_agent is None:
# We need to use different user agent for file download c.f., folder
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36" # NOQA: E501

url_origin = url

sess, cookies_file = _get_session(
proxy=proxy, use_cookies=use_cookies, return_cookies_file=True
proxy=proxy,
use_cookies=use_cookies,
user_agent=user_agent,
return_cookies_file=True,
)

gdrive_file_id, is_gdrive_download_link = parse_url(url, warning=not fuzzy)
Expand Down
10 changes: 9 additions & 1 deletion gdown/download_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def download_folder(
use_cookies=True,
remaining_ok=False,
verify=True,
user_agent=None,
):
"""Downloads entire folder from URL.
Expand All @@ -228,6 +229,8 @@ def download_folder(
Either a bool, in which case it controls whether the server's TLS
certificate is verified, or a string, in which case it must be a path
to a CA bundle to use. Default is True.
user_agent: str, optional
User-agent to use in the HTTP request.
Returns
-------
Expand All @@ -245,8 +248,13 @@ def download_folder(
raise ValueError("Either url or id has to be specified")
if id is not None:
url = "https://drive.google.com/drive/folders/{id}".format(id=id)
if user_agent is None:
# We need to use different user agent for folder download c.f., file
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36" # NOQA: E501

sess = _get_session(proxy=proxy, use_cookies=use_cookies)
sess = _get_session(
proxy=proxy, use_cookies=use_cookies, user_agent=user_agent
)

if not quiet:
print("Retrieving folder contents", file=sys.stderr)
Expand Down

0 comments on commit 975310b

Please sign in to comment.