Add files via upload

This commit is contained in:
ch3lmi 2023-02-10 14:50:16 +01:00 committed by GitHub
parent 415705d555
commit 52b300bda2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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