2021-02-11 21:05:27 -08:00
|
|
|
# Copyright (c) 2021 iliana etaoin
|
2018-04-01 20:23:21 -07:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
# Software Foundation, either version 3 of the License, or (at your option) any
|
|
|
|
# later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
import operator
|
|
|
|
import urllib.parse
|
|
|
|
|
2021-02-11 21:08:12 -08:00
|
|
|
import botocore.session
|
|
|
|
import requests
|
|
|
|
import serverless_wsgi
|
2018-04-01 20:23:21 -07:00
|
|
|
from flask import Flask, redirect, render_template, request, url_for
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
2021-02-11 21:08:12 -08:00
|
|
|
|
2021-01-02 01:25:31 -08:00
|
|
|
class ForbiddenEndpointError(Exception):
|
|
|
|
pass
|
2018-04-01 20:23:21 -07:00
|
|
|
|
2021-02-11 21:08:12 -08:00
|
|
|
|
|
|
|
@app.route("/<domain>")
|
2018-04-01 20:23:21 -07:00
|
|
|
def emojo(domain):
|
2021-02-11 21:08:12 -08:00
|
|
|
if request.args.get("show_all", "") == "on":
|
2019-07-15 18:06:48 -07:00
|
|
|
show_all = True
|
2019-07-15 03:50:57 -07:00
|
|
|
else:
|
2019-07-15 18:06:48 -07:00
|
|
|
show_all = False
|
2021-02-11 21:08:12 -08:00
|
|
|
if request.args.get("show_animated", "") == "on":
|
2019-07-15 18:06:48 -07:00
|
|
|
show_animated = True
|
2019-07-15 03:50:57 -07:00
|
|
|
else:
|
2019-07-15 18:06:48 -07:00
|
|
|
show_animated = False
|
|
|
|
|
2018-04-01 20:23:21 -07:00
|
|
|
try:
|
2021-02-11 21:08:12 -08:00
|
|
|
url = urllib.parse.urlunsplit(
|
|
|
|
("https", domain, "/api/v1/custom_emojis", "", "")
|
|
|
|
)
|
2021-01-02 01:25:31 -08:00
|
|
|
response = requests.get(url)
|
|
|
|
if response.status_code == 401:
|
|
|
|
raise ForbiddenEndpointError
|
|
|
|
|
2019-07-15 18:06:48 -07:00
|
|
|
if show_all:
|
2021-02-11 21:08:12 -08:00
|
|
|
emojo = sorted(response.json(), key=operator.itemgetter("shortcode"))
|
2019-07-15 03:50:57 -07:00
|
|
|
else:
|
2021-02-11 21:08:12 -08:00
|
|
|
emojo = sorted(
|
|
|
|
filter(lambda x: x.get("visible_in_picker", True), response.json()),
|
|
|
|
key=operator.itemgetter("shortcode"),
|
|
|
|
)
|
|
|
|
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)
|
2021-01-02 01:25:31 -08:00
|
|
|
except ForbiddenEndpointError:
|
2021-02-11 21:08:12 -08:00
|
|
|
return render_template("forbidden.html", domain=domain)
|
2018-04-01 20:23:21 -07:00
|
|
|
|
|
|
|
|
2021-02-11 21:08:12 -08:00
|
|
|
@app.route("/favicon.ico")
|
|
|
|
@app.route("/robots.txt")
|
2018-04-03 18:30:30 -07:00
|
|
|
def no_content():
|
2021-02-11 21:08:12 -08:00
|
|
|
return ("", 204)
|
2018-04-03 18:30:30 -07:00
|
|
|
|
|
|
|
|
2021-02-11 21:08:12 -08:00
|
|
|
@app.route("/code")
|
2018-04-03 18:31:14 -07:00
|
|
|
def code():
|
2021-02-11 21:08:12 -08:00
|
|
|
context = request.environ.get("context")
|
2018-04-03 18:31:14 -07:00
|
|
|
session = botocore.session.get_session()
|
|
|
|
# region name is detected from lambda environment
|
2021-02-11 21:08:12 -08:00
|
|
|
client = session.create_client("lambda")
|
|
|
|
code = client.get_function(
|
|
|
|
FunctionName=context.function_name, Qualifier=context.function_version
|
|
|
|
)
|
|
|
|
return redirect(code["Code"]["Location"], code=303)
|
2018-04-03 18:31:14 -07:00
|
|
|
|
|
|
|
|
2021-02-11 21:08:12 -08:00
|
|
|
@app.route("/", methods=("GET", "POST"))
|
2018-04-01 20:23:21 -07:00
|
|
|
def index():
|
2021-02-11 21:08:12 -08:00
|
|
|
if request.method == "POST":
|
|
|
|
if "instance" in request.form:
|
|
|
|
show_all = request.form.get("show_all")
|
|
|
|
show_animated = request.form.get("show_animated")
|
2019-07-15 18:06:48 -07:00
|
|
|
return redirect(
|
2021-02-11 21:08:12 -08:00
|
|
|
url_for(
|
|
|
|
"emojo",
|
|
|
|
domain=request.form["instance"],
|
|
|
|
show_all=show_all,
|
|
|
|
show_animated=show_animated,
|
|
|
|
)
|
|
|
|
)
|
2018-04-01 20:23:21 -07:00
|
|
|
else:
|
2021-02-11 21:08:12 -08:00
|
|
|
return redirect(url_for("index"))
|
2018-04-01 20:23:21 -07:00
|
|
|
else:
|
2021-02-11 21:08:12 -08:00
|
|
|
return render_template("index.html")
|
|
|
|
|
2021-02-11 21:05:27 -08:00
|
|
|
|
|
|
|
def handle_request(event, context):
|
|
|
|
return serverless_wsgi.handle_request(app, event, context)
|