Created
March 24, 2021 16:48
-
-
Save rohansingh/bbb5a492529df9e4bbeb58964b77f941 to your computer and use it in GitHub Desktop.
Example of the Google Calendar app for Tidbyt
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
"""GCal app excerpt | |
this is an excerpt that rips out the OAuth 2.0 and GCal API calls from | |
the actual implementation. it's provided as an example of how you could | |
implement a calendar app against any API. | |
API call and parsing of the response should go in the `main` function. | |
the goal is to build an `event` struct that contains a summary, start time, | |
and end time. | |
""" | |
load("render.star", r = "render") | |
load("http.star", "http") | |
load("time.star", "time") | |
load("encoding/base64.star", "base64") | |
load("encoding/json.star", "json") | |
FRAME_DELAY = 100 | |
DEFAULT_TIMEZONE = "US/Eastern" | |
def build_calendar_frame(now, event = None): | |
month = now.format("Jan") | |
# top half displays the calendar icon and date | |
top = [ | |
r.Row( | |
cross_align = "center", | |
expanded = True, | |
children = [ | |
r.PNG(CALENDAR_ICON), | |
r.Box(width = 2, height = 1), | |
r.Text( | |
month.upper(), | |
color = "#ff83f3", | |
offset = -1, | |
), | |
r.Box(width = 1, height = 1), | |
r.Text( | |
str(now.day()), | |
color = "#ff83f3", | |
offset = -1, | |
), | |
], | |
), | |
r.Box(height = 2), | |
] | |
# bottom half displays the upcoming event, if there is one. | |
# otherwise it just shows the time. | |
if event: | |
bottom = [ | |
r.Marquee( | |
width = 64, | |
child = r.Text( | |
event.summary.upper(), | |
), | |
), | |
r.Text( | |
event.start.format("at 3:04 PM"), | |
color = "#fff500", | |
), | |
] | |
else: | |
bottom = [ | |
r.Column( | |
expanded = True, | |
main_align = "end", | |
children = [ | |
r.WrappedText( | |
"NO MORE MEETINGS :)", | |
color = "#fff500", | |
height = 16, | |
), | |
], | |
), | |
] | |
return r.Root( | |
delay = FRAME_DELAY, | |
child = r.Box( | |
padding = 2, | |
color = "#111", | |
child = r.Column( | |
expanded = True, | |
children = top + bottom, | |
), | |
), | |
) | |
def build_event_frame(now, event): | |
minutes_to_start = (event.start - now).minutes() | |
minutes_to_end = (event.end - now).minutes() | |
hours_to_end = (event.end - now).hours() | |
if minutes_to_start >= 1: | |
tagline = ("in %d" % minutes_to_start, "min") | |
elif hours_to_end >= 99: | |
tagline = ("", "now") | |
elif minutes_to_end >= 99: | |
tagline = ("ends in %d" % hours_to_end, "h") | |
elif minutes_to_end > 1: | |
tagline = ("ends in %d" % minutes_to_end, "min") | |
else: | |
tagline = ("", "almost done") | |
return r.Root( | |
child = r.Box( | |
padding = 2, | |
child = r.Column( | |
main_align = "start", | |
cross_align = "start", | |
expanded = True, | |
children = [ | |
r.WrappedText( | |
event.summary.upper(), | |
height = 17, | |
), | |
r.Box( | |
color = "#ff78e9", | |
height = 1, | |
), | |
r.Box(height = 3), | |
r.Row( | |
main_align = "end", | |
expanded = True, | |
children = [ | |
r.Text( | |
tagline[0], | |
color = "#fff500", | |
), | |
r.Box(height = 1, width = 1), | |
r.Text( | |
tagline[1], | |
color = "#fff500", | |
), | |
], | |
), | |
], | |
), | |
), | |
) | |
def main(): | |
tz = DEFAULT_TIMEZONE | |
now = time.now().in_location(tz) | |
res = http.get( | |
url = API_URL, # this is where you'd call a calendar API | |
headers = { | |
# put in an auth header here | |
}, | |
params = { | |
# example of the parameters we pass to the Google Calendar API | |
"maxResults": "10", | |
"orderBy": "startTime", | |
"singleEvents": "true", | |
"timeMin": time.now().format("2006-01-02T15:04:05Z07:00"), | |
}, | |
) | |
if res.status_code != 200: | |
fail("events request failed with status code: %d - %s" % (res.status_code, res.body())) | |
event = None | |
# parse the GCal API response structure | |
for item in res.json()["items"]: | |
if not "dateTime" in item["start"]: | |
continue | |
meeting_response = [ | |
at.get("responseStatus") | |
for at in item.get("attendees", []) | |
if at.get("self") | |
] | |
if meeting_response == ["declined"]: | |
# declined this meeting | |
continue | |
event = struct( | |
summary = item["summary"], | |
start = time.time(item["start"]["dateTime"]).in_location(tz), | |
end = time.time(item["end"]["dateTime"]).in_location(tz), | |
) | |
break | |
if not event: | |
# no events in the calendar | |
return build_calendar_frame(now) | |
if event.start < (now + time.duration("30m")): | |
# event starts soon or is currently happening | |
return build_event_frame(now, event) | |
elif event.start.day() == now.day(): | |
# event is later today | |
return build_calendar_frame(now, event) | |
else: | |
return build_calendar_frame(now) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment