furmeet_events_htmltable2ics/backend_code/app.py

61 lines
2.2 KiB
Python
Raw Normal View History

2022-04-24 15:09:38 +00:00
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
2022-04-24 18:24:34 +00:00
from pathlib import Path
import datetime
2022-04-24 15:50:53 +00:00
import timetable_parser
2022-04-24 18:24:34 +00:00
from flask import Flask, make_response, render_template, request, send_file
2022-04-24 15:09:38 +00:00
app = Flask(__name__, instance_relative_config=True)
@app.route('/', methods=['HEAD', 'OPTIONS', 'GET'])
def index():
2022-04-24 18:24:34 +00:00
return render_template('index.html', datetime=datetime)
@app.route('/favicon.ico', methods=['HEAD', 'OPTIONS', 'GET'])
def favicon():
return send_file(Path('favicon.ico'))
2022-04-24 15:09:38 +00:00
@app.route('/convert.ics', methods=['HEAD', 'OPTIONS', 'GET'])
def convert():
2022-04-24 18:51:18 +00:00
url = request.args.get('url', '').strip()
format_ = request.args.get('format', '').strip()
axis = request.args.get('axis', '').strip()
lang = request.args.get('lang', '').strip()
content = request.args.get('content', '').strip()
year = request.args.get('year', '').strip()
month = request.args.get('month', '').strip()
day = request.args.get('day', '').strip()
tzcode = request.args.get('tzcode', '').strip()
2022-04-24 19:06:57 +00:00
ignore_names_str = request.args.get('ignore_names', '').strip()
ignore_names_sep = request.args.get('ignore_names_sep', '').strip()
ignore_names = list(
map(str.lower, filter(len, ignore_names_str.split(ignore_names_sep))))
2022-04-24 18:51:18 +00:00
'EN,ES,PT'.split(',').index(lang)
calendar = timetable_parser.main_decomposed_args(
origin=timetable_parser.Source.url,
path=url,
format_=timetable_parser.Format(format_),
line=[timetable_parser.AxisMeaning.time, timetable_parser.AxisMeaning.place,
][int(bool(axis == 'trans'))],
column=[timetable_parser.AxisMeaning.time, timetable_parser.AxisMeaning.place,
][int(bool(axis != 'trans'))],
content=timetable_parser.ContentMeaning(content),
year=int(year) if year != '' else datetime.date.today().year,
month=int(month) if month != '' else datetime.date.today().month,
day=int(day) if day != '' else datetime.date.today().day,
lang=lang,
timezone_=tzcode,
2022-04-24 19:06:57 +00:00
ignore_names=ignore_names,
2022-04-24 18:51:18 +00:00
)
resp_text = str(calendar)
2022-04-24 18:24:34 +00:00
resp = make_response(resp_text.encode('utf-8'), 200)
2022-04-24 15:09:38 +00:00
resp.mimetype = 'text/plain'
resp.mimetype_params['charset'] = 'utf-8'
return resp