From b3f88c1aa102ae2b8511d098ecd1800dbf5e6ffd Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Thu, 9 Jun 2022 19:42:12 +0200 Subject: [PATCH 1/2] Create GitHub action to update calendar when new entry is added --- update_calendar.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 update_calendar.py diff --git a/update_calendar.py b/update_calendar.py new file mode 100644 index 0000000..0f334b9 --- /dev/null +++ b/update_calendar.py @@ -0,0 +1,74 @@ +from __future__ import print_function + +import csv +import datetime +from pathlib import Path +from pprint import pprint + +from google.oauth2 import service_account +from googleapiclient.discovery import build + +SCOPES = [ + "https://www.googleapis.com/auth/calendar.events", + "https://www.googleapis.com/auth/calendar", +] + +# This calendar ID is public. +CALENDAR_ID = "0ji9k94abd2sehteqqqt4jo47c@group.calendar.google.com" + +CURRENT_YEAR = datetime.datetime.now().year + + +def main(): + creds = service_account.Credentials.from_service_account_file( + "credentials.json", scopes=SCOPES + ) + service = build("calendar", "v3", credentials=creds) + + current_events = ( + service.events().list(calendarId=CALENDAR_ID, pageToken=None).execute() + ) + + with Path(f"{CURRENT_YEAR}.csv").open("r") as csv_file: + csv_reader = csv.DictReader(csv_file, delimiter=",") + csv_events = [row for row in csv_reader] + + for event in csv_events: + if event["Subject"] in { + curr_event["summary"] for curr_event in current_events["items"] + }: + continue + + py_event_body = { + "summary": event["Subject"], + "location": event["Location"], + "start": {"date": event["Start Date"]}, + "end": {"date": event["End Date"]}, + "description": event.get("Website URL", ""), + } + python_event = ( + service.events() + .insert(calendarId=CALENDAR_ID, body=py_event_body, sendUpdates="all") + .execute() + ) + print("Event created: %s" % (python_event.get("htmlLink"))) + + deadline = event["Talk Deadline"] + if deadline: + talk_event_body = { + "summary": event["Subject"], + "location": event["Location"], + "start": {"date": deadline}, + "end": {"date": deadline}, + "description": event.get("Website URL", ""), + } + talk_proposal_event = ( + service.events() + .insert(calendarId=CALENDAR_ID, body=talk_event_body, sendUpdates="all") + .execute() + ) + print("Event created: %s" % (talk_proposal_event.get("htmlLink"))) + + +if __name__ == "__main__": + main() From c4943c7657da308231deff147d2662be77dd9424 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Thu, 9 Jun 2022 19:43:49 +0200 Subject: [PATCH 2/2] Remove unused imports --- update_calendar.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/update_calendar.py b/update_calendar.py index 0f334b9..0abd41e 100644 --- a/update_calendar.py +++ b/update_calendar.py @@ -1,9 +1,6 @@ -from __future__ import print_function - import csv import datetime from pathlib import Path -from pprint import pprint from google.oauth2 import service_account from googleapiclient.discovery import build