From 72b2e19ecf27120122d6401e62005996bb1595fb Mon Sep 17 00:00:00 2001 From: mrbwburns <> Date: Mon, 20 Nov 2023 17:29:44 +0100 Subject: [PATCH 01/15] add devcontainer config to repo --- .devcontainer/devcontainer.json | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 .devcontainer/devcontainer.json diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..094eb68 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,21 @@ +// For format details, see https://aka.ms/devcontainer.json. +{ + "name": "Python 3", + "image": "python:3.9-bullseye", + + // This is the settings.json mount + "mounts": ["source=/c/temp/settings_test.json,target=/boot/settings.json,type=bind,consistency=cached"], + + // Use 'postCreateCommand' to run commands after the container is created. + "postCreateCommand": "pip3 install --user -r requirements.txt", + + // We want to connect as root. More info: https://aka.ms/dev-containers-non-root. + "remoteUser": "root", + "customizations": { + "vscode": { + "extensions": [ + "ms-python.python" + ] + } + } +} From 563154ba358a3fb1c6a2dc8babf04b2c552163db Mon Sep 17 00:00:00 2001 From: mrbwburns <> Date: Mon, 20 Nov 2023 17:29:44 +0100 Subject: [PATCH 02/15] small devcontainer cleanup --- .devcontainer/devcontainer.json | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 094eb68..1b65fe2 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,16 +1,14 @@ // For format details, see https://aka.ms/devcontainer.json. { - "name": "Python 3", + "name": "Inkycal-dev", "image": "python:3.9-bullseye", // This is the settings.json mount "mounts": ["source=/c/temp/settings_test.json,target=/boot/settings.json,type=bind,consistency=cached"], // Use 'postCreateCommand' to run commands after the container is created. - "postCreateCommand": "pip3 install --user -r requirements.txt", + "postCreateCommand": "pip3 install --upgrade pip && pip3 install --user -r requirements.txt", - // We want to connect as root. More info: https://aka.ms/dev-containers-non-root. - "remoteUser": "root", "customizations": { "vscode": { "extensions": [ From 62bb0f600e09300fb31376f40337642d94270793 Mon Sep 17 00:00:00 2001 From: mrbwburns <> Date: Mon, 20 Nov 2023 17:29:45 +0100 Subject: [PATCH 03/15] first improvements for larger fontsizes --- inkycal/modules/inkycal_weather.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/inkycal/modules/inkycal_weather.py b/inkycal/modules/inkycal_weather.py index ec639bf..28d184a 100644 --- a/inkycal/modules/inkycal_weather.py +++ b/inkycal/modules/inkycal_weather.py @@ -243,6 +243,8 @@ class Weather(inkycal_module): # Draw the text in the text-box draw = ImageDraw.Draw(image) + print(f"Box width: {box_width}, Box height: {box_height}") + # TODO: fix ValueError: Width and height must be >= 0 space = Image.new('RGBA', (box_width, box_height)) ImageDraw.Draw(space).text((x, y), text, fill='black', font=font) @@ -272,7 +274,9 @@ class Weather(inkycal_module): else: logger.info('Please consider decreasing the height.') row_height = int((im_height * (1 - im_height / im_width)) / 3) - + + _, text_height = self.font.getsize("Text") + row_height = max(row_height, text_height) logger.debug(f"row_height: {row_height} | col_width: {col_width}") # Calculate spacings for better centering @@ -374,6 +378,7 @@ class Weather(inkycal_module): # Get current time now = arrow.utcnow() + now = now.to("local") if self.forecast_interval == 'hourly': From 7cac3fd19511af860b7c4eff1da5469efd9cec40 Mon Sep 17 00:00:00 2001 From: mrbwburns <> Date: Mon, 20 Nov 2023 17:29:45 +0100 Subject: [PATCH 04/15] - improve devcontainer setup - add openweather web scraper module --- .devcontainer/Dockerfile | 10 +++ .devcontainer/devcontainer.json | 10 ++- .devcontainer/postCreate.sh | 2 + inkycal/modules/inkycal_openweather_scrape.py | 75 +++++++++++++++++++ requirements.txt | 30 +++++++- 5 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 .devcontainer/Dockerfile create mode 100644 .devcontainer/postCreate.sh create mode 100644 inkycal/modules/inkycal_openweather_scrape.py diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..658ac30 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,10 @@ +FROM python:3.9-slim-bullseye +WORKDIR /usr/src/app +RUN apt-get -y update && apt-get install -yqq dos2unix chromium chromium-driver \ + libxi6 libgconf-2-4 python3-selenium \ + tzdata git +RUN git config --global --add safe.directory /usr/src/app +RUN python3 -m pip install --upgrade pip +RUN python3 -m pip install --user virtualenv +ENV TZ=Europe/Berlin +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 1b65fe2..121d9bc 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,18 +1,22 @@ // For format details, see https://aka.ms/devcontainer.json. { "name": "Inkycal-dev", - "image": "python:3.9-bullseye", + "build": { + "dockerfile": "Dockerfile" + }, // This is the settings.json mount "mounts": ["source=/c/temp/settings_test.json,target=/boot/settings.json,type=bind,consistency=cached"], // Use 'postCreateCommand' to run commands after the container is created. - "postCreateCommand": "pip3 install --upgrade pip && pip3 install --user -r requirements.txt", + "postCreateCommand": "chmod +x ./.devcontainer/postCreate.sh && ./.devcontainer/postCreate.sh", "customizations": { "vscode": { "extensions": [ - "ms-python.python" + "ms-python.python", + "ms-python.black-formatter", + "ms-azuretools.vscode-docker" ] } } diff --git a/.devcontainer/postCreate.sh b/.devcontainer/postCreate.sh new file mode 100644 index 0000000..6041923 --- /dev/null +++ b/.devcontainer/postCreate.sh @@ -0,0 +1,2 @@ +#!/bin/bash +source ./venv/Scripts/activate && pip3 install --upgrade pip && pip3 install --user -r requirements.txt \ No newline at end of file diff --git a/inkycal/modules/inkycal_openweather_scrape.py b/inkycal/modules/inkycal_openweather_scrape.py new file mode 100644 index 0000000..1e652fa --- /dev/null +++ b/inkycal/modules/inkycal_openweather_scrape.py @@ -0,0 +1,75 @@ +from PIL import Image +from PIL import ImageEnhance +from selenium import webdriver +from selenium.common.exceptions import ElementClickInterceptedException +from selenium.webdriver.chrome.options import Options +from selenium.webdriver.chrome.service import Service +from selenium.webdriver.common.by import By +from selenium.webdriver.support.wait import WebDriverWait + +import time + +# Set the desired viewport size (width, height) +my_width = 480 +my_height = 850 +mobile_emulation = { + "deviceMetrics": { "width": my_width, "height": my_height, "pixelRatio": 1.0 }, + "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" +} + +# Create an instance of the webdriver with some pre-configured options +options = Options() +options.add_argument("--no-sandbox") +options.add_argument("--headless=new") +#options.add_argument("--disable-gpu") +options.add_argument("--disable-dev-shm-usage") +options.add_experimental_option("mobileEmulation", mobile_emulation) +service = Service("/usr/bin/chromedriver") +driver = webdriver.Chrome(service=service, options=options) + +# Navigate to webpage +driver.get("https://openweathermap.org/city/2867714") + +# Wait and find the Cookie Button +login_button = driver.find_element(By.XPATH, '//*[@id="stick-footer-panel"]/div/div/div/div/div/button') +# Scroll to it +driver.execute_script("return arguments[0].scrollIntoView();", login_button) +# Click the button +button_clicked = False +while(button_clicked == False): + try: + login_button.click() + button_clicked = True + print("All cookies successfully accepted!") + except ElementClickInterceptedException: + print("Couldn't click the cookie button, retrying...") + time.sleep(10) + +# hacky wait statement for all the page elements to load +WebDriverWait(driver, timeout=20).until(lambda d: d.find_element(By.XPATH, '//*[@id="weather-widget"]/div[2]/div[1]/div[1]/div[1]/h2').text != "") + +# Scroll to the start of the forecast +forecast_element = driver.find_element(By.XPATH, '//*[@id="weather-widget"]/div[2]/div[1]/div[1]/div[1]') +driver.execute_script("return arguments[0].scrollIntoView();", forecast_element) + +# remove the map +map_element = driver.find_element(By.XPATH, '//*[@id="weather-widget"]/div[2]/div[1]/div[2]') +driver.execute_script("arguments[0].remove();", map_element) + +# zoom in a little - not now +#driver.execute_script("document.body.style.zoom='110%'"); + +# Save as a screenshot +image_filename = "openweather_scraped.png" +driver.save_screenshot(image_filename) + +# Close the WebDriver when done +driver.quit() + +# crop, resize, enhance & rotate the image for inky display +im = Image.open(image_filename, mode='r', formats=None) +im = im.crop((0, 50, my_width, my_height)) +#im = im.resize((800, 480), Image.Resampling.LANCZOS) +#im = im.rotate(90, Image.NEAREST, expand = 1) +im = ImageEnhance.Contrast(im).enhance(1.5) +im.save(image_filename) diff --git a/requirements.txt b/requirements.txt index be79521..a21b8be 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,30 +1,56 @@ +appdirs==1.4.4 arrow==1.2.3 +attrs==22.2.0 +beautifulsoup4==4.12.2 certifi==2023.7.22 +charset-normalizer==3.2.0 +contourpy==1.1.1 cycler==0.11.0 +distlib==0.3.7 +exceptiongroup==1.1.3 feedparser==6.0.10 +filelock==3.12.4 fonttools==4.40.0 +frozendict==2.3.8 geojson==2.3.0 +h11==0.14.0 +html5lib==1.1 icalendar==5.0.7 +idna==3.4 +importlib-resources==6.1.0 kiwisolver==1.4.4 lxml==4.9.2 matplotlib==3.7.1 multitasking==0.0.11 numpy==1.25.0 +outcome==1.2.0 packaging==23.1 pandas==2.0.2 Pillow==9.5.0 +platformdirs==3.10.0 pyowm==3.3.0 pyparsing==3.1.0 PySocks==1.7.1 python-dateutil==2.8.2 +python-dotenv==1.0.0 pytz==2023.3 recurring-ical-events==2.0.2 requests==2.31.0 +selenium==4.10.0 sgmllib3k==1.0.0 six==1.16.0 +sniffio==1.3.0 +sortedcontainers==2.4.0 +soupsieve==2.5 todoist-api-python==2.0.2 +trio==0.22.2 +trio-websocket==0.10.4 typing_extensions==4.6.3 +tzdata==2023.3 urllib3==2.0.3 +virtualenv==20.24.5 +webencodings==0.5.1 +wsproto==1.2.0 +x-wr-timezone==0.0.5 yfinance==0.2.21 -python-dotenv==1.0.0 -setuptools==68.0.0 +zipp==3.17.0 From a0fa33d5e114b0b4ccf1a1fdab4f2d216d41f12c Mon Sep 17 00:00:00 2001 From: mrbwburns <> Date: Mon, 20 Nov 2023 17:29:45 +0100 Subject: [PATCH 05/15] scraping the weather instead of using the API --- inkycal/modules/inkycal_openweather_scrape.py | 113 +++-- inkycal/modules/inkycal_weather.py | 460 +----------------- 2 files changed, 61 insertions(+), 512 deletions(-) diff --git a/inkycal/modules/inkycal_openweather_scrape.py b/inkycal/modules/inkycal_openweather_scrape.py index 1e652fa..db1b516 100644 --- a/inkycal/modules/inkycal_openweather_scrape.py +++ b/inkycal/modules/inkycal_openweather_scrape.py @@ -9,67 +9,72 @@ from selenium.webdriver.support.wait import WebDriverWait import time -# Set the desired viewport size (width, height) -my_width = 480 -my_height = 850 -mobile_emulation = { - "deviceMetrics": { "width": my_width, "height": my_height, "pixelRatio": 1.0 }, - "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" -} +def get_scraped_weatherforecast_image() -> Image: + # Set the desired viewport size (width, height) + my_width = 480 + my_height = 850 + mobile_emulation = { + "deviceMetrics": { "width": my_width, "height": my_height, "pixelRatio": 1.0 }, + "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" + } -# Create an instance of the webdriver with some pre-configured options -options = Options() -options.add_argument("--no-sandbox") -options.add_argument("--headless=new") -#options.add_argument("--disable-gpu") -options.add_argument("--disable-dev-shm-usage") -options.add_experimental_option("mobileEmulation", mobile_emulation) -service = Service("/usr/bin/chromedriver") -driver = webdriver.Chrome(service=service, options=options) + # Create an instance of the webdriver with some pre-configured options + options = Options() + options.add_argument("--no-sandbox") + options.add_argument("--headless=new") + #options.add_argument("--disable-gpu") + options.add_argument("--disable-dev-shm-usage") + options.add_experimental_option("mobileEmulation", mobile_emulation) + service = Service("/usr/bin/chromedriver") + driver = webdriver.Chrome(service=service, options=options) -# Navigate to webpage -driver.get("https://openweathermap.org/city/2867714") + # Navigate to webpage + driver.get("https://openweathermap.org/city/2867714") -# Wait and find the Cookie Button -login_button = driver.find_element(By.XPATH, '//*[@id="stick-footer-panel"]/div/div/div/div/div/button') -# Scroll to it -driver.execute_script("return arguments[0].scrollIntoView();", login_button) -# Click the button -button_clicked = False -while(button_clicked == False): - try: - login_button.click() - button_clicked = True - print("All cookies successfully accepted!") - except ElementClickInterceptedException: - print("Couldn't click the cookie button, retrying...") - time.sleep(10) + # Wait and find the Cookie Button + login_button = driver.find_element(By.XPATH, '//*[@id="stick-footer-panel"]/div/div/div/div/div/button') + # Scroll to it + driver.execute_script("return arguments[0].scrollIntoView();", login_button) + # Click the button + button_clicked = False + while(button_clicked == False): + try: + login_button.click() + button_clicked = True + print("All cookies successfully accepted!") + except ElementClickInterceptedException: + print("Couldn't click the cookie button, retrying...") + time.sleep(10) -# hacky wait statement for all the page elements to load -WebDriverWait(driver, timeout=20).until(lambda d: d.find_element(By.XPATH, '//*[@id="weather-widget"]/div[2]/div[1]/div[1]/div[1]/h2').text != "") + # hacky wait statement for all the page elements to load + WebDriverWait(driver, timeout=20).until(lambda d: d.find_element(By.XPATH, '//*[@id="weather-widget"]/div[2]/div[1]/div[1]/div[1]/h2').text != "") -# Scroll to the start of the forecast -forecast_element = driver.find_element(By.XPATH, '//*[@id="weather-widget"]/div[2]/div[1]/div[1]/div[1]') -driver.execute_script("return arguments[0].scrollIntoView();", forecast_element) + # Scroll to the start of the forecast + forecast_element = driver.find_element(By.XPATH, '//*[@id="weather-widget"]/div[2]/div[1]/div[1]/div[1]') + driver.execute_script("return arguments[0].scrollIntoView();", forecast_element) -# remove the map -map_element = driver.find_element(By.XPATH, '//*[@id="weather-widget"]/div[2]/div[1]/div[2]') -driver.execute_script("arguments[0].remove();", map_element) + # remove the map + map_element = driver.find_element(By.XPATH, '//*[@id="weather-widget"]/div[2]/div[1]/div[2]') + driver.execute_script("arguments[0].remove();", map_element) -# zoom in a little - not now -#driver.execute_script("document.body.style.zoom='110%'"); + # zoom in a little - not now + #driver.execute_script("document.body.style.zoom='110%'"); -# Save as a screenshot -image_filename = "openweather_scraped.png" -driver.save_screenshot(image_filename) + html_element = driver.find_element(By.TAG_NAME, "html") + driver.execute_script("arguments[0].style.fontSize = '16px';", html_element) -# Close the WebDriver when done -driver.quit() + # Save as a screenshot + image_filename = "openweather_scraped.png" + driver.save_screenshot(image_filename) -# crop, resize, enhance & rotate the image for inky display -im = Image.open(image_filename, mode='r', formats=None) -im = im.crop((0, 50, my_width, my_height)) -#im = im.resize((800, 480), Image.Resampling.LANCZOS) -#im = im.rotate(90, Image.NEAREST, expand = 1) -im = ImageEnhance.Contrast(im).enhance(1.5) -im.save(image_filename) + # Close the WebDriver when done + driver.quit() + + # crop, resize, enhance & rotate the image for inky display + im = Image.open(image_filename, mode='r', formats=None) + im = im.crop((0, 50, my_width, my_height)) + #im = im.resize((800, 480), Image.Resampling.LANCZOS) + #im = im.rotate(90, Image.NEAREST, expand = 1) + im = ImageEnhance.Contrast(im).enhance(1.3) + im.save(image_filename) + return im, im diff --git a/inkycal/modules/inkycal_weather.py b/inkycal/modules/inkycal_weather.py index 28d184a..2325f46 100644 --- a/inkycal/modules/inkycal_weather.py +++ b/inkycal/modules/inkycal_weather.py @@ -7,12 +7,10 @@ Copyright by aceinnolab from inkycal.modules.template import inkycal_module from inkycal.custom import * +from inkycal.modules.inkycal_openweather_scrape import get_scraped_weatherforecast_image import math import decimal -import arrow - -from pyowm.owm import OWM logger = logging.getLogger(__name__) @@ -107,462 +105,8 @@ class Weather(inkycal_module): def generate_image(self): """Generate image for this module""" - # Define new image size with respect to padding - im_width = int(self.width - (2 * self.padding_left)) - im_height = int(self.height - (2 * self.padding_top)) - im_size = im_width, im_height - logger.info(f'Image size: {im_size}') - - # Create an image for black pixels and one for coloured pixels - im_black = Image.new('RGB', size=im_size, color='white') - im_colour = Image.new('RGB', size=im_size, color='white') - - # Check if internet is available - if internet_available(): - logger.info('Connection test passed') - else: - raise NetworkNotReachableError - - def get_moon_phase(): - """Calculate the current (approximate) moon phase""" - - dec = decimal.Decimal - diff = now - arrow.get(2001, 1, 1) - days = dec(diff.days) + (dec(diff.seconds) / dec(86400)) - lunations = dec("0.20439731") + (days * dec("0.03386319269")) - position = lunations % dec(1) - index = math.floor((position * dec(8)) + dec("0.5")) - 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""" - answer = False - - if temp_unit == 'celsius' and round(float(temp.split('°')[0])) <= 0: - answer = True - elif temp_unit == 'fahrenheit' and round(float(temp.split('°')[0])) <= 0: - answer = True - return answer - - # Lookup-table for weather icons and weather codes - weathericons = { - '01d': '\uf00d', - '02d': '\uf002', - '03d': '\uf013', - '04d': '\uf012', - '09d': '\uf01a', - '10d': '\uf019', - '11d': '\uf01e', - '13d': '\uf01b', - '50d': '\uf014', - '01n': '\uf02e', - '02n': '\uf013', - '03n': '\uf013', - '04n': '\uf013', - '09n': '\uf037', - '10n': '\uf036', - '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? - xy = xy-coordinates as tuple -> (x,y) - 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 - font = self.weatherfont - - # Increase fontsize to fit specified height and width of text box - size = 8 - font = ImageFont.truetype(font.path, size) - text_width, text_height = font.getsize(text) - - while (text_width < int(box_width * 0.9) and - text_height < int(box_height * 0.9)): - size += 1 - font = ImageFont.truetype(font.path, size) - text_width, text_height = font.getsize(text) - - text_width, text_height = font.getsize(text) - - # 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) - - # Draw the text in the text-box - draw = ImageDraw.Draw(image) - print(f"Box width: {box_width}, Box height: {box_height}") - # TODO: fix ValueError: Width and height must be >= 0 - space = Image.new('RGBA', (box_width, box_height)) - ImageDraw.Draw(space).text((x, y), text, fill='black', font=font) - - if rotation != None: - space.rotate(rotation, expand=True) - - # 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| - # | current |----------|----------|----------|----------|----------|----------| - # | weather | humidity | sunrise | icon1 | icon2 | icon3 | icon4 | - # | icon |----------|----------|----------|----------|----------|----------| - # | | windspeed| sunset | temperat.| temperat.| temperat.| temperat.| - # |----------|----------|----------|----------|----------|----------|----------| - - # Calculate size rows and columns - col_width = im_width // 7 - - # Ratio width height - image_ratio = im_width / im_height - - if image_ratio >= 4: - row_height = im_height // 3 - else: - logger.info('Please consider decreasing the height.') - row_height = int((im_height * (1 - im_height / im_width)) / 3) - - _, text_height = self.font.getsize("Text") - row_height = max(row_height, text_height) - logger.debug(f"row_height: {row_height} | col_width: {col_width}") - - # Calculate spacings for better centering - spacing_top = int((im_width % col_width) / 2) - spacing_left = int((im_height % row_height) / 2) - - # Define sizes for weather icons - icon_small = int(col_width / 3) - icon_medium = icon_small * 2 - icon_large = icon_small * 3 - - # Calculate the x-axis position of each col - col1 = spacing_top - col2 = col1 + col_width - col3 = col2 + col_width - col4 = col3 + col_width - col5 = col4 + col_width - col6 = col5 + col_width - col7 = col6 + col_width - - # Calculate the y-axis position of each row - line_gap = int((im_height - spacing_top - 3 * row_height) // 4) - - row1 = line_gap - row2 = row1 + line_gap + row_height - row3 = row2 + line_gap + row_height - - # Draw lines on each row and border - ############################################################################ - ## draw = ImageDraw.Draw(im_black) - ## draw.line((0, 0, im_width, 0), fill='red') - ## draw.line((0, im_height-1, im_width, im_height-1), fill='red') - ## draw.line((0, row1, im_width, row1), fill='black') - ## draw.line((0, row1+row_height, im_width, row1+row_height), fill='black') - ## draw.line((0, row2, im_width, row2), fill='black') - ## draw.line((0, row2+row_height, im_width, row2+row_height), fill='black') - ## draw.line((0, row3, im_width, row3), fill='black') - ## 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) - temperature_pos = (col2 + icon_small, row1) - humidity_icon_pos = (col2, row2) - humidity_pos = (col2 + icon_small, row2) - windspeed_icon_pos = (col2, row3) - windspeed_pos = (col2 + icon_small, row3) - - # Positions for sunrise, sunset, moonphase - moonphase_pos = (col3, row1) - sunrise_icon_pos = (col3, row2) - sunrise_time_pos = (col3 + icon_small, row2) - sunset_icon_pos = (col3, row3) - sunset_time_pos = (col3 + icon_small, row3) - - # Positions for forecast 1 - stamp_fc1 = (col4, row1) - icon_fc1 = (col4, row1 + row_height) - temp_fc1 = (col4, row3) - - # Positions for forecast 2 - stamp_fc2 = (col5, row1) - icon_fc2 = (col5, row1 + row_height) - temp_fc2 = (col5, row3) - - # Positions for forecast 3 - stamp_fc3 = (col6, row1) - icon_fc3 = (col6, row1 + row_height) - temp_fc3 = (col6, row3) - - # Positions for forecast 4 - stamp_fc4 = (col7, row1) - icon_fc4 = (col7, row1 + row_height) - temp_fc4 = (col7, row3) - - # Create current-weather and weather-forecast objects - if self.location.isdigit(): - logging.debug('looking up location by ID') - weather = self.owm.weather_at_id(int(self.location)).weather - forecast = self.owm.forecast_at_id(int(self.location), '3h') - else: - logging.debug('looking up location by string') - weather = self.owm.weather_at_place(self.location).weather - forecast = self.owm.forecast_at_place(self.location, '3h') - - # Set decimals - dec_temp = None if self.round_temperature == True else 1 - dec_wind = None if self.round_windspeed == True else 1 - - # Set correct temperature units - if self.units == 'metric': - temp_unit = 'celsius' - elif self.units == 'imperial': - temp_unit = 'fahrenheit' - - logging.debug(f'temperature unit: {temp_unit}') - logging.debug(f'decimals temperature: {dec_temp} | decimals wind: {dec_wind}') - - # Get current time - now = arrow.utcnow() - now = now.to("local") - - if self.forecast_interval == 'hourly': - - logger.debug("getting hourly forecasts") - - # Forecasts are provided for every 3rd full hour - # find out how many hours there are until the next 3rd full hour - if (now.hour % 3) != 0: - hour_gap = 3 - (now.hour % 3) - else: - hour_gap = 3 - - # Create timings for hourly forcasts - forecast_timings = [now.shift(hours=+ hour_gap + _).floor('hour') - for _ in range(0, 12, 3)] - - # Create forecast objects for given timings - forecasts = [forecast.get_weather_at(forecast_time.datetime) for - forecast_time in forecast_timings] - - # Add forecast-data to fc_data dictionary - fc_data = {} - for forecast in forecasts: - temp = '{}°'.format(round( - forecast.temperature(unit=temp_unit)['temp'], ndigits=dec_temp)) - - icon = forecast.weather_icon_name - fc_data['fc' + str(forecasts.index(forecast) + 1)] = { - 'temp': temp, - 'icon': icon, - 'stamp': forecast_timings[forecasts.index(forecast)].to( - get_system_tz()).format('H.00' if self.hour_format == 24 else 'h a') - } - - elif self.forecast_interval == 'daily': - - 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 - """ - - # Create a list containing time-objects for every 3rd hour of the day - time_range = list(arrow.Arrow.range('hour', - now.shift(days=days_from_today).floor('day'), - now.shift(days=days_from_today).ceil('day') - ))[::3] - - # Get forecasts for each time-object - forecasts = [forecast.get_weather_at(_.datetime) for _ in time_range] - - # Get all temperatures for this day - daily_temp = [round(_.temperature(unit=temp_unit)['temp'], - ndigits=dec_temp) for _ in forecasts] - # 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 - status = max(set(daily_icons), key=daily_icons.count) - - weekday = now.shift(days=days_from_today).format('ddd', locale= - self.locale) - return {'temp': temp_range, 'icon': status, 'stamp': weekday} - - forecasts = [calculate_forecast(days) for days in range(1, 5)] - - fc_data = {} - for forecast in forecasts: - fc_data['fc' + str(forecasts.index(forecast) + 1)] = { - 'temp': forecast['temp'], - 'icon': forecast['icon'], - 'stamp': forecast['stamp'] - } - - for key, val in fc_data.items(): - logger.debug((key, val)) - - # Get some current weather details - temperature = '{}°'.format(round( - weather.temperature(unit=temp_unit)['temp'], ndigits=dec_temp)) - - weather_icon = weather.weather_icon_name - humidity = str(weather.humidity) - sunrise_raw = arrow.get(weather.sunrise_time()).to(self.timezone) - sunset_raw = arrow.get(weather.sunset_time()).to(self.timezone) - - logger.debug(f'weather_icon: {weather_icon}') - - if self.hour_format == 12: - logger.debug('using 12 hour format for sunrise/sunset') - sunrise = sunrise_raw.format('h:mm a') - sunset = sunset_raw.format('h:mm a') - - elif self.hour_format == 24: - logger.debug('using 24 hour format for sunrise/sunset') - sunrise = sunrise_raw.format('H:mm') - sunset = sunset_raw.format('H:mm') - - # Format the windspeed to user preference - if self.use_beaufort: - logger.debug("using beaufort for wind") - wind = str(weather.wind(unit='beaufort')['speed']) - - else: - - if self.units == 'metric': - logging.debug('getting windspeed in metric unit') - wind = str(weather.wind(unit='meters_sec')['speed']) + 'm/s' - - elif self.units == 'imperial': - logging.debug('getting windspeed in imperial unit') - wind = str(weather.wind(unit='miles_hour')['speed']) + 'miles/h' - - dec = decimal.Decimal - moonphase = get_moon_phase() - - # Fill weather details in col 1 (current weather icon) - draw_icon(im_colour, weather_icon_pos, (col_width, im_height), - weathericons[weather_icon]) - - # Fill weather details in col 2 (temp, humidity, wind) - draw_icon(im_colour, temperature_icon_pos, (icon_small, row_height), - '\uf053') - - if is_negative(temperature): - write(im_black, temperature_pos, (col_width - icon_small, row_height), - temperature, font=self.font) - else: - write(im_black, temperature_pos, (col_width - icon_small, row_height), - temperature, font=self.font) - - draw_icon(im_colour, humidity_icon_pos, (icon_small, row_height), - '\uf07a') - - write(im_black, humidity_pos, (col_width - icon_small, row_height), - humidity + '%', font=self.font) - - draw_icon(im_colour, windspeed_icon_pos, (icon_small, icon_small), - '\uf050') - - write(im_black, windspeed_pos, (col_width - icon_small, row_height), - wind, font=self.font) - - # Fill weather details in col 3 (moonphase, sunrise, sunset) - draw_icon(im_colour, moonphase_pos, (col_width, row_height), moonphase) - - draw_icon(im_colour, sunrise_icon_pos, (icon_small, icon_small), '\uf051') - write(im_black, sunrise_time_pos, (col_width - icon_small, row_height), - sunrise, font=self.font) - - draw_icon(im_colour, sunset_icon_pos, (icon_small, icon_small), '\uf052') - write(im_black, sunset_time_pos, (col_width - icon_small, row_height), sunset, - font=self.font) - - # Add the forecast data to the correct places - for pos in range(1, len(fc_data) + 1): - stamp = fc_data[f'fc{pos}']['stamp'] - - icon = weathericons[fc_data[f'fc{pos}']['icon']] - temp = fc_data[f'fc{pos}']['temp'] - - write(im_black, eval(f'stamp_fc{pos}'), (col_width, row_height), - stamp, font=self.font) - draw_icon(im_colour, eval(f'icon_fc{pos}'), (col_width, row_height + line_gap * 2), - icon) - 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 - - # Add borders around each sub-section - draw_border(im_black, (col1, row1), (col_width * 3 - 3, border_h), - shrinkage=(0, 0)) - - for _ in range(4, 8): - draw_border(im_black, (eval(f'col{_}'), row1), (border_w, border_h), - shrinkage=(0, 0)) - # return the images ready for the display - return im_black, im_colour + return get_scraped_weatherforecast_image() if __name__ == '__main__': From 6287affb0c4f702e6972f01491d6012ffe5aeeb3 Mon Sep 17 00:00:00 2001 From: mrbwburns <> Date: Mon, 20 Nov 2023 17:29:45 +0100 Subject: [PATCH 06/15] call the openweather scraper from the image module --- inkycal/modules/inky_image.py | 2 +- inkycal/modules/inkycal_image.py | 4 ++++ inkycal/modules/inkycal_openweather_scrape.py | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/inkycal/modules/inky_image.py b/inkycal/modules/inky_image.py index e069114..56ef31a 100755 --- a/inkycal/modules/inky_image.py +++ b/inkycal/modules/inky_image.py @@ -55,7 +55,7 @@ class Inkyimage: image = Image.open(path) except FileNotFoundError: logger.error('No image file found', exc_info=True) - raise Exception('Your file could not be found. Please check the filepath') + raise Exception(f'Your file could not be found. Please check the filepath: {path}') except OSError: logger.error('Invalid Image file provided', exc_info=True) diff --git a/inkycal/modules/inkycal_image.py b/inkycal/modules/inkycal_image.py index 37b4a1a..dcd4b8d 100755 --- a/inkycal/modules/inkycal_image.py +++ b/inkycal/modules/inkycal_image.py @@ -6,6 +6,7 @@ Copyright by aceinnolab """ from inkycal.modules.template import inkycal_module +from inkycal.modules.inkycal_openweather_scrape import get_scraped_weatherforecast_image from inkycal.custom import * from inkycal.modules.inky_image import Inkyimage as Images @@ -68,6 +69,9 @@ class Inkyimage(inkycal_module): print(f'{__name__} loaded') def generate_image(self): + """Call the openweather scraper""" + _,_ = get_scraped_weatherforecast_image() + """Generate image for this module""" # Define new image size with respect to padding diff --git a/inkycal/modules/inkycal_openweather_scrape.py b/inkycal/modules/inkycal_openweather_scrape.py index db1b516..2cc6b18 100644 --- a/inkycal/modules/inkycal_openweather_scrape.py +++ b/inkycal/modules/inkycal_openweather_scrape.py @@ -78,3 +78,10 @@ def get_scraped_weatherforecast_image() -> Image: im = ImageEnhance.Contrast(im).enhance(1.3) im.save(image_filename) return im, im + +def main(): + _, _ = get_scraped_weatherforecast_image() + +if __name__ == '__main__': + print(f'running {__name__} in standalone/debug mode') + main() \ No newline at end of file From ba630c381f1802730cb94eec5b31fa5b9ac19d2c Mon Sep 17 00:00:00 2001 From: mrbwburns <> Date: Mon, 20 Nov 2023 17:29:45 +0100 Subject: [PATCH 07/15] remove image "optimization" --- inkycal/main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inkycal/main.py b/inkycal/main.py index 122a817..153d745 100644 --- a/inkycal/main.py +++ b/inkycal/main.py @@ -518,12 +518,12 @@ class Inkycal: def _optimize_im(image, threshold=220): """Optimize the image for rendering on ePaper displays""" - buffer = numpy.array(image.convert('RGB')) - red, green = buffer[:, :, 0], buffer[:, :, 1] + #buffer = numpy.array(image.convert('RGB')) + #red, green = buffer[:, :, 0], buffer[:, :, 1] # grey->black - buffer[numpy.logical_and(red <= threshold, green <= threshold)] = [0, 0, 0] - image = Image.fromarray(buffer) + #buffer[numpy.logical_and(red <= threshold, green <= threshold)] = [0, 0, 0] + #image = Image.fromarray(buffer) return image def calibrate(self): From 5b16fff0bd46f2f1d22b98e2356e55bef50e54e8 Mon Sep 17 00:00:00 2001 From: mrbwburns <> Date: Mon, 20 Nov 2023 17:29:46 +0100 Subject: [PATCH 08/15] scraper shellscript + dockerfile improvements --- .devcontainer/Dockerfile | 11 +++++++++-- .devcontainer/devcontainer.json | 3 ++- run_weather_scraper.sh | 3 +++ 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100755 run_weather_scraper.sh diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 658ac30..36562fb 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,5 +1,5 @@ -FROM python:3.9-slim-bullseye -WORKDIR /usr/src/app +FROM python:3.9-slim-bullseye as development +WORKDIR /app RUN apt-get -y update && apt-get install -yqq dos2unix chromium chromium-driver \ libxi6 libgconf-2-4 python3-selenium \ tzdata git @@ -8,3 +8,10 @@ RUN python3 -m pip install --upgrade pip RUN python3 -m pip install --user virtualenv ENV TZ=Europe/Berlin RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone + +FROM development +COPY ../inkycal/modules/inkycal_openweather_scrape.py /app/ +COPY ./run_weather_scraper.sh /app/ + +# Set the entrypoint to the shell script +ENTRYPOINT ["run_weather_scraper.sh"] \ No newline at end of file diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 121d9bc..a05ecbe 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -2,7 +2,8 @@ { "name": "Inkycal-dev", "build": { - "dockerfile": "Dockerfile" + "dockerfile": "Dockerfile", + "target": "development" }, // This is the settings.json mount diff --git a/run_weather_scraper.sh b/run_weather_scraper.sh new file mode 100755 index 0000000..8391faf --- /dev/null +++ b/run_weather_scraper.sh @@ -0,0 +1,3 @@ +#!/bin/sh +python3 /home/ubuntu/Inkycal/inkycal/modules/inkycal_openweather_scrape.py +scp ./openweather_scraped.png inky@10.10.9.10:~/Inkycal/ From 0ca272b563b909a079aa75cfa9bcc1b9273252de Mon Sep 17 00:00:00 2001 From: mrbwburns <> Date: Mon, 20 Nov 2023 17:29:46 +0100 Subject: [PATCH 09/15] weather scraper improvements --- inkycal/main.py | 8 ++++---- inkycal/modules/inkycal_openweather_scrape.py | 6 +++--- run_weather_scraper.sh | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/inkycal/main.py b/inkycal/main.py index 122a817..153d745 100644 --- a/inkycal/main.py +++ b/inkycal/main.py @@ -518,12 +518,12 @@ class Inkycal: def _optimize_im(image, threshold=220): """Optimize the image for rendering on ePaper displays""" - buffer = numpy.array(image.convert('RGB')) - red, green = buffer[:, :, 0], buffer[:, :, 1] + #buffer = numpy.array(image.convert('RGB')) + #red, green = buffer[:, :, 0], buffer[:, :, 1] # grey->black - buffer[numpy.logical_and(red <= threshold, green <= threshold)] = [0, 0, 0] - image = Image.fromarray(buffer) + #buffer[numpy.logical_and(red <= threshold, green <= threshold)] = [0, 0, 0] + #image = Image.fromarray(buffer) return image def calibrate(self): diff --git a/inkycal/modules/inkycal_openweather_scrape.py b/inkycal/modules/inkycal_openweather_scrape.py index 1e345d4..ef56b72 100644 --- a/inkycal/modules/inkycal_openweather_scrape.py +++ b/inkycal/modules/inkycal_openweather_scrape.py @@ -64,7 +64,7 @@ def get_scraped_weatherforecast_image() -> Image: driver.execute_script("arguments[0].style.fontSize = '16px';", html_element) # Save as a screenshot - image_filename = "openweather_scraped.png" + image_filename = "/tmp/openweather_scraped.png" driver.save_screenshot(image_filename) # Close the WebDriver when done @@ -75,7 +75,7 @@ def get_scraped_weatherforecast_image() -> Image: im = im.crop((0, 50, my_width, my_height)) #im = im.resize((800, 480), Image.Resampling.LANCZOS) #im = im.rotate(90, Image.NEAREST, expand = 1) - #im = ImageEnhance.Contrast(im).enhance(1.3) + im = ImageEnhance.Contrast(im).enhance(1.3) im.save(image_filename) return im, im @@ -84,4 +84,4 @@ def main(): if __name__ == '__main__': print(f'running {__name__} in standalone/debug mode') - main() \ No newline at end of file + main() diff --git a/run_weather_scraper.sh b/run_weather_scraper.sh index 8391faf..84480ed 100755 --- a/run_weather_scraper.sh +++ b/run_weather_scraper.sh @@ -1,3 +1,3 @@ #!/bin/sh python3 /home/ubuntu/Inkycal/inkycal/modules/inkycal_openweather_scrape.py -scp ./openweather_scraped.png inky@10.10.9.10:~/Inkycal/ +scp -i /home/ubuntu/.ssh/id_rsa /tmp/openweather_scraped.png inky@10.10.9.10:~/Inkycal/ From 485228e35d22c70a56d47ee2b4463428f8db1197 Mon Sep 17 00:00:00 2001 From: mrbwburns <> Date: Mon, 20 Nov 2023 17:29:46 +0100 Subject: [PATCH 10/15] another shot for improved image display --- inkycal/main.py | 8 ++++---- inkycal/modules/inkycal_openweather_scrape.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/inkycal/main.py b/inkycal/main.py index 153d745..122a817 100644 --- a/inkycal/main.py +++ b/inkycal/main.py @@ -518,12 +518,12 @@ class Inkycal: def _optimize_im(image, threshold=220): """Optimize the image for rendering on ePaper displays""" - #buffer = numpy.array(image.convert('RGB')) - #red, green = buffer[:, :, 0], buffer[:, :, 1] + buffer = numpy.array(image.convert('RGB')) + red, green = buffer[:, :, 0], buffer[:, :, 1] # grey->black - #buffer[numpy.logical_and(red <= threshold, green <= threshold)] = [0, 0, 0] - #image = Image.fromarray(buffer) + buffer[numpy.logical_and(red <= threshold, green <= threshold)] = [0, 0, 0] + image = Image.fromarray(buffer) return image def calibrate(self): diff --git a/inkycal/modules/inkycal_openweather_scrape.py b/inkycal/modules/inkycal_openweather_scrape.py index 2cc6b18..1e345d4 100644 --- a/inkycal/modules/inkycal_openweather_scrape.py +++ b/inkycal/modules/inkycal_openweather_scrape.py @@ -75,7 +75,7 @@ def get_scraped_weatherforecast_image() -> Image: im = im.crop((0, 50, my_width, my_height)) #im = im.resize((800, 480), Image.Resampling.LANCZOS) #im = im.rotate(90, Image.NEAREST, expand = 1) - im = ImageEnhance.Contrast(im).enhance(1.3) + #im = ImageEnhance.Contrast(im).enhance(1.3) im.save(image_filename) return im, im From 2f494eab9e39fed933965d9b8d5d425275027335 Mon Sep 17 00:00:00 2001 From: mrbwburns <> Date: Mon, 20 Nov 2023 17:29:46 +0100 Subject: [PATCH 11/15] revert original weather module back to master --- inkycal/modules/inkycal_weather.py | 455 ++++++++++++++++++++++++++++- 1 file changed, 453 insertions(+), 2 deletions(-) diff --git a/inkycal/modules/inkycal_weather.py b/inkycal/modules/inkycal_weather.py index 2325f46..ec639bf 100644 --- a/inkycal/modules/inkycal_weather.py +++ b/inkycal/modules/inkycal_weather.py @@ -7,10 +7,12 @@ Copyright by aceinnolab from inkycal.modules.template import inkycal_module from inkycal.custom import * -from inkycal.modules.inkycal_openweather_scrape import get_scraped_weatherforecast_image import math import decimal +import arrow + +from pyowm.owm import OWM logger = logging.getLogger(__name__) @@ -105,8 +107,457 @@ class Weather(inkycal_module): def generate_image(self): """Generate image for this module""" + # Define new image size with respect to padding + im_width = int(self.width - (2 * self.padding_left)) + im_height = int(self.height - (2 * self.padding_top)) + im_size = im_width, im_height + logger.info(f'Image size: {im_size}') + + # Create an image for black pixels and one for coloured pixels + im_black = Image.new('RGB', size=im_size, color='white') + im_colour = Image.new('RGB', size=im_size, color='white') + + # Check if internet is available + if internet_available(): + logger.info('Connection test passed') + else: + raise NetworkNotReachableError + + def get_moon_phase(): + """Calculate the current (approximate) moon phase""" + + dec = decimal.Decimal + diff = now - arrow.get(2001, 1, 1) + days = dec(diff.days) + (dec(diff.seconds) / dec(86400)) + lunations = dec("0.20439731") + (days * dec("0.03386319269")) + position = lunations % dec(1) + index = math.floor((position * dec(8)) + dec("0.5")) + 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""" + answer = False + + if temp_unit == 'celsius' and round(float(temp.split('°')[0])) <= 0: + answer = True + elif temp_unit == 'fahrenheit' and round(float(temp.split('°')[0])) <= 0: + answer = True + return answer + + # Lookup-table for weather icons and weather codes + weathericons = { + '01d': '\uf00d', + '02d': '\uf002', + '03d': '\uf013', + '04d': '\uf012', + '09d': '\uf01a', + '10d': '\uf019', + '11d': '\uf01e', + '13d': '\uf01b', + '50d': '\uf014', + '01n': '\uf02e', + '02n': '\uf013', + '03n': '\uf013', + '04n': '\uf013', + '09n': '\uf037', + '10n': '\uf036', + '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? + xy = xy-coordinates as tuple -> (x,y) + 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 + font = self.weatherfont + + # Increase fontsize to fit specified height and width of text box + size = 8 + font = ImageFont.truetype(font.path, size) + text_width, text_height = font.getsize(text) + + while (text_width < int(box_width * 0.9) and + text_height < int(box_height * 0.9)): + size += 1 + font = ImageFont.truetype(font.path, size) + text_width, text_height = font.getsize(text) + + text_width, text_height = font.getsize(text) + + # 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) + + # Draw the text in the text-box + draw = ImageDraw.Draw(image) + space = Image.new('RGBA', (box_width, box_height)) + ImageDraw.Draw(space).text((x, y), text, fill='black', font=font) + + if rotation != None: + space.rotate(rotation, expand=True) + + # 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| + # | current |----------|----------|----------|----------|----------|----------| + # | weather | humidity | sunrise | icon1 | icon2 | icon3 | icon4 | + # | icon |----------|----------|----------|----------|----------|----------| + # | | windspeed| sunset | temperat.| temperat.| temperat.| temperat.| + # |----------|----------|----------|----------|----------|----------|----------| + + # Calculate size rows and columns + col_width = im_width // 7 + + # Ratio width height + image_ratio = im_width / im_height + + if image_ratio >= 4: + row_height = im_height // 3 + else: + logger.info('Please consider decreasing the height.') + row_height = int((im_height * (1 - im_height / im_width)) / 3) + + logger.debug(f"row_height: {row_height} | col_width: {col_width}") + + # Calculate spacings for better centering + spacing_top = int((im_width % col_width) / 2) + spacing_left = int((im_height % row_height) / 2) + + # Define sizes for weather icons + icon_small = int(col_width / 3) + icon_medium = icon_small * 2 + icon_large = icon_small * 3 + + # Calculate the x-axis position of each col + col1 = spacing_top + col2 = col1 + col_width + col3 = col2 + col_width + col4 = col3 + col_width + col5 = col4 + col_width + col6 = col5 + col_width + col7 = col6 + col_width + + # Calculate the y-axis position of each row + line_gap = int((im_height - spacing_top - 3 * row_height) // 4) + + row1 = line_gap + row2 = row1 + line_gap + row_height + row3 = row2 + line_gap + row_height + + # Draw lines on each row and border + ############################################################################ + ## draw = ImageDraw.Draw(im_black) + ## draw.line((0, 0, im_width, 0), fill='red') + ## draw.line((0, im_height-1, im_width, im_height-1), fill='red') + ## draw.line((0, row1, im_width, row1), fill='black') + ## draw.line((0, row1+row_height, im_width, row1+row_height), fill='black') + ## draw.line((0, row2, im_width, row2), fill='black') + ## draw.line((0, row2+row_height, im_width, row2+row_height), fill='black') + ## draw.line((0, row3, im_width, row3), fill='black') + ## 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) + temperature_pos = (col2 + icon_small, row1) + humidity_icon_pos = (col2, row2) + humidity_pos = (col2 + icon_small, row2) + windspeed_icon_pos = (col2, row3) + windspeed_pos = (col2 + icon_small, row3) + + # Positions for sunrise, sunset, moonphase + moonphase_pos = (col3, row1) + sunrise_icon_pos = (col3, row2) + sunrise_time_pos = (col3 + icon_small, row2) + sunset_icon_pos = (col3, row3) + sunset_time_pos = (col3 + icon_small, row3) + + # Positions for forecast 1 + stamp_fc1 = (col4, row1) + icon_fc1 = (col4, row1 + row_height) + temp_fc1 = (col4, row3) + + # Positions for forecast 2 + stamp_fc2 = (col5, row1) + icon_fc2 = (col5, row1 + row_height) + temp_fc2 = (col5, row3) + + # Positions for forecast 3 + stamp_fc3 = (col6, row1) + icon_fc3 = (col6, row1 + row_height) + temp_fc3 = (col6, row3) + + # Positions for forecast 4 + stamp_fc4 = (col7, row1) + icon_fc4 = (col7, row1 + row_height) + temp_fc4 = (col7, row3) + + # Create current-weather and weather-forecast objects + if self.location.isdigit(): + logging.debug('looking up location by ID') + weather = self.owm.weather_at_id(int(self.location)).weather + forecast = self.owm.forecast_at_id(int(self.location), '3h') + else: + logging.debug('looking up location by string') + weather = self.owm.weather_at_place(self.location).weather + forecast = self.owm.forecast_at_place(self.location, '3h') + + # Set decimals + dec_temp = None if self.round_temperature == True else 1 + dec_wind = None if self.round_windspeed == True else 1 + + # Set correct temperature units + if self.units == 'metric': + temp_unit = 'celsius' + elif self.units == 'imperial': + temp_unit = 'fahrenheit' + + logging.debug(f'temperature unit: {temp_unit}') + logging.debug(f'decimals temperature: {dec_temp} | decimals wind: {dec_wind}') + + # Get current time + now = arrow.utcnow() + + if self.forecast_interval == 'hourly': + + logger.debug("getting hourly forecasts") + + # Forecasts are provided for every 3rd full hour + # find out how many hours there are until the next 3rd full hour + if (now.hour % 3) != 0: + hour_gap = 3 - (now.hour % 3) + else: + hour_gap = 3 + + # Create timings for hourly forcasts + forecast_timings = [now.shift(hours=+ hour_gap + _).floor('hour') + for _ in range(0, 12, 3)] + + # Create forecast objects for given timings + forecasts = [forecast.get_weather_at(forecast_time.datetime) for + forecast_time in forecast_timings] + + # Add forecast-data to fc_data dictionary + fc_data = {} + for forecast in forecasts: + temp = '{}°'.format(round( + forecast.temperature(unit=temp_unit)['temp'], ndigits=dec_temp)) + + icon = forecast.weather_icon_name + fc_data['fc' + str(forecasts.index(forecast) + 1)] = { + 'temp': temp, + 'icon': icon, + 'stamp': forecast_timings[forecasts.index(forecast)].to( + get_system_tz()).format('H.00' if self.hour_format == 24 else 'h a') + } + + elif self.forecast_interval == 'daily': + + 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 + """ + + # Create a list containing time-objects for every 3rd hour of the day + time_range = list(arrow.Arrow.range('hour', + now.shift(days=days_from_today).floor('day'), + now.shift(days=days_from_today).ceil('day') + ))[::3] + + # Get forecasts for each time-object + forecasts = [forecast.get_weather_at(_.datetime) for _ in time_range] + + # Get all temperatures for this day + daily_temp = [round(_.temperature(unit=temp_unit)['temp'], + ndigits=dec_temp) for _ in forecasts] + # 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 + status = max(set(daily_icons), key=daily_icons.count) + + weekday = now.shift(days=days_from_today).format('ddd', locale= + self.locale) + return {'temp': temp_range, 'icon': status, 'stamp': weekday} + + forecasts = [calculate_forecast(days) for days in range(1, 5)] + + fc_data = {} + for forecast in forecasts: + fc_data['fc' + str(forecasts.index(forecast) + 1)] = { + 'temp': forecast['temp'], + 'icon': forecast['icon'], + 'stamp': forecast['stamp'] + } + + for key, val in fc_data.items(): + logger.debug((key, val)) + + # Get some current weather details + temperature = '{}°'.format(round( + weather.temperature(unit=temp_unit)['temp'], ndigits=dec_temp)) + + weather_icon = weather.weather_icon_name + humidity = str(weather.humidity) + sunrise_raw = arrow.get(weather.sunrise_time()).to(self.timezone) + sunset_raw = arrow.get(weather.sunset_time()).to(self.timezone) + + logger.debug(f'weather_icon: {weather_icon}') + + if self.hour_format == 12: + logger.debug('using 12 hour format for sunrise/sunset') + sunrise = sunrise_raw.format('h:mm a') + sunset = sunset_raw.format('h:mm a') + + elif self.hour_format == 24: + logger.debug('using 24 hour format for sunrise/sunset') + sunrise = sunrise_raw.format('H:mm') + sunset = sunset_raw.format('H:mm') + + # Format the windspeed to user preference + if self.use_beaufort: + logger.debug("using beaufort for wind") + wind = str(weather.wind(unit='beaufort')['speed']) + + else: + + if self.units == 'metric': + logging.debug('getting windspeed in metric unit') + wind = str(weather.wind(unit='meters_sec')['speed']) + 'm/s' + + elif self.units == 'imperial': + logging.debug('getting windspeed in imperial unit') + wind = str(weather.wind(unit='miles_hour')['speed']) + 'miles/h' + + dec = decimal.Decimal + moonphase = get_moon_phase() + + # Fill weather details in col 1 (current weather icon) + draw_icon(im_colour, weather_icon_pos, (col_width, im_height), + weathericons[weather_icon]) + + # Fill weather details in col 2 (temp, humidity, wind) + draw_icon(im_colour, temperature_icon_pos, (icon_small, row_height), + '\uf053') + + if is_negative(temperature): + write(im_black, temperature_pos, (col_width - icon_small, row_height), + temperature, font=self.font) + else: + write(im_black, temperature_pos, (col_width - icon_small, row_height), + temperature, font=self.font) + + draw_icon(im_colour, humidity_icon_pos, (icon_small, row_height), + '\uf07a') + + write(im_black, humidity_pos, (col_width - icon_small, row_height), + humidity + '%', font=self.font) + + draw_icon(im_colour, windspeed_icon_pos, (icon_small, icon_small), + '\uf050') + + write(im_black, windspeed_pos, (col_width - icon_small, row_height), + wind, font=self.font) + + # Fill weather details in col 3 (moonphase, sunrise, sunset) + draw_icon(im_colour, moonphase_pos, (col_width, row_height), moonphase) + + draw_icon(im_colour, sunrise_icon_pos, (icon_small, icon_small), '\uf051') + write(im_black, sunrise_time_pos, (col_width - icon_small, row_height), + sunrise, font=self.font) + + draw_icon(im_colour, sunset_icon_pos, (icon_small, icon_small), '\uf052') + write(im_black, sunset_time_pos, (col_width - icon_small, row_height), sunset, + font=self.font) + + # Add the forecast data to the correct places + for pos in range(1, len(fc_data) + 1): + stamp = fc_data[f'fc{pos}']['stamp'] + + icon = weathericons[fc_data[f'fc{pos}']['icon']] + temp = fc_data[f'fc{pos}']['temp'] + + write(im_black, eval(f'stamp_fc{pos}'), (col_width, row_height), + stamp, font=self.font) + draw_icon(im_colour, eval(f'icon_fc{pos}'), (col_width, row_height + line_gap * 2), + icon) + 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 + + # Add borders around each sub-section + draw_border(im_black, (col1, row1), (col_width * 3 - 3, border_h), + shrinkage=(0, 0)) + + for _ in range(4, 8): + draw_border(im_black, (eval(f'col{_}'), row1), (border_w, border_h), + shrinkage=(0, 0)) + # return the images ready for the display - return get_scraped_weatherforecast_image() + return im_black, im_colour if __name__ == '__main__': From 0d8cb6c42a09a4624dc8f8efdb4ac0add2f074a0 Mon Sep 17 00:00:00 2001 From: mrbwburns <> Date: Mon, 20 Nov 2023 17:29:46 +0100 Subject: [PATCH 12/15] no image processing on main remove the weather scraper call from the inky image module --- inkycal/main.py | 8 ++++---- inkycal/modules/inkycal_image.py | 4 ---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/inkycal/main.py b/inkycal/main.py index 122a817..153d745 100644 --- a/inkycal/main.py +++ b/inkycal/main.py @@ -518,12 +518,12 @@ class Inkycal: def _optimize_im(image, threshold=220): """Optimize the image for rendering on ePaper displays""" - buffer = numpy.array(image.convert('RGB')) - red, green = buffer[:, :, 0], buffer[:, :, 1] + #buffer = numpy.array(image.convert('RGB')) + #red, green = buffer[:, :, 0], buffer[:, :, 1] # grey->black - buffer[numpy.logical_and(red <= threshold, green <= threshold)] = [0, 0, 0] - image = Image.fromarray(buffer) + #buffer[numpy.logical_and(red <= threshold, green <= threshold)] = [0, 0, 0] + #image = Image.fromarray(buffer) return image def calibrate(self): diff --git a/inkycal/modules/inkycal_image.py b/inkycal/modules/inkycal_image.py index dcd4b8d..37b4a1a 100755 --- a/inkycal/modules/inkycal_image.py +++ b/inkycal/modules/inkycal_image.py @@ -6,7 +6,6 @@ Copyright by aceinnolab """ from inkycal.modules.template import inkycal_module -from inkycal.modules.inkycal_openweather_scrape import get_scraped_weatherforecast_image from inkycal.custom import * from inkycal.modules.inky_image import Inkyimage as Images @@ -69,9 +68,6 @@ class Inkyimage(inkycal_module): print(f'{__name__} loaded') def generate_image(self): - """Call the openweather scraper""" - _,_ = get_scraped_weatherforecast_image() - """Generate image for this module""" # Define new image size with respect to padding From f146949249aad1e66c7c46a6252db3caf2013f36 Mon Sep 17 00:00:00 2001 From: mrbwburns <> Date: Mon, 20 Nov 2023 17:29:46 +0100 Subject: [PATCH 13/15] latest best guess at scraping the weather --- inkycal/modules/inkycal_openweather_scrape.py | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/inkycal/modules/inkycal_openweather_scrape.py b/inkycal/modules/inkycal_openweather_scrape.py index ef56b72..42185d7 100644 --- a/inkycal/modules/inkycal_openweather_scrape.py +++ b/inkycal/modules/inkycal_openweather_scrape.py @@ -1,12 +1,13 @@ from PIL import Image from PIL import ImageEnhance from selenium import webdriver -from selenium.common.exceptions import ElementClickInterceptedException +from selenium.common.exceptions import ElementClickInterceptedException, TimeoutException, NoSuchElementException from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait +import sys import time def get_scraped_weatherforecast_image() -> Image: @@ -47,7 +48,14 @@ def get_scraped_weatherforecast_image() -> Image: time.sleep(10) # hacky wait statement for all the page elements to load - WebDriverWait(driver, timeout=20).until(lambda d: d.find_element(By.XPATH, '//*[@id="weather-widget"]/div[2]/div[1]/div[1]/div[1]/h2').text != "") + page_loaded = False + while(page_loaded == False): + try: + WebDriverWait(driver, timeout=45).until(lambda d: d.find_element(By.XPATH, '//*[@id="weather-widget"]/div[2]/div[1]/div[1]/div[1]/h2').text != "") + page_loaded = True + except TimeoutException: + print("Couldn't get the page to load, retrying...") + time.sleep(60) # Scroll to the start of the forecast forecast_element = driver.find_element(By.XPATH, '//*[@id="weather-widget"]/div[2]/div[1]/div[1]/div[1]') @@ -57,8 +65,12 @@ def get_scraped_weatherforecast_image() -> Image: map_element = driver.find_element(By.XPATH, '//*[@id="weather-widget"]/div[2]/div[1]/div[2]') driver.execute_script("arguments[0].remove();", map_element) + # remove the hourly forecast + #map_element = driver.find_element(By.XPATH, '//*[@id="weather-widget"]/div[2]/div[2]/div[1]') + #driver.execute_script("arguments[0].remove();", map_element) + # zoom in a little - not now - #driver.execute_script("document.body.style.zoom='110%'"); + driver.execute_script("document.body.style.zoom='110%'"); html_element = driver.find_element(By.TAG_NAME, "html") driver.execute_script("arguments[0].style.fontSize = '16px';", html_element) @@ -72,10 +84,10 @@ def get_scraped_weatherforecast_image() -> Image: # crop, resize, enhance & rotate the image for inky display im = Image.open(image_filename, mode='r', formats=None) - im = im.crop((0, 50, my_width, my_height)) - #im = im.resize((800, 480), Image.Resampling.LANCZOS) + im = im.crop((0, 100, (my_width-50), (my_height-50))) + im = im.resize((480, 800), resample=Image.LANCZOS) #im = im.rotate(90, Image.NEAREST, expand = 1) - im = ImageEnhance.Contrast(im).enhance(1.3) + im = ImageEnhance.Contrast(im).enhance(1.0) im.save(image_filename) return im, im @@ -83,5 +95,6 @@ def main(): _, _ = get_scraped_weatherforecast_image() if __name__ == '__main__': - print(f'running {__name__} in standalone/debug mode') + now = time.asctime() + print(f"It's {now} - running {__name__} in standalone/debug mode") main() From b885105147079a2f36b0c286d464ff6ca8f8f120 Mon Sep 17 00:00:00 2001 From: mrbwburns <> Date: Sun, 26 Nov 2023 20:29:02 +0100 Subject: [PATCH 14/15] bump devcontainer to bookworm, add documentation --- .devcontainer/Dockerfile | 2 +- .devcontainer/devcontainer.json | 11 +- README.md | 290 +----------------- inkycal/modules/inkycal_openweather_scrape.py | 40 ++- run_weather_scraper.sh | 2 +- 5 files changed, 37 insertions(+), 308 deletions(-) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 36562fb..7143502 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.9-slim-bullseye as development +FROM python:3.11-slim-bookworm as development WORKDIR /app RUN apt-get -y update && apt-get install -yqq dos2unix chromium chromium-driver \ libxi6 libgconf-2-4 python3-selenium \ diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 03b1ec4..1bf5540 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,17 +1,16 @@ // For format details, see https://aka.ms/devcontainer.json. { "name": "Inkycal-dev", - "build": { - "dockerfile": "Dockerfile", - "target": "development" - }, - + "build": { + "dockerfile": "Dockerfile", + "target": "development" + }, // This is the settings.json mount "mounts": ["source=/c/temp/settings_test.json,target=/boot/settings.json,type=bind,consistency=cached"], // Use 'postCreateCommand' to run commands after the container is created. - "postCreateCommand": "chmod +x ./.devcontainer/postCreate.sh && ./.devcontainer/postCreate.sh", + "postCreateCommand": "dos2unix ./.devcontainer/postCreate.sh && chmod +x ./.devcontainer/postCreate.sh && ./.devcontainer/postCreate.sh", "customizations": { "vscode": { diff --git a/README.md b/README.md index 4608ede..8a482a2 100644 --- a/README.md +++ b/README.md @@ -1,287 +1,9 @@ -# Welcome to inkycal v2.0.3! +# Inkycal-based full screen weather display (scraper solution) -
-
-
-
-