furmeet_events_htmltable2ics/backend_code/app.py

167 lines
7.3 KiB
Python

#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import json
from pathlib import Path
import datetime
import traceback
import timetable_parser
from flask import Flask, make_response, render_template, request, send_file
shared_solution_key = Path(__file__).parent.joinpath(
'shared_solution_key.txt').read_text(encoding='utf-8').strip()
app = Flask(__name__, instance_relative_config=True)
@app.route('/', methods=['HEAD', 'OPTIONS', 'GET'])
def index():
return render_template('index.html', datetime=datetime)
@app.route('/favicon.ico', methods=['HEAD', 'OPTIONS', 'GET'])
def favicon():
return send_file(Path('favicon.ico'))
@app.route('/convert.ics', methods=['HEAD', 'OPTIONS', 'GET'])
def convert():
key = request.args.get('key', '').strip()
if shared_solution_key != key:
raise ValueError('Wrong access key')
try:
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()
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))))
'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,
ignore_names=ignore_names,
)
resp_text = str(calendar)
resp = make_response(resp_text.encode('utf-8'), 200)
resp.mimetype = 'text/plain'
resp.mimetype_params['charset'] = 'utf-8'
return resp
except Exception:
tb = traceback.format_exc()
resp = make_response(tb.encode('utf-8'), 500)
resp.mimetype = 'text/plain'
resp.mimetype_params['charset'] = 'utf-8'
return resp
@app.route('/multiconvert.json', methods=['HEAD', 'OPTIONS', 'GET'])
def multiconvert():
key = request.args.get('key', '').strip()
if shared_solution_key != key:
raise ValueError('Wrong access key')
try:
ml_merging_strategy = request.args.get(
'ml_merging_strategy', '').strip()
ml_merging_expr = request.args.get('ml_merging_expr', '').strip()
ml_merging_conflict_fallback = request.args.get(
'ml_merging_conflict_fallback', '').strip()
',EN,ES,PT'.split(',').index(ml_merging_conflict_fallback)
ml_isep = request.args.get('ml_isep', '').strip()
ml_osep = request.args.get('ml_osep', '').strip()
ml_year = request.args.get('ml_year', '').strip()
ml_month = request.args.get('ml_month', '').strip()
ml_day = request.args.get('ml_day', '').strip()
ml_tzcode = request.args.get('ml_tzcode', '').strip()
ml_ignore_names = request.args.get('ml_ignore_names', '').strip()
ml_ignore_names_sep = request.args.get(
'ml_ignore_names_sep', '').strip()
en_url = request.args.get('en_url', '').strip()
en_format = request.args.get('en_format', '').strip()
en_axis = request.args.get('en_axis', '').strip()
en_lang = request.args.get('en_lang', '').strip()
en_content = request.args.get('en_content', '').strip()
es_url = request.args.get('es_url', '').strip()
es_format = request.args.get('es_format', '').strip()
es_axis = request.args.get('es_axis', '').strip()
es_lang = request.args.get('es_lang', '').strip()
es_content = request.args.get('es_content', '').strip()
pt_url = request.args.get('pt_url', '').strip()
pt_format = request.args.get('pt_format', '').strip()
pt_axis = request.args.get('pt_axis', '').strip()
pt_lang = request.args.get('pt_lang', '').strip()
pt_content = request.args.get('pt_content', '').strip()
resp = timetable_parser.multi_decomposed_args(
ml_merging_strategy=bool(
['positional', 'expression'].index(ml_merging_strategy)),
ml_merging_sets=[
*filter(len,
map(lambda o: [*filter(len,
map(str.strip,
o.split(ml_isep)
))],
map(str.strip,
ml_merging_expr.split(ml_osep)
)
))
],
ml_merging_conflict_fallback=None if ml_merging_conflict_fallback == '' else ml_merging_conflict_fallback,
ml_year=int(ml_year),
ml_month=int(ml_month),
ml_day=int(ml_day),
ml_tzcode=ml_tzcode,
ml_ignore=[
*filter(len, map(str.strip, ml_ignore_names.split(ml_ignore_names_sep)))],
en_origin=timetable_parser.Source.url,
en_url=en_url,
en_format=timetable_parser.Format(en_format),
en_axis=[timetable_parser.AxisMeaning.time, timetable_parser.AxisMeaning.place,
][int(bool(en_axis == 'trans'))],
en_lang=en_lang,
en_content=timetable_parser.ContentMeaning(en_content),
es_origin=timetable_parser.Source.url,
es_url=es_url,
es_format=timetable_parser.Format(es_format),
es_axis=[timetable_parser.AxisMeaning.time, timetable_parser.AxisMeaning.place,
][int(bool(es_axis == 'trans'))],
es_lang=es_lang,
es_content=timetable_parser.ContentMeaning(es_content),
pt_origin=timetable_parser.Source.url,
pt_url=pt_url,
pt_format=timetable_parser.Format(pt_format),
pt_axis=[timetable_parser.AxisMeaning.time, timetable_parser.AxisMeaning.place,
][int(bool(pt_axis == 'trans'))],
pt_lang=pt_lang,
pt_content=timetable_parser.ContentMeaning(pt_content),
)
resp_text = json.dumps(resp, indent=4)
resp = make_response(resp_text.encode('utf-8'), 200)
resp.mimetype = 'application/json'
resp.mimetype_params['charset'] = 'utf-8'
return resp
except Exception:
tb = traceback.format_exc()
resp = make_response(tb.encode('utf-8'), 500)
resp.mimetype = 'text/plain'
resp.mimetype_params['charset'] = 'utf-8'
return resp