Created
April 13, 2020 16:19
-
-
Save andylshort/dc9ecd70dbf4782cd781b1e2a00301da to your computer and use it in GitHub Desktop.
iCalendar via Flask
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
from flask import Flask, send_file, make_response, jsonify | |
from icalendar import Calendar, Event | |
from datetime import datetime, timedelta | |
from pytz import UTC | |
import atexit | |
import json | |
import os | |
import tasks | |
app = Flask(__name__) | |
cache = {} | |
@app.route("/") | |
def index(): | |
return "dashboard" | |
@app.route("/tasks") | |
def get_tasks(): | |
return json.dumps(cache["tasks"], default=lambda o: o.__dict__, sort_keys=True, indent=4) | |
@app.route("/calendar") | |
def get_calendar(): | |
cal = Calendar() | |
cal.add("prodid", "-//test//") | |
cal.add("version", "2.0") | |
for t in list(filter(lambda x: x.due is not None, cache["tasks"])): | |
dt = datetime.strptime(t.due, "%Y-%m-%dT%H:%M:%SZ") | |
event = Event() | |
event.add("summary", t["title"]) | |
event.add("dtstart", dt.date()) | |
event.add("dtend", dt.date() + timedelta(days=1)) | |
event.add("dtstamp", datetime.now(tz=UTC)) | |
event.add("priority", 5) | |
cal.add_component(event) | |
response = make_response(cal.to_ical()) | |
response.headers["Content-Disposition"] = "attachment; filename=calendar.ics" | |
return response | |
@atexit.register | |
def quit_server(): | |
print("Writing tasks to file...") | |
tasks.write_tasks(cache["tasks"]) | |
def main(): | |
cache["tasks"] = tasks.load_tasks() | |
app.run(debug=True) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment