pyowm improvements #137

- Bump pyowm from 2.10.0 to 3.1.1 (latest)
- updated Weather module to support new pyowm version
- removed ad-hoc logic for beaufort scale (supported by pyowm now)
- Added support for location ID and location string (see #137)
This commit is contained in:
Ace 2020-11-12 10:22:34 +01:00
parent e866a04e0e
commit ea51185173
3 changed files with 35 additions and 33 deletions

View File

@ -13,7 +13,7 @@ import arrow
from locale import getdefaultlocale as sys_locale from locale import getdefaultlocale as sys_locale
try: try:
import pyowm from pyowm.owm import OWM
except ImportError: except ImportError:
print('pyowm is not installed! Please install with:') print('pyowm is not installed! Please install with:')
print('pip3 install pyowm') print('pip3 install pyowm')
@ -37,7 +37,9 @@ class Weather(inkycal_module):
}, },
"location": { "location": {
"label":"Please enter your location in the following format: City, Country-Code" "label":"Please enter your location in the following format: City, Country-Code. "+
"You can also enter the location ID found in the url "+
"e.g. https://openweathermap.org/city/4893171 -> ID is 4893171"
} }
} }
@ -94,19 +96,19 @@ class Weather(inkycal_module):
raise Exception('config is missing {}'.format(param)) raise Exception('config is missing {}'.format(param))
# required parameters # required parameters
self.location = config['location']
self.api_key = config['api_key'] self.api_key = config['api_key']
self.location = config['location']
# optional parameters # optional parameters
self.round_temperature = bool(config['round_temperature']) self.round_temperature = config['round_temperature']
self.round_windspeed = bool(config['round_windspeed']) self.round_windspeed = config['round_windspeed']
self.forecast_interval = config['forecast_interval'] self.forecast_interval = config['forecast_interval']
self.units = config['units'] self.units = config['units']
self.hour_format = int(config['hour_format']) self.hour_format = int(config['hour_format'])
self.use_beaufort = bool(config['use_beaufort']) self.use_beaufort = config['use_beaufort']
# additional configuration # additional configuration
self.owm = pyowm.OWM(self.api_key) self.owm = OWM(self.api_key).weather_manager()
self.timezone = get_system_tz() self.timezone = get_system_tz()
self.locale = sys_locale()[0] self.locale = sys_locale()[0]
self.weatherfont = ImageFont.truetype( self.weatherfont = ImageFont.truetype(
@ -320,8 +322,13 @@ class Weather(inkycal_module):
temp_fc4 = (col7, row3) temp_fc4 = (col7, row3)
# Create current-weather and weather-forecast objects # Create current-weather and weather-forecast objects
weather = self.owm.weather_at_place(self.config['location']).get_weather() if self.location.isdigit():
forecast = self.owm.three_hours_forecast(self.config['location']) self.location = int(self.location)
weather = self.owm.weather_at_id(self.location).weather
forecast = self.owm.forecast_at_id(self.location, '3h')
else:
weather = self.owm.weather_at_place(self.location).weather
forecast = self.owm.forecast_at_place(self.location, '3h')
# Set decimals # Set decimals
dec_temp = None if self.round_temperature == True else 1 dec_temp = None if self.round_temperature == True else 1
@ -358,9 +365,9 @@ class Weather(inkycal_module):
fc_data = {} fc_data = {}
for forecast in forecasts: for forecast in forecasts:
temp = '{}°'.format(round( temp = '{}°'.format(round(
forecast.get_temperature(unit=temp_unit)['temp'], ndigits=dec_temp)) forecast.temperature(unit=temp_unit)['temp'], ndigits=dec_temp))
icon = forecast.get_weather_icon_name() icon = forecast.weather_icon_name
fc_data['fc'+str(forecasts.index(forecast)+1)] = { fc_data['fc'+str(forecasts.index(forecast)+1)] = {
'temp':temp, 'temp':temp,
'icon':icon, 'icon':icon,
@ -370,8 +377,7 @@ class Weather(inkycal_module):
elif self.forecast_interval == 'daily': elif self.forecast_interval == 'daily':
### logger.debug("getting daily forecasts")
logger.debug("daily")
def calculate_forecast(days_from_today): def calculate_forecast(days_from_today):
@ -389,14 +395,14 @@ class Weather(inkycal_module):
forecasts = [forecast.get_weather_at(_.datetime) for _ in time_range] forecasts = [forecast.get_weather_at(_.datetime) for _ in time_range]
# Get all temperatures for this day # Get all temperatures for this day
daily_temp = [round(_.get_temperature(unit=temp_unit)['temp'], daily_temp = [round(_.temperature(unit=temp_unit)['temp'],
ndigits=dec_temp) for _ in forecasts] ndigits=dec_temp) for _ in forecasts]
# Calculate min. and max. temp for this day # Calculate min. and max. temp for this day
temp_range = '{}°/{}°'.format(max(daily_temp), min(daily_temp)) temp_range = '{}°/{}°'.format(max(daily_temp), min(daily_temp))
# Get all weather icon codes for this day # Get all weather icon codes for this day
daily_icons = [_.get_weather_icon_name() for _ in forecasts] daily_icons = [_.weather_icon_name for _ in forecasts]
# Find most common element from all weather icon codes # Find most common element from all weather icon codes
status = max(set(daily_icons), key=daily_icons.count) status = max(set(daily_icons), key=daily_icons.count)
@ -418,36 +424,32 @@ class Weather(inkycal_module):
logger.debug((key,val)) logger.debug((key,val))
# Get some current weather details # Get some current weather details
temperature = '{}°'.format(weather.get_temperature(unit=temp_unit)['temp']) temperature = '{}°'.format(weather.temperature(unit=temp_unit)['temp'])
weather_icon = weather.get_weather_icon_name() weather_icon = weather.weather_icon_name
humidity = str(weather.get_humidity()) humidity = str(weather.humidity)
windspeed = weather.get_wind(unit='meters_sec')['speed'] sunrise_raw = arrow.get(weather.sunrise_time()).to(self.timezone)
sunrise_raw = arrow.get(weather.get_sunrise_time()).to(self.timezone) sunset_raw = arrow.get(weather.sunset_time()).to(self.timezone)
sunset_raw = arrow.get(weather.get_sunset_time()).to(self.timezone)
if self.hour_format == 12: if self.hour_format == 12:
sunrise = sunrise_raw.format('h:mm a') sunrise = sunrise_raw.format('h:mm a')
sunset = sunset_raw.format('h:mm a') sunset = sunset_raw.format('h:mm a')
elif self.hour_format == 24: elif self.hour_format == 24:
sunrise = sunrise_raw.format('H:mm') sunrise = sunrise_raw.format('H:mm')
sunset = sunset_raw.format('H:mm') sunset = sunset_raw.format('H:mm')
# Format the windspeed to user preference # Format the windspeed to user preference
if self.use_beaufort == True: if self.use_beaufort == True:
windspeed_to_beaufort = [0.02, 1.5, 3.3, 5.4, 7.9, 10.7, 13.8, 17.1, logging.debug("using beaufort for wind")
20.7, 24.4, 28.4, 32.6, 100] wind = str(weather.wind(unit='beaufort')['speed'])
wind = str([windspeed_to_beaufort.index(_) for _ in windspeed_to_beaufort
if windspeed < _][0])
elif self.use_beaufort == False: elif self.use_beaufort == False:
meters_sec = round(windspeed, ndigits = dec_wind)
miles_per_hour = round(windspeed * 2.23694, ndigits = dec_wind)
if self.units == 'metric': if self.units == 'metric':
wind = str(meters_sec) + 'm/s' wind = str(weather.wind(unit='meters_sec')['speed']) + 'm/s'
elif self.units == 'imperial': elif self.units == 'imperial':
wind = str(miles_per_hour) + 'mph' wind = str(weather.wind(unit='miles_hour')['speed']) + 'miles/h'
dec = decimal.Decimal dec = decimal.Decimal
moonphase = get_moon_phase() moonphase = get_moon_phase()
@ -515,4 +517,4 @@ class Weather(inkycal_module):
return im_black, im_colour return im_black, im_colour
if __name__ == '__main__': if __name__ == '__main__':
print('running {0} in standalone mode'.format(filename)) print('running {0} in standalone mode'.format(filename))

View File

@ -1,4 +1,4 @@
pyowm==2.10.0 # weather pyowm==3.1.1 # weather
Pillow>=7.1.1 # imaging Pillow>=7.1.1 # imaging
icalendar==4.0.6 # iCalendar parsing icalendar==4.0.6 # iCalendar parsing
recurring-ical-events==0.1.17b0 # parse recurring events recurring-ical-events==0.1.17b0 # parse recurring events
@ -7,4 +7,4 @@ numpy>=1.18.2 # image pre-processing #pre-installed on Raspbia
arrow>=0.15.6 # time operations arrow>=0.15.6 # time operations
Flask==1.1.2 # webserver Flask==1.1.2 # webserver
Flask-WTF==0.14.3 # webforms Flask-WTF==0.14.3 # webforms
sphinxemoji==0.1.8 # for emoji support on documentation sphinxemoji==0.1.8 # for emoji support on documentation

View File

@ -8,7 +8,7 @@ __author__ = "aceisace"
__author_email__ = "aceisace63@yahoo.com" __author_email__ = "aceisace63@yahoo.com"
__url__ = "https://github.com/aceisace/Inky-Calendar" __url__ = "https://github.com/aceisace/Inky-Calendar"
__install_requires__ = ['pyowm==2.10.0', # weather __install_requires__ = ['pyowm==3.1.1', # weather
'Pillow>=7.1.1' , # imaging 'Pillow>=7.1.1' , # imaging
'icalendar==4.0.6', # iCalendar parsing 'icalendar==4.0.6', # iCalendar parsing
'recurring-ical-events==0.1.17b0',# parse recurring events 'recurring-ical-events==0.1.17b0',# parse recurring events