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 option in download() to appear as browser #1277

Merged
merged 1 commit into from
Jul 8, 2019
Merged
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
15 changes: 11 additions & 4 deletions openmc/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import os.path
from pathlib import Path
from urllib.parse import urlparse
from urllib.request import urlopen
from urllib.request import urlopen, Request

_BLOCK_SIZE = 16384


def download(url, checksum=None, **kwargs):
def download(url, checksum=None, as_browser=False, **kwargs):
"""Download file from a URL

Parameters
Expand All @@ -16,15 +16,22 @@ def download(url, checksum=None, **kwargs):
URL from which to download
checksum : str or None
MD5 checksum to check against
Keyword arguments passed to :func:urllib.request.urlopen
as_browser : bool
Change User-Agent header to appear as a browser
kwargs : dict
Keyword arguments passed to :func:urllib.request.urlopen

Returns
-------
basename : str
Name of file written locally

"""
req = urlopen(url, **kwargs)
if as_browser:
page = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
else:
page = url
req = urlopen(page, **kwargs)
# Get file size from header
file_size = req.length

Expand Down