From c111b9348067f174770677d3038ffb7313cb58bf Mon Sep 17 00:00:00 2001 From: paulmart-n Date: Mon, 20 Nov 2023 14:21:55 +0100 Subject: [PATCH] rapla-poc --- .gitignore | 2 +- app.py | 33 ++++++++++++++++++------ fetchRAPLA.py | 60 ++++++++++++++++++++++++++++++++++++++++++++ static/style.css | 4 +++ templates/rapla.html | 22 ++++++++++++++++ 5 files changed, 113 insertions(+), 8 deletions(-) create mode 100644 fetchRAPLA.py create mode 100644 templates/rapla.html diff --git a/.gitignore b/.gitignore index 1fcf22c..7147a53 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ __pycache__/ ENV/ set/ VIRTUAL_ENV/ - +calendars/ \ No newline at end of file diff --git a/app.py b/app.py index 5a07c9f..6fdbd47 100644 --- a/app.py +++ b/app.py @@ -3,22 +3,41 @@ import os from flask import Flask from flask import render_template, url_for, send_from_directory, redirect, request, send_file from werkzeug.utils import secure_filename - - +from fetchRAPLA import * app = Flask(__name__) + @app.route("/") def index(): - return render_template('index.html', headermessage = 'Header', message='DualHub') + return render_template('index.html', headermessage='Header', message='DualHub') -@app.route ("/") +@app.route("/") def make_msg(msg): - message = msg - return render_template('index.html', message="DualHub", headermessage=message) + message = msg + return render_template('index.html', message="DualHub", headermessage=message) +@app.route("/rapla", methods=["GET", "POST"]) +def chooseRaplas(): + r = getRaplas() + return render_template("rapla.html", raplas=r) + + +@app.route("/plan") +def getRapla(): + file = str(request.values.get("file")) + url = str(request.values.get("url")) + if file != "None": + print(file) + return send_file("calendars/" + file) + elif url != "None": + file = getNewRapla(url) + if type(file) != int: + return send_file("calendars/" + file) + return render_template("index.html") + if __name__ == "__main__": - app.run(host='0.0.0.0', port=2024, debug=True) + app.run(host='0.0.0.0', port=2024, debug=True) diff --git a/fetchRAPLA.py b/fetchRAPLA.py new file mode 100644 index 0000000..7cb0765 --- /dev/null +++ b/fetchRAPLA.py @@ -0,0 +1,60 @@ +import urllib.error +from urllib.request import urlretrieve +import icalendar +import json + + +def parseURL(url: str): + rapla = url.find("rapla.") + if rapla == -1: + return 0 + elif len(url[:rapla]) == 4 or len(url[:rapla]) > 6: + url = url[rapla:] + http = url.find(":") + if url[:http] == "http": + url = "https" + url[http:] + elif http == -1: + url = "https://" + url + p = url.find("page=") + u = url.find("&") + if (url[p + 5:u]).lower() == "ical": + return url + elif p != -1: + return url[:p + 5] + "ical" + url[u:] + else: + return 0 + + +def getNewRapla(url: str): + url = parseURL(url) + if url == 0: + return 0 + urlfile = url.find("file=") + kurs = url[urlfile + 5:].upper() + + try: + urlretrieve(url, "calendars/rapla" + kurs + ".ical") + except urllib.error.URLError: + return -1 + + with open("calendars/rapla" + kurs + ".ical") as f: + calendar = icalendar.Calendar.from_ical(f.read()) + # events = calendar.walk("VEVENT") + file = open("calendars/list.json", "r+") + jsoncal = json.load(file) + jsoncal.update({kurs: "rapla" + kurs + ".ical"}) + file.close() + file = open("calendars/list.json", "w") + json.dump(jsoncal, file, indent=4) + return "rapla"+kurs+".ical" + + +def getRaplas(): + file = open("calendars/list.json", "r") + jsonf = json.load(file) + kursl = [] + filel = [] + for i in jsonf: + kursl += [i] + filel += [jsonf[i]] + return sorted(kursl), sorted(filel) diff --git a/static/style.css b/static/style.css index 954e2c1..ff5b461 100644 --- a/static/style.css +++ b/static/style.css @@ -9,6 +9,10 @@ body animation-iteration-count: infinite } +input { + width: 500px +} + .cs { diff --git a/templates/rapla.html b/templates/rapla.html new file mode 100644 index 0000000..cb646d6 --- /dev/null +++ b/templates/rapla.html @@ -0,0 +1,22 @@ + + + + {{headermessage}} 👀 + + + + +

Verfügbare Raplas

+ {% block content %} + {% for i in range (raplas[0]|length) %} +

{{ raplas [0] [i] }}

+ {% endfor %} + {% endblock %} +

Eigenen Rapla hinzufügen

+
+ + + +
+ +