display emojos by category

This commit is contained in:
heartdev 2021-08-31 07:32:08 +00:00
parent ba4305089b
commit f4809d9d71
2 changed files with 49 additions and 31 deletions

50
app.py
View File

@ -19,10 +19,22 @@ import urllib.parse
import botocore.session import botocore.session
import requests import requests
import serverless_wsgi import serverless_wsgi
from collections import defaultdict
from dataclasses import dataclass
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__)
def slug_filter(s):
return s.lower().replace(" ", "-")
app.jinja_env.filters["slug"] = slug_filter
@dataclass
class Emoj:
__slots__ = frozenset({"shortcode", "url"})
shortcode: str
url: str
@app.route("/<domain>") @app.route("/<domain>")
def emojo(domain): def emojo(domain):
@ -35,27 +47,33 @@ def emojo(domain):
else: else:
show_animated = False show_animated = False
url = urllib.parse.urlunsplit(
("https", domain, "/api/v1/custom_emojis", "", "")
)
try: try:
url = urllib.parse.urlunsplit(
("https", domain, "/api/v1/custom_emojis", "", "")
)
response = requests.get(url) response = requests.get(url)
if response.status_code == 401:
return render_template("forbidden.html", domain=domain)
if show_all:
emojo = sorted(response.json(), key=operator.itemgetter("shortcode"))
else:
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: except requests.exceptions.RequestException:
return render_template("oh_no.html", domain=domain) return render_template("oh_no.html", domain=domain)
if response.status_code == 401:
return render_template("forbidden.html", domain=domain)
categories = defaultdict(list)
for emoji in sorted(
response.json(),
# sort by category, then name within each category, then disambiguate by capitalization
key=lambda x: (x.get("category", ""), x["shortcode"].lower(), x["shortcode"]),
):
if not show_all and not emoji.get("visible_in_picker", True):
continue
url = emoji["url" if show_animated else "static_url"]
categories[emoji.get("category")].append(Emoj(shortcode=emoji["shortcode"], url=url))
return render_template(
"emojo.html", domain=domain, categories=categories, show_animated=show_animated
)
@app.route("/favicon.ico") @app.route("/favicon.ico")
@app.route("/robots.txt") @app.route("/robots.txt")

View File

@ -5,23 +5,23 @@
<b>{{ domain }}</b> emojo list<br> <b>{{ domain }}</b> emojo list<br>
click/touch to copy to clipboard click/touch to copy to clipboard
</p> </p>
<dl class="emojo"> {% for category, emojo in categories.items() %}
{% if show_animated %} <h2 id="{{ category or "(No category)"|slug }}">
{% if not category %}
<em>(No category)</em>
{% else %}
<em>{{ category }}</em>
{% endif %}
</h2>
<dl class="emojo">
{% for emoj in emojo %} {% for emoj in emojo %}
<div> <div>
<dt><img src="{{ emoj.url }}" alt=":{{ emoj.shortcode }}:"></dt> <dt><img src="{{ emoj.url }}" alt=":{{ emoj.shortcode }}:"></dt>
<dd>:{{ emoj.shortcode }}:</dd> <dd>:{{ emoj.shortcode }}:</dd>
</div> </div>
{% endfor %} {% endfor %}
{% else %} </dl>
{% for emoj in emojo %} {% endfor %}
<div>
<dt><img src="{{ emoj.static_url }}" alt=":{{ emoj.shortcode }}:"></dt>
<dd>:{{ emoj.shortcode }}:</dd>
</div>
{% endfor %}
{% endif %}
</dl>
{% endblock body %} {% endblock body %}
{% block script %} {% block script %}
<script src="{{ url_for('static', filename='copy.js') }}"></script> <script src="{{ url_for('static', filename='copy.js') }}"></script>