Skip to content

Commit

Permalink
Add Pirate Weather provider - https://pirateweather.net
Browse files Browse the repository at this point in the history
Drop-in replacement for Dark Sky, except for the fact that it doesn't
return sunrise/sunset times under the current conditions. Have to
request `daily` data for those. Kinda silly.
  • Loading branch information
dgw authored and RustyBower committed Apr 4, 2023
1 parent 02ebf13 commit 82891ef
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ OpenWeatherMap
https://openweathermap.org/
Pirate Weather

A drop-in replacement API for the old Dark Sky service bought by Apple.

.. code-block::
https://pirateweather.net/
Python Requirements
~~~~~~~~~~~~~~~~~~~
.. code-block::
Expand Down
70 changes: 70 additions & 0 deletions sopel_modules/weather/providers/weather/pirateweather.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# coding=utf-8
import requests

from datetime import datetime


def pirateweather_forecast(bot, latitude, longitude, location):
url = 'https://api.pirateweather.net/forecast/{}/{},{}'.format(
bot.config.weather.weather_api_key,
latitude,
longitude
)

params = {
'exclude': 'currently,minutely,hourly,alerts,flags', # Exclude extra data we don't want/need
'units': 'si'
}
try:
r = requests.get(url, params=params)
except:
raise Exception("An Error Occurred. Check Logs For More Information.")
data = r.json()
if r.status_code != 200:
raise Exception('Error: {}'.format(data['error']))
else:
weather_data = {'location': location, 'data': []}
for day in data['daily']['data'][0:4]:
weather_data['data'].append({
'dow': datetime.fromtimestamp(day['time']).strftime('%A'),
'summary': day['summary'].strip('.'),
'high_temp': day['temperatureHigh'],
'low_temp': day['temperatureLow']
})
return weather_data


def pirateweather_weather(bot, latitude, longitude, location):
url = 'https://api.pirateweather.net/forecast/{}/{},{}'.format(
bot.config.weather.weather_api_key,
latitude,
longitude
)

params = {
'exclude': 'minutely,hourly,alerts,flags', # Exclude extra data we don't want/need
'units': 'si',
}
try:
r = requests.get(url, params=params)
except:
raise Exception("An Error Occurred. Check Logs For More Information.")
data = r.json()
if r.status_code != 200:
raise Exception('Error: {}'.format(data['error']))
else:
weather_data = {
'location': location,
'temp': data['currently']['temperature'],
'condition': data['currently']['summary'],
'humidity': data['currently']['humidity'],
'wind': {'speed': data['currently']['windSpeed'], 'bearing': data['currently']['windBearing']},
'uvindex': data['currently']['uvIndex'],
'timezone': data['timezone'],
}

if bot.config.weather.sunrise_sunset:
weather_data['sunrise'] = data['daily']['data'][0]['sunriseTime']
weather_data['sunset'] = data['daily']['data'][0]['sunsetTime']

return weather_data
8 changes: 8 additions & 0 deletions sopel_modules/weather/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
from .providers.weather.darksky import darksky_forecast, darksky_weather
from .providers.weather.openmeteo import openmeteo_forecast, openmeteo_weather
from .providers.weather.openweathermap import openweathermap_forecast, openweathermap_weather
from .providers.weather.pirateweather import pirateweather_forecast, pirateweather_weather

WEATHER_PROVIDERS = [
'darksky',
'openmeteo',
'openweathermap',
'pirateweather',
]

GEOCOORDS_PROVIDERS = {
Expand Down Expand Up @@ -246,6 +248,9 @@ def get_forecast(bot, trigger):
# OpenWeatherMap
elif bot.config.weather.weather_provider == 'openweathermap':
return openweathermap_forecast(bot, latitude, longitude, location)
# Pirate Weather
elif bot.config.weather.weather_provider == 'pirateweather':
return pirateweather_forecast(bot, latitude, longitude, location)
# Unsupported Provider
else:
raise Exception('Error: Unsupported Provider')
Expand All @@ -267,6 +272,9 @@ def get_weather(bot, trigger):
# OpenWeatherMap
elif bot.config.weather.weather_provider == 'openweathermap':
return openweathermap_weather(bot, latitude, longitude, location)
# Pirate Weather
elif bot.config.weather.weather_provider == 'pirateweather':
return pirateweather_weather(bot, latitude, longitude, location)
# Unsupported Provider
else:
raise Exception('Error: Unsupported Provider')
Expand Down

0 comments on commit 82891ef

Please sign in to comment.