From 2985f15e7ded90ed373a5c9accba622d2f1bebf3 Mon Sep 17 00:00:00 2001 From: Adam Brakhane Date: Sun, 10 Jul 2022 15:16:42 -0600 Subject: [PATCH 1/3] display previous time if no image exists --- literary_clock.py | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/literary_clock.py b/literary_clock.py index 443eddca..0ad15c6e 100644 --- a/literary_clock.py +++ b/literary_clock.py @@ -3,6 +3,7 @@ import sys from datetime import datetime +from datetime import timedelta from glob import glob from random import randrange from PIL import Image, ImageDraw, ImageFont, ImageOps @@ -25,6 +26,25 @@ def format_weather_description(weather_description): weather_dict[2] = splits[1] if len(splits) > 1 else '' return weather_dict +def getTimeQuotes(currentTime,depth): + hour_minute = currentTime.strftime('%H%M') + + firstTry = True + if depth > 0: + firstTry = False + + quotes_path = 'images/metadata/quote_%s_*_credits.png' % hour_minute + quotes = glob(quotes_path) + if len(quotes) == 0: + # print("No quotes for "+hour_minute) + + # Call the same request, but 1 minute ago + return getTimeQuotes(currentTime-timedelta(minutes=1),depth+1) + else: + # print("Got quote for "+hour_minute) + return quotes, firstTry + + def main(): openweathermap_apikey = os.getenv("OPENWEATHERMAP_APIKEY") @@ -59,19 +79,21 @@ def main(): image.paste(iconImage, (20, 5)) now = datetime.now() - hour_minute = now.strftime('%H%M') - quotes_path = 'images/metadata/quote_%s_*_credits.png' % hour_minute - quotes = glob(quotes_path) - if len(quotes) == 0: - now_time = now.strftime('%H:%M') + quotes,firstTry=getTimeQuotes(now,0) + + # Write the quote to screen + quote = quotes[randrange(0, len(quotes))] + quoteImage = Image.open(quote).convert('1') + image.paste(quoteImage, (0, 80)) + + # If not our first try, also write the current time + # if !firstTry: + if True: + now_time = now.strftime('%I:%M %p') draw_time = ImageDraw.Draw(image) - time_font = ImageFont.truetype('Literata72pt-Regular.ttf', 144) - draw_time.text((220, 150), now_time, font=time_font, fill=0) - else: - quote = quotes[randrange(0, len(quotes))] - quoteImage = Image.open(quote).convert('1') - image.paste(quoteImage, (0, 80)) + time_font = ImageFont.truetype('Literata72pt-Regular.ttf', 30) + draw_time.text((670, 30), now_time, font=time_font, fill=0) today = now.strftime('%a, %B, %d') dayFont = ImageFont.truetype('Literata72pt-Regular.ttf', 48) From 071fc04f34420b702f1d0f120e17dec89e02fbe1 Mon Sep 17 00:00:00 2001 From: Adam Brakhane Date: Sun, 10 Jul 2022 15:33:45 -0600 Subject: [PATCH 2/3] only show the current time if the image is out of date --- literary_clock.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/literary_clock.py b/literary_clock.py index 0ad15c6e..e8bc1c31 100644 --- a/literary_clock.py +++ b/literary_clock.py @@ -81,15 +81,14 @@ def main(): now = datetime.now() quotes,firstTry=getTimeQuotes(now,0) - + # Write the quote to screen quote = quotes[randrange(0, len(quotes))] quoteImage = Image.open(quote).convert('1') image.paste(quoteImage, (0, 80)) # If not our first try, also write the current time - # if !firstTry: - if True: + if !firstTry: now_time = now.strftime('%I:%M %p') draw_time = ImageDraw.Draw(image) time_font = ImageFont.truetype('Literata72pt-Regular.ttf', 30) From fe150b9296253d795f79b2cf8d87d7fc0bd15d4b Mon Sep 17 00:00:00 2001 From: Adam Brakhane Date: Sun, 10 Jul 2022 15:36:40 -0600 Subject: [PATCH 3/3] fix python typo --- literary_clock.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/literary_clock.py b/literary_clock.py index e8bc1c31..cc8b86b0 100644 --- a/literary_clock.py +++ b/literary_clock.py @@ -88,7 +88,7 @@ def main(): image.paste(quoteImage, (0, 80)) # If not our first try, also write the current time - if !firstTry: + if not firstTry: now_time = now.strftime('%I:%M %p') draw_time = ImageDraw.Draw(image) time_font = ImageFont.truetype('Literata72pt-Regular.ttf', 30)