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

Adding proxy configuration #143

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
39 changes: 25 additions & 14 deletions meteostat/core/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
The code is licensed under the MIT license.
"""

from io import BytesIO
from gzip import GzipFile
from urllib.request import urlopen, Request, ProxyHandler, build_opener
from urllib.error import HTTPError
from multiprocessing import Pool
from multiprocessing.pool import ThreadPool
Expand Down Expand Up @@ -70,28 +73,36 @@ def load_handler(
columns: list,
types: Union[dict, None],
parse_dates: list,
coerce_dates: bool = False,
proxy: str = None,
coerce_dates: bool = False
) -> pd.DataFrame:
"""
Load a single CSV file into a DataFrame
"""

try:
handlers = []

# Set a proxy
if proxy:
opener = (ProxyHandler({'http': proxy, 'https': proxy}))

# Read CSV file from Meteostat endpoint
df = pd.read_csv(
endpoint + path,
compression="gzip",
names=columns,
dtype=types,
parse_dates=parse_dates,
)

# Force datetime conversion
if coerce_dates:
df.iloc[:, parse_dates] = df.iloc[:, parse_dates].apply(
pd.to_datetime, errors="coerce"
)
with build_opener(*handlers).open(Request(endpoint + path)) as response:
# Decompress the content
with GzipFile(fileobj=BytesIO(response.read()), mode='rb') as file:
df = pd.read_csv(
file,
names=columns,
dtype=types,
parse_dates=parse_dates,
)

# Force datetime conversion
if coerce_dates:
df.iloc[:, parse_dates] = df.iloc[:, parse_dates].apply(
pd.to_datetime, errors="coerce"
)

except (FileNotFoundError, HTTPError):

Expand Down
3 changes: 3 additions & 0 deletions meteostat/interface/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class Base:
# Base URL of the Meteostat bulk data interface
endpoint: str = "https://bulk.meteostat.net/v2/"

# Proxy URL for the Meteostat bulk data interface
proxy: str = None

# Location of the cache directory
cache_dir: str = os.path.expanduser("~") + os.sep + ".meteostat" + os.sep + "cache"

Expand Down
7 changes: 6 additions & 1 deletion meteostat/interface/meteodata.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ def _load_data(self, station: str, year: Union[int, None] = None) -> None:

# Get data from Meteostat
df = load_handler(
self.endpoint, file, self._columns, self._types, self._parse_dates
self.endpoint,
file,
self._columns,
self._types,
self._parse_dates,
self.proxy
)

# Validate and prepare data for further processing
Expand Down
8 changes: 7 additions & 1 deletion meteostat/interface/stations.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ def _load(self) -> None:

# Get data from Meteostat
df = load_handler(
self.endpoint, file, self._columns, self._types, self._parse_dates, True
self.endpoint,
file,
self._columns,
self._types,
self._parse_dates,
self.proxy,
True
)

# Add index
Expand Down
1 change: 1 addition & 0 deletions meteostat/interface/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def _load_flags(self, station: str, year: Union[int, None] = None) -> None:
self._columns,
{key: "string" for key in self._columns[self._first_met_col :]},
self._parse_dates,
self.proxy
)

# Validate Series
Expand Down