Skip to content
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
121 changes: 0 additions & 121 deletions .github/workflows/check-version.yml

This file was deleted.

2 changes: 0 additions & 2 deletions .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ ignore = [
"SIM115", # Use context handler for opening files
"TRY003", # Avoid specifying long messages outside the exception class
"TRY400", # Use `logging.exception` instead of `logging.error`
# Ignored due to performance: https://github.com/charliermarsh/ruff/issues/2923
"UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)`

# May conflict with the formatter, https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
"W191",
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ Parameters:
- **latitude** - The latitude of the location for the forecast
- **longitude** - The longitude of the location for the forecast
- **time** - (optional) A datetime object for the forecast either in the past or future - see How Timezones Work below for the details on how timezones are handled in this library.
- **units** - (optional) A string of the preferred units of measurement, "us" is the default. "us","ca","uk","si" are also available. See the API Docs (https://pirateweather.net/en/latest/API/#units) for exactly what each unit means.
- **units** - (optional) A string of the preferred units of measurement, "auto" is the default. "us","ca","uk","si" are also available. See the API Docs (https://pirateweather.net/en/latest/API/#units) for exactly what each unit means.
- **lang** - (optional) A string of the desired language. See https://pirateweather.net/en/latest/API/#language for supported languages.
- **lazy** - (optional) Defaults to `false`. If `true` the function will request the json data as it is needed. Results in more requests, but maybe a faster response time.
- **extend** - (optional) Defaults to `false`. If `"hourly"` the API will hourly data for 168 hours instead of the standard 48 hours.
- **version** - (optional) Defaults to `1`. If set to `2` the API will return fields that were not part of the Dark Sky API.
- **icon** - (optional) Defaults to `darksky`. If set to `pirate` the API will return icons which aren't apart of the default Dark Sky icon set.
- **extraVars** - (optional) Is used to add additional parameters to the API response. The only extra parameter at the moment is `stationPressure` but more may be added in the future.
- **callback** - (optional) Pass a function to be used as a callback. If used, load_forecast() will use an asynchronous HTTP call and **will not return the forecast object directly**, instead it will be passed to the callback function. Make sure it can accept it.

----------------------------------------------------
Expand Down
15 changes: 9 additions & 6 deletions pirateweather/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ def load_forecast(
lat,
lng,
time=None,
units="us",
units="auto",
lang="en",
lazy=False,
callback=None,
extend=None,
version=1,
icon="darksky",
extraVars=None,
Comment thread
cloneofghosts marked this conversation as resolved.
):
"""Build the request url and loads some or all of the needed json depending on lazy is True.

Expand All @@ -27,7 +28,7 @@ def load_forecast(
time: A datetime.datetime object representing the desired time of
the forecast. If no timezone is present, the API assumes local
time at the provided latitude and longitude.
units: A string of the preferred units of measurement, "us" is
units: A string of the preferred units of measurement, "auto" is
default. also ca,uk,si is available
lang: Return summary properties in the desired language
lazy: Defaults to false. The function will only request the json
Expand All @@ -37,13 +38,15 @@ def load_forecast(
of the standard 48 hours.
version: If set to 2 the API will return fields that were not part of the Dark Sky API.
icon: If set to pirate the API will return icons which aren't apart of the default Dark Sky icon set
extraVars: Is used to add additional parameters to the API response.
"""

if time is None:
url = (
f"https://api.pirateweather.net/forecast/{key}/{lat},{lng}"
f"?units={units}&lang={lang}&extend={extend}&version={version}&icon={icon}"
)
url = f"https://api.pirateweather.net/forecast/{key}/{lat},{lng}?units={units}&lang={lang}&version={version}&icon={icon}"
if extend:
url += f"&extend={extend}"
if extraVars:
url += f"&extraVars={extraVars}"
else:
url_time = time.replace(
microsecond=0
Expand Down
3 changes: 3 additions & 0 deletions pirateweather/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ def __init__(self, d=None):
self.nearestStation = d.get("nearest-station")
self.sources = list(d.get("sources"))
self.sourceTimes = d.get("sourceTimes")
self.processTime = d.get("processTime")
self.ingestVersion = d.get("ingestVersion")
self.nearestCity = d.get("nearestCity")
Comment thread
cloneofghosts marked this conversation as resolved.

def __unicode__(self):
"""Return a string representation of the data block."""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def read(fname):

setup(
name="python-pirateweather",
version="1.1.2",
version="1.2.0",
author="cloneofghosts",
description=("A thin Python Wrapper for the Pirate Weather API"),
license="BSD 2-clause",
Expand Down
69 changes: 56 additions & 13 deletions tests/test_pirateweather.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def setUp(self):
"""Set up the API key, location and time for the tests."""

self.api_key = os.environ.get("PIRATEWEATHER_API_KEY")
if not self.api_key:
# Skip end-to-end tests when the API key isn't available in CI/local dev
# They require network access and a valid key.
self.skipTest("PIRATEWEATHER_API_KEY not set")
self.lat = 52.370235
self.lng = 4.903549
self.time = datetime(2015, 2, 27, 6, 0, 0)
Expand Down Expand Up @@ -58,7 +62,7 @@ def test_extend(self):
hourl_data = forecast.hourly()

assert forecast.response.status_code == 200
assert len(hourl_data.data) == 169
assert len(hourl_data.data) == 168

def test_version(self):
"""Test querying the API endpoint."""
Expand All @@ -81,17 +85,33 @@ def test_version_data_point(self):
assert forecast.response.status_code == 200
assert fc_cur.fireIndex

def test_extra_vars(self):
"""Test querying the API endpoint."""

forecast = pirateweather.load_forecast(
self.api_key, self.lat, self.lng, units="us", extraVars="stationPressure"
)
fc_cur = forecast.currently()

assert forecast.response.status_code == 200
assert fc_cur.stationPressure

def test_flags(self):
"""Test the data returned by the flags block."""

forecast = pirateweather.load_forecast(self.api_key, self.lat, self.lng)
forecast = pirateweather.load_forecast(
self.api_key, self.lat, self.lng, version=2
)
flags = forecast.flags()

assert len(flags.sources) == 3
assert len(flags.sourceTimes) == 2
assert flags.nearestStation == 0
assert flags.units == "us"
assert flags.units == "si"
assert flags.sourceTimes.get("gfs")
assert flags.processTime
assert flags.ingestVersion
assert flags.nearestCity == "Amsterdam"
Comment thread
cloneofghosts marked this conversation as resolved.

def test_invalid_key(self):
"""Test querying the API endpoint with a invalid API key."""
Expand Down Expand Up @@ -128,15 +148,25 @@ class BasicFunctionality(unittest.TestCase):
@responses.activate
def setUp(self):
"""Set up the data to use in the next tests."""

URL = "https://api.pirateweather.net/forecast/foo/50.0,10.0?units=us&lang=en&extend=None&version=1"
URL = "https://api.pirateweather.net/forecast/foo/50.0,10.0?units=auto&lang=en&version=1&icon=darksky"
responses.add(
responses.GET,
URL,
body=open(os.path.join("./fixtures/test.json")).read(),
body=open(
os.path.join(os.path.dirname(__file__), "fixtures", "test.json")
).read(),
status=200,
content_type="application/json",
match_querystring=True,
match=[
responses.matchers.query_param_matcher(
{
"units": "auto",
"lang": "en",
"version": "1",
"icon": "darksky",
}
)
],
)

api_key = "foo"
Expand Down Expand Up @@ -229,15 +259,27 @@ class ForecastsWithAlerts(unittest.TestCase):
@responses.activate
def setUp(self):
"""Set up the test data with alerts to use in the next tests."""

URL = "https://api.pirateweather.net/forecast/foo/50.0,10.0?units=us&lang=en&extend=None&version=1"
URL = "https://api.pirateweather.net/forecast/foo/50.0,10.0?units=auto&lang=en&version=1&icon=darksky"
responses.add(
responses.GET,
URL,
body=open(os.path.join("./fixtures/test_with_alerts.json")).read(),
body=open(
os.path.join(
os.path.dirname(__file__), "fixtures", "test_with_alerts.json"
)
).read(),
status=200,
content_type="application/json",
match_querystring=True,
match=[
responses.matchers.query_param_matcher(
{
"units": "auto",
"lang": "en",
"version": "1",
"icon": "darksky",
}
)
],
)

api_key = "foo"
Expand Down Expand Up @@ -306,10 +348,11 @@ def setUp(self):
responses.add(
responses.GET,
URL,
body=open(os.path.join("./fixtures/test.json")).read(),
body=open(
os.path.join(os.path.dirname(__file__), "fixtures", "test.json")
).read(),
status=200,
content_type="application/json",
match_querystring=True,
)

self.forecast = pirateweather.manual("http://test_url.com/")
Expand Down