From f4809d9d71ed28a699805419c7ba33674558c529 Mon Sep 17 00:00:00 2001
From: heartdev <88726114+lt3Dev@users.noreply.github.com>
Date: Tue, 31 Aug 2021 07:32:08 +0000
Subject: [PATCH] display emojos by category
---
app.py | 50 ++++++++++++++++++++++++++++++--------------
templates/emojo.html | 30 +++++++++++++-------------
2 files changed, 49 insertions(+), 31 deletions(-)
diff --git a/app.py b/app.py
index baa1e55..4a29e7e 100644
--- a/app.py
+++ b/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("/")
def emojo(domain):
@@ -35,27 +47,33 @@ def emojo(domain):
else:
show_animated = False
+ url = urllib.parse.urlunsplit(
+ ("https", domain, "/api/v1/custom_emojis", "", "")
+ )
try:
- url = urllib.parse.urlunsplit(
- ("https", domain, "/api/v1/custom_emojis", "", "")
- )
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:
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("/robots.txt")
diff --git a/templates/emojo.html b/templates/emojo.html
index 7f67f51..85f7c13 100644
--- a/templates/emojo.html
+++ b/templates/emojo.html
@@ -5,23 +5,23 @@
{{ domain }} emojo list
click/touch to copy to clipboard
-
- {% if show_animated %}
+{% for category, emojo in categories.items() %}
+
+ {% if not category %}
+ (No category)
+ {% else %}
+ {{ category }}
+ {% endif %}
+
+
{% for emoj in emojo %}
-
-
-
- :{{ emoj.shortcode }}:
-
+
+
+
- :{{ emoj.shortcode }}:
+
{% endfor %}
- {% else %}
- {% for emoj in emojo %}
-
-
-
- :{{ emoj.shortcode }}:
-
- {% endfor %}
- {% endif %}
-
+
+{% endfor %}
{% endblock body %}
{% block script %}