rapla-poc

This commit is contained in:
2023-11-20 14:21:55 +01:00
parent 3709f8d019
commit c111b93480
5 changed files with 113 additions and 8 deletions

2
.gitignore vendored
View File

@ -2,4 +2,4 @@ __pycache__/
ENV/
set/
VIRTUAL_ENV/
calendars/

23
app.py
View File

@ -3,11 +3,11 @@ 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')
@ -19,6 +19,25 @@ def make_msg(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)

60
fetchRAPLA.py Normal file
View File

@ -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)

View File

@ -9,6 +9,10 @@ body
animation-iteration-count: infinite
}
input {
width: 500px
}
.cs
{

22
templates/rapla.html Normal file
View File

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="de">
<head>
<title> {{headermessage}} 👀</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
<script async src="https://analytics.paulmartin.cloud/script.js" data-website-id="1c02dacf-6952-4560-a303-0c25f432c48d"></script>
</head>
<body>
<h1>Verfügbare Raplas </h1>
{% block content %}
{% for i in range (raplas[0]|length) %}
<h2> <a href={{ url_for("getRapla", file=raplas[1][i]) }}> {{ raplas [0] [i] }}</a> </h2>
{% endfor %}
{% endblock %}
<h1>Eigenen Rapla hinzufügen</h1>
<form>
<label for="url">Rapla-URL eingeben, falls Du deinen Kurs nicht siehst:</label>
<input type="text" name="url" id="url">
<input type="submit" value="Importieren!" formaction={{ url_for("getRapla", new="True", url=url) }}>
</form>
</body>
</html>