|
1 | | -from flask import Flask |
| 1 | +import os |
| 2 | +import requests |
| 3 | +import time |
| 4 | +import icalendar |
| 5 | + |
| 6 | +from datetime import datetime |
| 7 | +from flask import Flask, request, abort |
| 8 | +from flask_cors import CORS |
| 9 | +from requests_file import FileAdapter |
| 10 | +from cachetools import TTLCache, cached |
| 11 | + |
| 12 | +CACHE_TTL = os.environ.get("CACHE_TTL", 1800) |
| 13 | + |
| 14 | +CACHE = TTLCache(maxsize=1000, ttl=CACHE_TTL) |
2 | 15 |
|
3 | 16 | app = Flask(__name__) |
4 | 17 |
|
5 | | -_SERVER_NAME = 'iCal SimpleJson Proxy' |
| 18 | +cors = CORS(app) |
| 19 | +app.config["CORS_HEADERS"] = "Content-Type" |
| 20 | + |
| 21 | +methods = ["GET", "POST", "HEAD", "OPTIONS"] |
| 22 | + |
6 | 23 |
|
7 | 24 | @app.route("/") |
8 | | -def ok(): |
| 25 | +def index(): |
9 | 26 | return "OK" |
10 | 27 |
|
11 | | -@app.route("/query") |
| 28 | + |
| 29 | +@app.route("/search", methods=methods) |
| 30 | +def search(): |
| 31 | + return [] |
| 32 | + |
| 33 | + |
| 34 | +@app.route("/query", methods=methods) |
12 | 35 | def query(): |
13 | | - return {} |
14 | | - |
15 | | -@app.after_request |
16 | | -def add_cors_headers(response): |
17 | | - response.headers['Access-Control-Allow-Headers'] = 'accept, content-type' |
18 | | - response.headers['Access-Control-Allow-Methods'] = 'POST, OPTIONS' |
19 | | - response.headers['Access-Control-Allow-Origin'] = '*' |
20 | | - return response |
| 36 | + return [] |
| 37 | + |
| 38 | + |
| 39 | +@app.route("/annotations", methods=methods) |
| 40 | +def annotations(): |
| 41 | + if request.method == "POST": |
| 42 | + annotation = request.json.get( |
| 43 | + "annotation", |
| 44 | + { |
| 45 | + "name": "Test Annotation", |
| 46 | + "datasource": { |
| 47 | + "type": "grafana-simple-json-datasource", |
| 48 | + "uid": "TEST", |
| 49 | + }, |
| 50 | + "enable": True, |
| 51 | + "iconColor": "red", |
| 52 | + "query": "#test", |
| 53 | + }, |
| 54 | + ) |
| 55 | + ical_url = request.headers.get( |
| 56 | + "X-ICAL-URL", |
| 57 | + f"file://{os.path.dirname(os.path.realpath(__file__))}/fixtures/calendar.ics", |
| 58 | + ) |
| 59 | + |
| 60 | + tags = [] |
| 61 | + if "X-TAGS" in request.headers: |
| 62 | + tags = request.headers.get("X-TAGS").split(",") |
| 63 | + |
| 64 | + return _ical_annotations(ical_url, tags, annotation) |
| 65 | + return [] |
| 66 | + |
| 67 | + |
| 68 | +@app.route("/tag-keys", methods=methods) |
| 69 | +def tag_keys(): |
| 70 | + return [] |
| 71 | + |
| 72 | + |
| 73 | +@app.route("/tag-values", methods=methods) |
| 74 | +def tag_values(): |
| 75 | + return [] |
| 76 | + |
| 77 | + |
| 78 | +def _ical_annotations_cache_key(url: str, tags: list, *args): |
| 79 | + return f'{url}/{",".join(tags)}' |
| 80 | + |
| 81 | + |
| 82 | +@cached(CACHE, _ical_annotations_cache_key) |
| 83 | +def _ical_annotations(url: str, tags: list, annotation: dict): |
| 84 | + ical_data = _fetch_ical_data(url) |
| 85 | + ical_as_annotations = _convert_ical_to_annotations(ical_data, tags) |
| 86 | + |
| 87 | + return [dict(x, **{"annotation": annotation}) for x in ical_as_annotations] |
| 88 | + |
| 89 | + |
| 90 | +def _fetch_ical_data(url: str): |
| 91 | + s = requests.Session() |
| 92 | + s.mount("file://", FileAdapter()) |
| 93 | + |
| 94 | + ical_resp = s.get(url) |
| 95 | + if ical_resp.status_code != 200: |
| 96 | + abort(ical_resp.status_code) |
| 97 | + |
| 98 | + ical_data = ical_resp.text |
| 99 | + if not ical_data.startswith("BEGIN:VCALENDAR"): |
| 100 | + abort(400) |
| 101 | + |
| 102 | + return ical_data |
| 103 | + |
| 104 | + |
| 105 | +def _convert_ical_to_annotations(ical_data, tags): |
| 106 | + ical_annotations = [] |
| 107 | + ical_calendar = icalendar.Calendar.from_ical(ical_data) |
| 108 | + |
| 109 | + for component in ical_calendar.walk(): |
| 110 | + if component.name == "VEVENT": |
| 111 | + try: |
| 112 | + ical_annotations.append( |
| 113 | + { |
| 114 | + "time": int( |
| 115 | + time.mktime(component.get("dtstart").dt.timetuple()) * 1000 |
| 116 | + ), |
| 117 | + "title": component.get("summary"), |
| 118 | + "tags": tags, |
| 119 | + "text": component.get("description", ""), |
| 120 | + } |
| 121 | + ) |
| 122 | + except AttributeError: |
| 123 | + print( |
| 124 | + f'level=ERROR msg="decoding event failed" event_summary="{component.get("summary")}" event_dtstart="{component.get("dtstart")}" event_dtstamp="{component.get("dtstamp")}" event_dtend="{component.get("dtend")}"' |
| 125 | + ) |
| 126 | + return ical_annotations |
0 commit comments