Created
March 14, 2018 22:17
-
-
Save willfurnell/bacd103c01878e3787fc9d0e5f4787d3 to your computer and use it in GitHub Desktop.
WIP e-ink calendar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import epd4in2 | |
from icalendar import * | |
from datetime import * | |
import datetime as dtt | |
import urllib.request | |
import pytz | |
from PIL import Image, ImageDraw, ImageFont | |
def ensure_timezone(data): | |
lon = pytz.timezone('Europe/London') | |
try: | |
if data.get('dtstart').dt.tzinfo is None: | |
data.get('dtstart').dt = lon.localize(data.get('dtstart').dt) | |
except AttributeError: | |
data.get('dtstart').dt = datetime.combine(data.get('dtstart').dt, datetime.min.time()) | |
data.get('dtstart').dt = lon.localize(data.get('dtstart').dt) | |
return data | |
f = urllib.request.urlopen('REDACTED-KENT-CALENDAR') | |
cal1 = f.read() | |
f2 = urllib.request.urlopen('REDACTED-PERSONAL-CALENDAR') | |
cal2 = f2.read() | |
cal = Calendar.from_ical(cal1 + cal2, multiple=True) | |
components = cal[0].walk() + cal[1].walk() | |
components = filter(lambda c: c.name=='VEVENT', components) | |
components = map(ensure_timezone, components) | |
now = datetime.now(timezone.utc) | |
components = filter(lambda c: c.get('dtstart').dt - now > timedelta(0) and c.get('dtstart').dt - now < timedelta(days=1), components) # filter out past and future events | |
components = sorted(components, key=lambda c: c.get('dtstart').dt) | |
# Now we want to make an image of this | |
epd = epd4in2.EPD() | |
epd.init() | |
EPD_WIDTH = 400 | |
EPD_HEIGHT = 300 | |
image = Image.new('1', (EPD_WIDTH, EPD_HEIGHT), 1) | |
draw = ImageDraw.Draw(image) | |
fnt = ImageFont.truetype('arial.ttf', 12) | |
draw.rectangle((0, 0, 400, 5), fill=0) | |
draw.text((170, 5), "Will's Calendar", fill=0, font=fnt) | |
draw.rectangle((0, 17, 400, 20), fill=0) | |
counter = 22 | |
for comp in components[:4]: | |
draw.text((0, counter), comp.get('SUMMARY'), fill=0, font=fnt) | |
draw.text((0, counter+12), str(comp.decoded('DTSTART')), fill=0, font=fnt) | |
draw.text((0, counter+22), str(comp.decoded('DTEND')), fill=0, font=fnt) | |
draw.rectangle((0, counter+40, 400, counter+45), fill=0) | |
counter += 45 | |
draw.text((170, counter), "Unread", fill=0, font=fnt) | |
draw.rectangle((0, counter + 12, 400, counter + 16), fill=0) | |
#i = Image.open('dickbutt.bmp') | |
#image.paste(i, (0,400)) | |
epd.display_frame(epd.get_frame_buffer(image)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment