-
Notifications
You must be signed in to change notification settings - Fork 0
/
WotD.py
109 lines (94 loc) · 4.61 KB
/
WotD.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import epd2in13_V4 as display #For 2.13inch screen.
from PIL import Image, ImageDraw, ImageFont
import time
import datetime
import textwrap
from bs4 import BeautifulSoup
import requests
epd = display.EPD() # get the display
epd.init() # initialize the display
#print("Clear...") # prints to console, not the display, for debugging
#epd.Clear() # clear the display
def get_wotd():
url='https://www.dictionary.com/e/word-of-the-day/'
try:
r=requests.get(url)
soup=BeautifulSoup(r.content,'html.parser')
wotd=soup.find(class_="wotd-item").get_text().split("\n")
wotd=[item for item in wotd if item!=""]
wotd=[wotd[i] for i in (0,1,2,7,8)]
wotd[0] = ', '.join(wotd[0].split(', ')[1:]) #Remove day.
date_obj = datetime.datetime.strptime(wotd[0], "%B %d, %Y") # Convert the string to a datetime object
wotd[0] = date_obj.strftime("%d.%m.%y") # Format the datetime object to "DD.MM.YY" format
wotd[1] = wotd[1].strip().title() #clean big space and capitalize first letter.
wotd[2] = wotd[2].lstrip() #Get rid of loeading long blank space.
return wotd
except requests.exceptions.RequestException as e:
raise Exception(e)
def wotd_to_display(wotd):
date, word, phonetic, word_type, definition = wotd
word_type = "("+word_type+")"
epd_w, epd_h = display.EPD_HEIGHT, display.EPD_WIDTH #reverse for lanscape orientation.
HBlackImage = Image.new('1', (epd_w, epd_h), 255) #255(white)
draw = ImageDraw.Draw(HBlackImage) # Create draw object and pass in the image layer.
font_date = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf', 14)
font_word = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf', 20)
font_def = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf', 12)
draw.text((2, 2), "--Word of the Day-- " + date, font = font_date, fill = 0) #draw date
draw.text((2,42), word_type+" "+phonetic, font=font_def, fill=0) #draw word type and phonetic.
draw.text((5, 20), word, font = font_word, fill = 0) #draw word
definition = textwrap.wrap(definition,width=36) #draw meaning
y=0
for def_line in definition:
draw.text((5, 60+y), def_line, font = font_def, fill = 0)
y+=11
HBlackImage = HBlackImage.rotate(180, expand=1)
epd.display(epd.getbuffer(HBlackImage))
def display_error(e):
print("Showing error message on display", e) # prints to console, not the display, for debugging
epd.Clear() # clear the display
epd_w, epd_h = display.EPD_HEIGHT, display.EPD_WIDTH #reverse for lanscape orientation.
HBlackImage = Image.new('1', (epd_w, epd_h), 255) #255(white)
draw = ImageDraw.Draw(HBlackImage) # Create draw object and pass in the image layer.
font_e = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf', 15)
err_lines = textwrap.wrap(str(e)+", try again in 60s.",width=32) #draw meaning
y=0
for err_line in err_lines:
draw.text((5, 10+y), err_line, font = font_e, fill = 0)
y+=15
HBlackImage = HBlackImage.rotate(180, expand=1)
epd.display(epd.getbuffer(HBlackImage))
def check_and_call():
#Call for first time after reboot.
print("Clear...") # prints to console, not the display, for debugging
epd.Clear() # clear the display
wotd = get_wotd()
wotd_to_display(wotd)
# Get the current time
current_time = datetime.datetime.now()
print("Function called on:", current_time)
# Define the specific time of the new day when you want to call the function
specific_time = datetime.time(8, 0, 0) # Adjust this to your desired time (hour, minute, second)
# Check if the current time is equal to or after the specific time
if current_time.time() >= specific_time:
# Wait until the next day
tomorrow = datetime.date.today() + datetime.timedelta(days=1)
next_day_time = datetime.datetime.combine(tomorrow, specific_time)
time_difference = (next_day_time - current_time)
print("Sleep until ", next_day_time)
time.sleep(time_difference.total_seconds())
else:
# Wait until specific refresh time.
today = datetime.date.today()
next_refresh = datetime.datetime.combine(today, specific_time)
time_difference = (next_refresh - current_time)
print("Sleep until ", next_refresh)
time.sleep(time_difference.total_seconds())
# Run the loop continuously
while True:
try:
check_and_call()
except Exception as e:
display_error(e)
print("Try again in the next 60s.")
time.sleep(60)