from PIL import Image, ImageDraw, ImageFont
from waveshare_epd import epd2in15g
from datetime import datetime, timezone
from tzlocal import get_localzone
import pytz
import textwrap
import pycountry
import random
import time
def display_text(text):
# Initialize the E-Paper display
epd = epd2in15g.EPD()
epd.init()
epd.Clear()
# Create a new image with white background
image = Image.new('1', (epd.height, epd.width), 255)
draw = ImageDraw.Draw(image)
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
# Load a font (you can use any TTF font you have)
font, lines_with_positions = fit_text_to_width_and_wrap_center(draw, text, epd.height, epd.width, font_path, 24)
# Add some text to the image
for x, y, line in lines_with_positions:
draw.text((x, y), line, font=font, fill=0)
# Rotate image
rotated_image = image.rotate(270, expand=True)
# Display the image on the E-Paper
epd.display(epd.getbuffer(rotated_image))
time.sleep(2)
# Clean up
epd.sleep()
def fit_text_to_width_and_wrap_center(draw, text, max_width, max_height, font_path, initial_size):
size = initial_size
while size > 6:
font = ImageFont.truetype(font_path, size)
# Estimate average character width to wrap the text
avg_char_width = sum(font.getlength(c) for c in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") / 52
max_chars_per_line = max(1, int(max_width / avg_char_width))
wrapped_lines = textwrap.wrap(text, width=max_chars_per_line)
line_height = font.getmetrics()[0] + font.getmetrics()[1] # ascent + descent
total_text_height = len(wrapped_lines) * line_height
if all(draw.textlength(line, font=font) <= max_width for line in wrapped_lines) and total_text_height <= max_height:
start_y = max((max_height - total_text_height) // 2, 0)
line_positions = []
y = start_y
for line in wrapped_lines:
line_width = draw.textlength(line, font=font)
x = (max_width - line_width) // 2
line_positions.append((x, y, line))
y += line_height
return font, line_positions
size -= 1
# Fallback tiny font
font = ImageFont.truetype(font_path, 6)
wrapped_lines = textwrap.wrap(text, width=40)
line_height = font.getmetrics()[0] + font.getmetrics()[1]
total_text_height = len(wrapped_lines) * line_height
start_y = max((max_height - total_text_height) // 2, 0)
line_positions = []
y = start_y
for line in wrapped_lines:
x = (max_width - draw.textlength(line, font=font)) // 2
line_positions.append((x, y, line))
y += line_height
return font, line_positions
def country_flag(country_code):
if not country_code or len(country_code) != 2:
return ''
try:
return ''.join(chr(127397 + ord(char)) for char in country_code.upper())
except Exception:
return ''
target_hour = 17
now_utc = datetime.now(timezone.utc)
timezone_to_country = {
tz: country_code
for country_code, timezones in pytz.country_timezones.items()
for tz in timezones
}
results = []
phrases = [
"It's 5 o'clock in {} so technically, you're right on time.",
"Somewhere in the world it's 5PM and that place is {} . Bottoms up!",
"Time is an illusion. In {}, it's 5PM drink accordingly.",
"Theyre already two drinks in over in {} catch up!",
"Its 5PM in {} which means your cocktail has moral support.",
"Your justification for happy hour? It's 5 o'clock in {}.",
"Global time zones exist *specifically* to enable this: its 5PM in {}.",
"Somewhere its 5PM. Right now, that somewhere is {}. Youre good.",
"It's 5PM in {} and that's all the permission you need.",
"If anyone questions your drink, just say Hey, its 5PM in {}.",
"Because 5PM in {} > waiting for 5PM here.",
"In {}, its 5PM and theyre toasting to your good decisions.",
"Five oclock in {} your excuse just clocked in.",
"It's 5PM in {}. Lets pretend were there, emotionally.",
"Thanks to {}, your drink is now just *good global citizenship*.",
"5PM in {} means it's happy hour somewhere specifically, right here.",
"Don't blame yourself. Blame {} they made it 5PM.",
"In {}, they call this 'responsible hydration.' Who are we to argue?",
"Global problems need global solutions like 5PM in {}.",
"The Earth spins for a reason: so it can always be 5PM *somewhere*. Right now, that somewhere is {}.",
"Cheers! It's 5 o'clock in {}",
"It's 5PM in {} time to unwind!",
"Raise a glass for {} it's 5PM there!",
"It's 5 somewhere specifically in {}",
"It's 5PM in {} bottoms up!",
"Its 5PM in {} be a good influence and join in."
]
local_phrases = [
"It's 5PM somewhere... and it's happening right here!",
"Guess what time it is? It's 5PM in your neck of the woods!",
"No need to check the clock, just check the flag. It's 5PM, baby!",
"5PM here, and the flags waving for you to join the party!",
"It's 5PM in this corner of the world go ahead, youve earned it!",
"Cheers to 5PM! You dont need to know where, just drink!",
"Your justification for happy hour: 5PM somewhere, and that somewheres waving its flag!",
"Here's to 5PM and the flag that gives you permission!",
"5PM isnt just a time; its a reason to raise a glass... wherever this flags from!",
"No time zones, just happy hour. Let's go!",
"In case you needed an excuse, 5PMs here thanks to this flag!",
"Someones already started their happy hour... and its this flags turn now!",
"When its 5PM, you know its time for a drink just look at the flag!",
"5PM. Flag up. Drink up. Lets go!",
"Its 5PM... and the flags telling you to live your best life!"
]
def get_casual_message(location_display):
return random.choice(phrases).format(location_display)
local_time = datetime.now().astimezone()
is_local_5pm = local_time.hour == target_hour
local_tz = get_localzone()
local_tz_name = str(local_tz)
local_country_code = timezone_to_country.get(local_tz_name)
if local_country_code:
try:
local_country = pycountry.countries.get(alpha_2=local_country_code)
local_country_name = local_country.name if local_country else 'Your Place'
except LookupError:
local_country_name = 'Your Place'
else:
local_country_name = 'Your Place'
local_flag = country_flag(local_country_code) if local_country_code else ''
for tz_name in pytz.all_timezones:
tz = pytz.timezone(tz_name)
tz_now = datetime.now(tz)
if tz_now.hour == target_hour:
country_code = timezone_to_country.get(tz_name)
location = tz_name.split('/')[-1].replace('_', ' ')
if country_code:
country = pycountry.countries.get(alpha_2=country_code)
country_name = country.name if country else 'Unknown'
if location.lower() == country_name.lower():
display = get_casual_message(location)
else:
display = get_casual_message(f"{location} ({country_name})")
results.append(display)
# Pick a random one to display
if results:
if is_local_5pm:
message = random.choice(local_phrases)
else:
message = random.choice(results)
print(message)
display_text(message)
Comments