furmeet_events_htmls2lang/backend_code/app.py

55 lines
1.7 KiB
Python

#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import datetime
import json
from pathlib import Path
import htmllang_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.json', methods=['HEAD', 'OPTIONS', 'GET'])
def convert():
key = request.args.get('key', '').strip()
if shared_solution_key != key:
raise ValueError('Wrong access key')
url_en = request.args.get('url_en', '').strip()
url_es = request.args.get('url_es', '').strip()
url_pt = request.args.get('url_pt', '').strip()
css_en = request.args.get('css_en', '').strip()
css_es = request.args.get('css_es', '').strip()
css_pt = request.args.get('css_pt', '').strip()
one_fails_all = bool(request.args.get('one_fails_all', '').strip())
plain = bool(request.args.get('plain', '').strip())
obj = htmllang_parser.main_decomposed_args(
url_en=url_en,
url_es=url_es,
url_pt=url_pt,
css_en=css_en,
css_es=css_es,
css_pt=css_pt,
one_fails_all=one_fails_all,
plain=plain,
timeout=15,
)
resp_text = json.dumps(obj, sort_keys=True, indent=4)
resp = make_response(resp_text.encode('utf-8'), 200)
resp.mimetype = 'application/json'
resp.mimetype_params['charset'] = 'utf-8'
return resp