reformat emojos.py

This commit is contained in:
iliana etaoin 2021-02-11 21:08:12 -08:00
parent 6aa3bd3114
commit 99b58f2713
No known key found for this signature in database
GPG Key ID: 4B1B8478D7E0DF1A
1 changed files with 47 additions and 30 deletions

View File

@ -13,76 +13,93 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
import botocore.session
import operator import operator
import requests
import serverless_wsgi
import urllib.parse import urllib.parse
import botocore.session
import requests
import serverless_wsgi
from flask import Flask, redirect, render_template, request, url_for from flask import Flask, redirect, render_template, request, url_for
app = Flask(__name__) app = Flask(__name__)
class ForbiddenEndpointError(Exception): class ForbiddenEndpointError(Exception):
pass pass
@app.route('/<domain>')
@app.route("/<domain>")
def emojo(domain): def emojo(domain):
if request.args.get('show_all', '') == 'on': if request.args.get("show_all", "") == "on":
show_all = True show_all = True
else: else:
show_all = False show_all = False
if request.args.get('show_animated', '') == 'on': if request.args.get("show_animated", "") == "on":
show_animated = True show_animated = True
else: else:
show_animated = False show_animated = False
try: try:
url = urllib.parse.urlunsplit(('https', domain, '/api/v1/custom_emojis', '', '')) url = urllib.parse.urlunsplit(
("https", domain, "/api/v1/custom_emojis", "", "")
)
response = requests.get(url) response = requests.get(url)
if response.status_code == 401: if response.status_code == 401:
raise ForbiddenEndpointError raise ForbiddenEndpointError
if show_all: if show_all:
emojo = sorted(response.json(), key=operator.itemgetter('shortcode')) emojo = sorted(response.json(), key=operator.itemgetter("shortcode"))
else: else:
emojo = sorted(filter(lambda x: x.get('visible_in_picker', True), response.json()), emojo = sorted(
key=operator.itemgetter('shortcode')) filter(lambda x: x.get("visible_in_picker", True), response.json()),
return render_template('emojo.html', domain=domain, emojo=emojo, show_animated=show_animated) key=operator.itemgetter("shortcode"),
except requests.exceptions.RequestException as e: )
return render_template('oh_no.html', domain=domain) return render_template(
"emojo.html", domain=domain, emojo=emojo, show_animated=show_animated
)
except requests.exceptions.RequestException:
return render_template("oh_no.html", domain=domain)
except ForbiddenEndpointError: except ForbiddenEndpointError:
return render_template('forbidden.html', domain=domain) return render_template("forbidden.html", domain=domain)
@app.route('/favicon.ico') @app.route("/favicon.ico")
@app.route('/robots.txt') @app.route("/robots.txt")
def no_content(): def no_content():
return ('', 204) return ("", 204)
@app.route('/code') @app.route("/code")
def code(): def code():
context = request.environ.get('context') context = request.environ.get("context")
session = botocore.session.get_session() session = botocore.session.get_session()
# region name is detected from lambda environment # region name is detected from lambda environment
client = session.create_client('lambda') client = session.create_client("lambda")
code = client.get_function(FunctionName=context.function_name, Qualifier=context.function_version) code = client.get_function(
return redirect(code['Code']['Location'], code=303) FunctionName=context.function_name, Qualifier=context.function_version
)
return redirect(code["Code"]["Location"], code=303)
@app.route('/', methods=('GET', 'POST')) @app.route("/", methods=("GET", "POST"))
def index(): def index():
if request.method == 'POST': if request.method == "POST":
if 'instance' in request.form: if "instance" in request.form:
show_all = request.form.get('show_all') show_all = request.form.get("show_all")
show_animated = request.form.get('show_animated') show_animated = request.form.get("show_animated")
return redirect( return redirect(
url_for('emojo', domain=request.form['instance'], show_all=show_all, show_animated=show_animated)) url_for(
"emojo",
domain=request.form["instance"],
show_all=show_all,
show_animated=show_animated,
)
)
else: else:
return redirect(url_for('index')) return redirect(url_for("index"))
else: else:
return render_template('index.html') return render_template("index.html")
def handle_request(event, context): def handle_request(event, context):
return serverless_wsgi.handle_request(app, event, context) return serverless_wsgi.handle_request(app, event, context)