avatar id retriever

This commit is contained in:
Xevion
2019-09-20 17:18:54 -05:00
parent 644db6f8a1
commit 595823aaae
2 changed files with 19 additions and 0 deletions

1
.gitignore vendored
View File

@@ -11,3 +11,4 @@ key
/venv/*
/app/__pycache__/*
/__pycache__/*
app/static/token.dat

View File

@@ -18,6 +18,7 @@ import faker
import json
import pprint
import os
import sys
print = pprint.PrettyPrinter().pprint
fake = faker.Faker()
@@ -37,6 +38,23 @@ def modpacks():
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route('/avatar/')
@app.route('/avatar/<id>/')
@app.route('/avatar/<id>')
def getAvatar(id=''):
# Constants
token = open(os.path.join(sys.path[0], 'app', 'static', 'token.dat'), 'r').read()
headers = {'Authorization' : f'Bot {token}'}
api = "https://discordapp.com/api/v6/users/{}"
cdn = "https://cdn.discordapp.com/avatars/{}/{}.png"
# Get User Data which contains Avatar Hash
response = requests.get(api.format(id), headers=headers)
if response.status_code != 200:
return response.text
user = json.loads(response.text)
url = cdn.format(id, user['avatar'])
return "<img src=\"{}\">".format(url)
@app.errorhandler(401)
def unauthorized(e):
return redirect(url_for('login'))