Skip to content

Commit

Permalink
OpenWeatherMap
Browse files Browse the repository at this point in the history
Pywapi replaced by OpenWeatherMap.
Fixes #163
  • Loading branch information
FrankX0 committed Nov 20, 2021
1 parent 67c5cee commit e9c5284
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
15 changes: 8 additions & 7 deletions wordclock_config/wordclock_config.reference.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,15 @@ activate = True
[plugin_weather_forecast]
activate = True

# choose weather service to retrieve forecast (options: weather_dot_com, yahoo, meteoswiss (note: only for swiss cities))
#weather_service = meteoswiss
#zipcode = 8606 #only needed if using meteoswiss
#weather_service = yahoo
weather_service = weather_dot_com
# choose weather service to retrieve forecast (options: meteoswiss (note: openweathermap, only for swiss cities))
#Data for the OpenWeatherMap service
weather_service = openweathermap
api_key = Your API-key here
city = Your city here

# Location id to access weather forecast services (weather.com, yahoo)
location_id = GMXX3828
#Data for the meteoswiss service
#weather_service = meteoswiss
#zipcode = 8606

### Below here, the wordclocks stencil parameter are provided
### E.g. to create your own/custom stencils
Expand Down
18 changes: 8 additions & 10 deletions wordclock_plugins/weather_forecast/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import os
import pywapi
import requests
import json
import time
Expand All @@ -22,9 +21,12 @@ def __init__(self, config):
self.pretty_name = "Weather forecast"
self.description = "Displays the current temperature"

self.zipcode = config.get('plugin_' + self.name, 'zipcode')
self.location_id = config.get('plugin_' + self.name, 'location_id')
self.weather_service = config.get('plugin_weather_forecast', 'weather_service')
if self.weather_service == 'openweathermap':
self.api_key = config.get('plugin_weather_forecast', 'api_key')
self.city = config.get('plugin_weather_forecast', 'city')
elif self.weather_service == 'meteoswiss':
self.zipcode = config.get('plugin_weather_forecast', 'zipcode')

try:
import am2302_ths
Expand All @@ -40,14 +42,10 @@ def run(self, wcd, wci):
Displaying expected temperature
"""
# Get current forecast
if self.weather_service == 'yahoo':
current_weather_forecast = pywapi.get_weather_from_yahoo(self.location_id)
outdoor_temp = current_weather_forecast['current_conditions']['temperature']
elif self.weather_service == 'weather_dot_com':
current_weather_forecast = pywapi.get_weather_from_weather_com(self.location_id)
outdoor_temp = current_weather_forecast['current_conditions']['temperature']
if self.weather_service == 'openweathermap':
outdoor_temp = str((json.loads(requests.get('http://api.openweathermap.org/data/2.5/weather?q=' + self.city + '&appid=' + self.api_key + '&units=metric').text))['main']['temp'])
elif self.weather_service == 'meteoswiss':
outdoor_temp = (json.loads(requests.get('https://www.meteoschweiz.admin.ch/product/output/weather-widget/forecast/version__20210514_1034/de/' + zipcode + '00.json', headers={'referer': 'https://www.meteoschweiz.admin.ch/home/service-und-publikationen/produkte.html'}).text))['data']['current']['temperature']
outdoor_temp = (json.loads(requests.get('https://www.meteoschweiz.admin.ch/product/output/weather-widget/forecast/version__20210514_1034/de/' + self.zipcode + '00.json', headers={'referer': 'https://www.meteoschweiz.admin.ch/home/service-und-publikationen/produkte.html'}).text))['data']['current']['temperature']
else:
logging.warning('No valid weather_forecast found!')
return
Expand Down

0 comments on commit e9c5284

Please sign in to comment.