Merge pull request #15 from renatolond/improve_error_for_authorized_fetch

Improve error for instances with authorized fetch enabled
This commit is contained in:
iliana etaoin 2021-02-11 17:14:20 -08:00 committed by GitHub
commit 3a04fd752f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View File

@ -22,6 +22,8 @@ from flask import Flask, redirect, render_template, request, url_for
app = Flask(__name__)
class ForbiddenEndpointError(Exception):
pass
@app.route('/<domain>')
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')

5
templates/forbidden.html Normal file
View File

@ -0,0 +1,5 @@
{% extends "base.html" %}
{% block title %}Custom emoji list for {{ domain }}{% endblock title %}
{% block body %}
<p><b>{{ domain }}</b> doesn't allow access to the <code>v1/custom_emojis</code> endpoint. :(</p>
{% endblock body %}