-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from caarmen/issue-11-integrate-weatherapi
Issue #11: Add weatherapi support
- Loading branch information
Showing
6 changed files
with
79 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from wspp import weatherapi, weatherstack | ||
from wspp.settings import ProviderName, Settings | ||
|
||
|
||
def get_current_weather_code( | ||
settings: Settings, | ||
is_day: bool, | ||
) -> int: | ||
""" | ||
:return: The weather_code for the current condition at the location defined | ||
in the environment variable. | ||
""" | ||
if not is_day: | ||
return 999 | ||
|
||
if settings.weatherprovider.name == ProviderName.WEATHERSTACK: | ||
provider_function = weatherstack.get_current_weather_code | ||
else: | ||
provider_function = weatherapi.get_current_weather_code | ||
|
||
return provider_function( | ||
api_access_key=settings.weatherprovider.api_access_key, | ||
latitude=settings.wspp.latitude, | ||
longitude=settings.wspp.longitude, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import re | ||
|
||
import requests | ||
|
||
|
||
def get_current_weather_code( | ||
api_access_key: str, | ||
latitude: float, | ||
longitude: float, | ||
) -> int: | ||
""" | ||
:return: The weather_code for the current condition at the location defined | ||
in the environment variable. | ||
""" | ||
response = requests.get( | ||
url="http://api.weatherapi.com/v1/current.json", | ||
params={ | ||
"key": api_access_key, | ||
"q": f"{latitude},{longitude}", | ||
"aqi": "no", | ||
}, | ||
) | ||
if not response.ok: | ||
raise Exception(response.text) | ||
response_data: dict = response.json() | ||
# ex: //cdn.weatherapi.com/weather/64x64/day/248.png | ||
icon = response_data["current"]["condition"]["icon"] | ||
pattern = re.compile(r"^.*/([0-9]*).png$") | ||
tokens = pattern.findall(icon) | ||
code = tokens[-1] | ||
|
||
return code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters