forked from stenjo/Middager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middag.py
executable file
·112 lines (95 loc) · 4 KB
/
middag.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
110
111
112
from __future__ import print_function
import datetime
from re import T
import time
import pickle
import os.path
import sys, getopt
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from luma.led_matrix.device import max7219
from luma.core.interface.serial import spi, noop
from luma.core.render import canvas
from luma.core.virtual import viewport
from luma.core.legacy import text, show_message
from luma.core.legacy.font import proportional, CP437_FONT, TINY_FONT, SINCLAIR_FONT, LCD_FONT
from backports.datetime_fromisoformat import MonkeyPatch
MonkeyPatch.patch_fromisoformat()
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
def dayText(event):
weekday = ['Mandag', 'Tirsdag', 'Onsdag', 'Torsdag', 'Fredag','Lørdag','Søndag']
text = ''
dt = datetime.datetime.fromisoformat(event['start'].get('dateTime', event['start'].get('date')))
today = datetime.datetime.today()
tomorrow = today + datetime.timedelta(1)
if dt.date() == today.date() :
text = text + 'I dag: '
elif dt.date() == tomorrow.date() :
text = text + 'I morgen: '
else :
text = text + weekday[dt.weekday()] + ': '
text = text + event['summary']
if dt.hour > 0:
text = text + dt.strftime(' kl. %H:%M')
return text
def main(argv):
loop = None
try:
opts, args = getopt.getopt(argv,"hm:",["help ","mode ="])
except getopt.GetoptError:
print('middag.py -m <loop>')
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--help'):
print('middag.py -m <loop>')
sys.exit()
elif opt in ("-m", "--mode"):
if arg in ('loop'):
loop = True
print (loop)
"""Shows basic usage of the Google Calendar API.
Prints the start and name of the next 10 events on the user's calendar.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'/home/pi/.creds.json', SCOPES)
creds = flow.run_local_server()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('calendar', 'v3', credentials=creds)
# create matrix device
serial = spi(port=0, device=0, gpio=noop())
device = max7219(serial, cascaded=8, block_orientation=90, rotate=0)
print("Created device")
start = time.perf_counter()
while (start + 550) > time.perf_counter() or loop:
# Call the Calendar API
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
# print('Getting the upcoming 10 events')
events_result = service.events().list(calendarId='parterapeutene.no_e1or90m2lp6p523ma7u15v2pc0@group.calendar.google.com', timeMin=now,
maxResults=7, singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])
msg = ''
if not events:
msg = '- finner ingen middager - '
for event in events:
msg = dayText(event)
show_message(device, msg, fill="white", font=proportional(CP437_FONT))
time.sleep(2)
if __name__ == '__main__':
main(sys.argv[1:])