Update inkycal_weather.py

This commit is contained in:
ch3lmi 2023-02-13 09:06:38 +01:00 committed by GitHub
parent 52b300bda2
commit d57744adfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,25 +1,21 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#!python3
"""
Weather module for Inky-Calendar software.
Inkycal weather module
Copyright by aceisace
"""
from inkycal.modules.template import inkycal_module
from inkycal.custom import *
import math, decimal
import math
import decimal
import arrow
from locale import getdefaultlocale as sys_locale
try:
from pyowm.owm import OWM
except ImportError:
print('pyowm is not installed! Please install with:')
print('pip3 install pyowm')
filename = os.path.basename(__file__).split('.py')[0]
logger = logging.getLogger(filename)
logger = logging.getLogger(__name__)
class Weather(inkycal_module):
"""Weather class
@ -106,8 +102,7 @@ class Weather(inkycal_module):
fonts['weathericons-regular-webfont'], size=self.fontsize)
# give an OK message
print(f"{filename} loaded")
print(f"{__name__} loaded")
def generate_image(self):
"""Generate image for this module"""
@ -123,11 +118,10 @@ class Weather(inkycal_module):
im_colour = Image.new('RGB', size=im_size, color='white')
# Check if internet is available
if internet_available() == True:
if internet_available():
logger.info('Connection test passed')
else:
logger.exception('Network could not be reached :(')
raise
raise NetworkNotReachableError
def get_moon_phase():
"""Calculate the current (approximate) moon phase"""
@ -141,7 +135,6 @@ class Weather(inkycal_module):
return {0: '\uf095', 1: '\uf099', 2: '\uf09c', 3: '\uf0a0',
4: '\uf0a3', 5: '\uf0a7', 6: '\uf0aa', 7: '\uf0ae'}[int(index) & 7]
def is_negative(temp):
"""Check if temp is below freezing point of water (0°C/30°F)
returns True if temp below freezing point, else False"""
@ -163,7 +156,6 @@ class Weather(inkycal_module):
'11n': '\uf03b', '13n': '\uf038', '50n': '\uf023'
}
def draw_icon(image, xy, box_size, icon, rotation=None):
"""Custom function to add icons of weather font on image
image = on which image should the text be added?
@ -171,21 +163,6 @@ class Weather(inkycal_module):
box_size = size of text-box -> (width,height)
icon = icon-unicode, looks this up in weathericons dictionary
"""
icon_size_correction = {
'\uf00d': 10/60, '\uf02e': 51/150, '\uf019': 21/60,
'\uf01b': 21/60, '\uf0b5': 51/150, '\uf050': 25/60,
'\uf013': 51/150, '\uf002': 0, '\uf031': 29/100,
'\uf015': 21/60, '\uf01e': 52/150, '\uf056': 51/150,
'\uf053': 14/150, '\uf012': 51/150, '\uf01a': 51/150,
'\uf014': 51/150, '\uf037': 42/150, '\uf036': 42/150,
'\uf03b': 42/150, '\uf038': 42/150, '\uf023': 35/150,
'\uf07a': 35/150, '\uf051': 18/150, '\uf052': 18/150,
'\uf0aa': 0, '\uf095': 0, '\uf099': 0, '\uf09c': 0,
'\uf0a0': 0, '\uf0a3': 0, '\uf0a7': 0, '\uf0aa': 0,
'\uf0ae': 0
}
x, y = xy
box_width, box_height = box_size
text = icon
@ -206,7 +183,7 @@ class Weather(inkycal_module):
# Align text to desired position
x = int((box_width / 2) - (text_width / 2))
y = int((box_height / 2) - (text_height / 2) - (icon_size_correction[icon]*size)/2)
y = int((box_height / 2) - (text_height / 2))
# Draw the text in the text-box
draw = ImageDraw.Draw(image)
@ -219,8 +196,6 @@ class Weather(inkycal_module):
# Update only region with text (add text with transparent background)
image.paste(space, xy, space)
# column1 column2 column3 column4 column5 column6 column7
# |----------|----------|----------|----------|----------|----------|----------|
# | time | temperat.| moonphase| forecast1| forecast2| forecast3| forecast4|
@ -230,7 +205,6 @@ class Weather(inkycal_module):
# | | windspeed| sunset | temperat.| temperat.| temperat.| temperat.|
# |----------|----------|----------|----------|----------|----------|----------|
# Calculate size rows and columns
col_width = im_width // 7
@ -283,7 +257,6 @@ class Weather(inkycal_module):
## draw.line((0, row3+row_height, im_width, row3+row_height), fill='black')
############################################################################
# Positions for current weather details
weather_icon_pos = (col1, 0)
temperature_icon_pos = (col2, row1)
@ -383,7 +356,6 @@ class Weather(inkycal_module):
logger.debug("getting daily forecasts")
def calculate_forecast(days_from_today):
"""Get temperature range and most frequent icon code for forecast
days_from_today should be int from 1-4: e.g. 2 -> 2 days from today
@ -404,7 +376,6 @@ class Weather(inkycal_module):
# Calculate min. and max. temp for this day
temp_range = f'{max(daily_temp)}°/{min(daily_temp)}°'
# Get all weather icon codes for this day
daily_icons = [_.weather_icon_name for _ in forecasts]
# Find most common element from all weather icon codes
@ -449,11 +420,11 @@ class Weather(inkycal_module):
sunset = sunset_raw.format('H:mm')
# Format the windspeed to user preference
if self.use_beaufort == True:
if self.use_beaufort:
logger.debug("using beaufort for wind")
wind = str(weather.wind(unit='beaufort')['speed'])
elif self.use_beaufort == False:
else:
if self.units == 'metric':
logging.debug('getting windspeed in metric unit')
@ -518,7 +489,6 @@ class Weather(inkycal_module):
write(im_black, eval(f'temp_fc{pos}'), (col_width, row_height),
temp, font=self.font)
border_h = row3 + row_height
border_w = col_width - 3 # leave 3 pixels gap
@ -533,5 +503,6 @@ class Weather(inkycal_module):
# return the images ready for the display
return im_black, im_colour
if __name__ == '__main__':
print(f'running {filename} in standalone mode')
print(f'running {__name__} in standalone mode')