|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Code contributed by github user seidnerj in |
| 4 | +https://github.com/python-caldav/caldav/issues/119#issuecomment-2561980368 |
| 5 | +
|
| 6 | +This code has not been tested by the maintainer of the caldav library. |
| 7 | +""" |
| 8 | +import os |
| 9 | + |
| 10 | +from flask import Flask |
| 11 | +from flask import jsonify |
| 12 | +from flask import Response |
| 13 | +from google.oauth2.credentials import Credentials |
| 14 | + |
| 15 | +from caldav import DAVClient |
| 16 | +from caldav.requests import HTTPBearerAuth |
| 17 | + |
| 18 | + |
| 19 | +app = Flask(__name__) |
| 20 | + |
| 21 | +# Constants |
| 22 | +CREDENTIALS_FILE = "credentials.json" |
| 23 | +TOKEN_FILE = "token.json" |
| 24 | +CALDAV_URL_TEMPLATE = ( |
| 25 | + "https://apidata.googleusercontent.com/caldav/v2/{calendar_id}/user" |
| 26 | +) |
| 27 | + |
| 28 | +# Cache to store calendar summaries and IDs |
| 29 | +CALENDAR_CACHE = {} |
| 30 | + |
| 31 | + |
| 32 | +def get_google_credentials(): |
| 33 | + """ |
| 34 | + Load or refresh Google OAuth2 credentials. |
| 35 | + """ |
| 36 | + |
| 37 | + if os.path.exists(TOKEN_FILE): |
| 38 | + creds = Credentials.from_authorized_user_file(TOKEN_FILE) |
| 39 | + else: |
| 40 | + from google_auth_oauthlib.flow import InstalledAppFlow |
| 41 | + |
| 42 | + flow = InstalledAppFlow.from_client_secrets_file( |
| 43 | + CREDENTIALS_FILE, scopes=["https://www.googleapis.com/auth/calendar"] |
| 44 | + ) |
| 45 | + creds = flow.run_local_server(port=0) |
| 46 | + |
| 47 | + # save credentials for future use |
| 48 | + with open(TOKEN_FILE, "w") as token: |
| 49 | + token.write(creds.to_json()) |
| 50 | + |
| 51 | + return creds |
| 52 | + |
| 53 | + |
| 54 | +def get_calendar_list(): |
| 55 | + """ |
| 56 | + Fetch the list of calendars and store them in the cache. |
| 57 | + """ |
| 58 | + |
| 59 | + creds = get_google_credentials() |
| 60 | + from googleapiclient.discovery import build |
| 61 | + |
| 62 | + service = build("calendar", "v3", credentials=creds) |
| 63 | + |
| 64 | + calendars = service.calendarList().list().execute() |
| 65 | + for calendar in calendars.get("items", []): |
| 66 | + CALENDAR_CACHE[calendar["summary"]] = calendar["id"] |
| 67 | + |
| 68 | + |
| 69 | +@app.route("/calendars", methods=["GET"]) |
| 70 | +def list_calendars(): |
| 71 | + """ |
| 72 | + Endpoint to list all available calendars. |
| 73 | + """ |
| 74 | + if not CALENDAR_CACHE: |
| 75 | + get_calendar_list() |
| 76 | + |
| 77 | + return jsonify({"calendars": list(CALENDAR_CACHE.keys())}) |
| 78 | + |
| 79 | + |
| 80 | +@app.route("/calendar/<calendar_name>.ics", methods=["GET"]) |
| 81 | +def serve_calendar_ics(calendar_name): |
| 82 | + """ |
| 83 | + Endpoint to serve .ics data for a specific calendar. |
| 84 | + """ |
| 85 | + if calendar_name not in CALENDAR_CACHE: |
| 86 | + return jsonify({"error": "Calendar not found"}), 404 |
| 87 | + |
| 88 | + calendar_id = CALENDAR_CACHE[calendar_name] |
| 89 | + creds = get_google_credentials() |
| 90 | + access_token = creds.token |
| 91 | + |
| 92 | + try: |
| 93 | + calendar_url = CALDAV_URL_TEMPLATE.format(calendar_id=calendar_id) |
| 94 | + |
| 95 | + # connect to the calendar using CalDAV |
| 96 | + client = DAVClient(url=calendar_url, auth=HTTPBearerAuth(access_token)) |
| 97 | + principal = client.principal() |
| 98 | + calendars = principal.calendars() |
| 99 | + |
| 100 | + # fetch events from the first calendar (usually the only one) |
| 101 | + calendar = calendars[0] |
| 102 | + ics_data = "" |
| 103 | + for event in calendar.events(): |
| 104 | + ics_data += event.data |
| 105 | + |
| 106 | + # serve the calendar as an ICS file |
| 107 | + return Response(ics_data, mimetype="text/calendar") |
| 108 | + except Exception as ex: |
| 109 | + return jsonify({"error": str(ex)}), 500 |
| 110 | + |
| 111 | + |
| 112 | +# requirements: flask, caldav, google-auth, google-auth-oauthlib, google-api-python-client |
| 113 | +if __name__ == "__main__": |
| 114 | + # preload calendar list on server start |
| 115 | + get_calendar_list() |
| 116 | + app.run(host="0.0.0.0", port=5000) |
0 commit comments