-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Pirate Weather provider - https://pirateweather.net
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
1 parent
02ebf13
commit 82891ef
Showing
3 changed files
with
86 additions
and
0 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
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 |
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