diff --git a/emojos.py b/emojos.py index dafc6da..d946993 100644 --- a/emojos.py +++ b/emojos.py @@ -22,6 +22,8 @@ from flask import Flask, redirect, render_template, request, url_for app = Flask(__name__) +class ForbiddenEndpointError(Exception): + pass @app.route('/') def emojo(domain): @@ -36,14 +38,20 @@ def emojo(domain): try: url = urllib.parse.urlunsplit(('https', domain, '/api/v1/custom_emojis', '', '')) + response = requests.get(url) + if response.status_code == 401: + raise ForbiddenEndpointError + if show_all: - emojo = sorted(requests.get(url).json(), key=operator.itemgetter('shortcode')) + emojo = sorted(response.json(), key=operator.itemgetter('shortcode')) else: - emojo = sorted(filter(lambda x: x.get('visible_in_picker', True), requests.get(url).json()), + 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 as e: return render_template('oh_no.html', domain=domain) + except ForbiddenEndpointError: + return render_template('forbidden.html', domain=domain) @app.route('/favicon.ico') diff --git a/templates/forbidden.html b/templates/forbidden.html new file mode 100644 index 0000000..e0433b6 --- /dev/null +++ b/templates/forbidden.html @@ -0,0 +1,5 @@ +{% extends "base.html" %} +{% block title %}Custom emoji list for {{ domain }}{% endblock title %} +{% block body %} +

{{ domain }} doesn't allow access to the v1/custom_emojis endpoint. :(

+{% endblock body %}