Update inkycal_weather.py
This commit is contained in:
parent
52b300bda2
commit
d57744adfb
@ -1,25 +1,21 @@
|
|||||||
#!/usr/bin/python3
|
#!python3
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""
|
"""
|
||||||
Weather module for Inky-Calendar software.
|
Inkycal weather module
|
||||||
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, decimal
|
import math
|
||||||
|
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')
|
|
||||||
|
|
||||||
filename = os.path.basename(__file__).split('.py')[0]
|
logger = logging.getLogger(__name__)
|
||||||
logger = logging.getLogger(filename)
|
|
||||||
|
|
||||||
class Weather(inkycal_module):
|
class Weather(inkycal_module):
|
||||||
"""Weather class
|
"""Weather class
|
||||||
@ -106,8 +102,7 @@ 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"{filename} loaded")
|
print(f"{__name__} loaded")
|
||||||
|
|
||||||
|
|
||||||
def generate_image(self):
|
def generate_image(self):
|
||||||
"""Generate image for this module"""
|
"""Generate image for this module"""
|
||||||
@ -123,11 +118,10 @@ 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() == True:
|
if internet_available():
|
||||||
logger.info('Connection test passed')
|
logger.info('Connection test passed')
|
||||||
else:
|
else:
|
||||||
logger.exception('Network could not be reached :(')
|
raise NetworkNotReachableError
|
||||||
raise
|
|
||||||
|
|
||||||
def get_moon_phase():
|
def get_moon_phase():
|
||||||
"""Calculate the current (approximate) 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',
|
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"""
|
||||||
@ -163,7 +156,6 @@ 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?
|
||||||
@ -171,21 +163,6 @@ 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
|
||||||
@ -206,7 +183,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) - (icon_size_correction[icon]*size)/2)
|
y = int((box_height / 2) - (text_height / 2))
|
||||||
|
|
||||||
# Draw the text in the text-box
|
# Draw the text in the text-box
|
||||||
draw = ImageDraw.Draw(image)
|
draw = ImageDraw.Draw(image)
|
||||||
@ -219,8 +196,6 @@ 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|
|
||||||
@ -230,7 +205,6 @@ 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
|
||||||
|
|
||||||
@ -283,7 +257,6 @@ 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)
|
||||||
@ -383,7 +356,6 @@ 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
|
||||||
@ -404,7 +376,6 @@ 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
|
||||||
@ -449,11 +420,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 == True:
|
if self.use_beaufort:
|
||||||
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'])
|
||||||
|
|
||||||
elif self.use_beaufort == False:
|
else:
|
||||||
|
|
||||||
if self.units == 'metric':
|
if self.units == 'metric':
|
||||||
logging.debug('getting windspeed in metric unit')
|
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),
|
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
|
||||||
|
|
||||||
@ -533,5 +503,6 @@ 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 {filename} in standalone mode')
|
print(f'running {__name__} in standalone mode')
|
||||||
|
Loading…
Reference in New Issue
Block a user