display emojos by category
This commit is contained in:
parent
ba4305089b
commit
f4809d9d71
40
app.py
40
app.py
|
@ -19,10 +19,22 @@ import urllib.parse
|
|||
import botocore.session
|
||||
import requests
|
||||
import serverless_wsgi
|
||||
from collections import defaultdict
|
||||
from dataclasses import dataclass
|
||||
from flask import Flask, redirect, render_template, request, url_for
|
||||
|
||||
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>")
|
||||
def emojo(domain):
|
||||
|
@ -35,26 +47,32 @@ def emojo(domain):
|
|||
else:
|
||||
show_animated = False
|
||||
|
||||
try:
|
||||
url = urllib.parse.urlunsplit(
|
||||
("https", domain, "/api/v1/custom_emojis", "", "")
|
||||
)
|
||||
try:
|
||||
response = requests.get(url)
|
||||
except requests.exceptions.RequestException:
|
||||
return render_template("oh_no.html", domain=domain)
|
||||
|
||||
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"),
|
||||
)
|
||||
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, emojo=emojo, show_animated=show_animated
|
||||
"emojo.html", domain=domain, categories=categories, show_animated=show_animated
|
||||
)
|
||||
except requests.exceptions.RequestException:
|
||||
return render_template("oh_no.html", domain=domain)
|
||||
|
||||
|
||||
@app.route("/favicon.ico")
|
||||
|
|
|
@ -5,23 +5,23 @@
|
|||
<b>{{ domain }}</b> emojo list<br>
|
||||
click/touch to copy to clipboard
|
||||
</p>
|
||||
{% for category, emojo in categories.items() %}
|
||||
<h2 id="{{ category or "(No category)"|slug }}">
|
||||
{% if not category %}
|
||||
<em>(No category)</em>
|
||||
{% else %}
|
||||
<em>{{ category }}</em>
|
||||
{% endif %}
|
||||
</h2>
|
||||
<dl class="emojo">
|
||||
{% if show_animated %}
|
||||
{% for emoj in emojo %}
|
||||
<div>
|
||||
<dt><img src="{{ emoj.url }}" alt=":{{ emoj.shortcode }}:"></dt>
|
||||
<dd>:{{ emoj.shortcode }}:</dd>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
{% for emoj in emojo %}
|
||||
<div>
|
||||
<dt><img src="{{ emoj.static_url }}" alt=":{{ emoj.shortcode }}:"></dt>
|
||||
<dd>:{{ emoj.shortcode }}:</dd>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
</dl>
|
||||
{% endfor %}
|
||||
{% endblock body %}
|
||||
{% block script %}
|
||||
<script src="{{ url_for('static', filename='copy.js') }}"></script>
|
||||
|
|
Loading…
Reference in New Issue