forked from mendhak/waveshare-epaper-display
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen-calendar-get.py
executable file
·130 lines (96 loc) · 4.21 KB
/
screen-calendar-get.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import datetime
import html
import time
import pickle
import os.path
import os
import logging
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.discovery import build
from utility import is_stale, update_svg
logging.basicConfig(level=logging.INFO)
# note: increasing this will require updates to the SVG template to accommodate more events
max_event_results = 4
google_calendar_id=os.getenv("GOOGLE_CALENDAR_ID","primary")
ttl = float(os.getenv("CALENDAR_TTL", 1 * 60 * 60))
def get_credentials():
google_token_pickle = 'token.pickle'
google_credentials_json = 'credentials.json'
google_api_scopes = ['https://www.googleapis.com/auth/calendar.readonly']
credentials = 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(google_token_pickle):
with open(google_token_pickle, 'rb') as token:
credentials = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not credentials or not credentials.valid:
if credentials and credentials.expired and credentials.refresh_token:
credentials.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
google_credentials_json, google_api_scopes)
credentials = flow.run_local_server()
# Save the credentials for the next run
with open(google_token_pickle, 'wb') as token:
pickle.dump(credentials, token)
return credentials
def get_events(max_event_results):
google_calendar_pickle = 'calendar.pickle'
service = build('calendar', 'v3', credentials=get_credentials(), cache_discovery=False)
events_result = None
if is_stale(os.getcwd() + "/" + google_calendar_pickle, ttl):
logging.debug("Pickle is stale, calling the Calendar API")
# Call the Calendar API
events_result = service.events().list(
calendarId=google_calendar_id,
timeMin=datetime.datetime.utcnow().isoformat() + 'Z',
maxResults=max_event_results,
singleEvents=True,
orderBy='startTime').execute()
with open(google_calendar_pickle, 'wb') as cal:
pickle.dump(events_result, cal)
else:
logging.debug("Pickle is fresh, no need to call the Calendar API")
with open(google_calendar_pickle,'rb') as cal:
events_result = pickle.load(cal)
events = events_result.get('items', [])
if not events:
logging.info("No upcoming events found.")
return events
def get_output_dict_by_events(events, event_slot_count):
formatted_events={}
event_count = len(events)
for event_i in range(event_slot_count):
event_label_id = str(event_i + 1)
if (event_i <= event_count - 1):
formatted_events['CAL_DATETIME_' + event_label_id] = get_datetime_formatted(events[event_i]['start'])
formatted_events['CAL_DESC_' + event_label_id] = smart_truncate(events[event_i]['summary'])
else:
formatted_events['CAL_DATETIME_' + event_label_id] = ""
formatted_events['CAL_DESC_' + event_label_id] = ""
return formatted_events
def get_datetime_formatted(event_start):
if(event_start.get('dateTime')):
start = event_start.get('dateTime')
day = time.strftime("%a %b %-d, %H:%M", time.strptime(start,"%Y-%m-%dT%H:%M:%S%z"))
else:
start = event_start.get('date')
day = time.strftime("%a %b %-d", time.strptime(start, "%Y-%m-%d"))
return day
def smart_truncate(content, length=20, suffix='...'):
if len(content) <= length:
return content
else:
return ' '.join(content[:length+1].split(' ')[0:-1]) + suffix
def main():
output_svg_filename = 'screen-output-weather.svg'
events = get_events(max_event_results)
output_dict = get_output_dict_by_events(events, max_event_results)
logging.debug("main() - {}".format(output_dict))
logging.info("Updating SVG")
update_svg(output_svg_filename, output_svg_filename, output_dict)
if __name__ == "__main__":
main()