-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmravatar.py
More file actions
107 lines (90 loc) · 3.54 KB
/
Copy pathmravatar.py
File metadata and controls
107 lines (90 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from flask import Flask, redirect, request, Response, send_file, jsonify
from requests_cache import CachedSession
import requests_cache
import json
import re
import requests
app = Flask(__name__)
# Setup Requests Cache
persistent = CachedSession(
'persistent',
backend=requests_cache.backends.sqlite.SQLiteCache('/tmp/persistent.sqlite'),
allowable_methods=['GET'],
allowable_codes=[200],
stale_if_error=True,
)
session = CachedSession(
'cache',
backend=requests_cache.backends.sqlite.SQLiteCache('/tmp/cache.sqlite'),
expire_after=10800,
allowable_methods=['GET'],
allowable_codes=[200],
stale_if_error=True,
)
# /avatar/<user> entrypoint
@app.route("/avatar/<user>")
def req_avatar(user):
default_img_url = "https://cdn.jsdelivr.net/gh/mastodon/mastodon@latest/public/avatars/original/missing.png"
if request.args.get('default'):
default_img_url = requests.utils.unquote(request.args.get('default'))
# Validate Username
if re.search('@[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+',user):
webfinger_url = "https://"+re.split('@',user[1:])[1]+"/.well-known/webfinger?resource=acct:"+user[1:]
elif re.search('[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+',user):
webfinger_url = "https://"+re.split('@',user)[1]+"/.well-known/webfinger?resource=acct:"+user
else:
app.logger.error("%s is invalid.", user)
return redirect(default_img_url, code=302)
# Find account url
try:
webfinger = persistent.get(webfinger_url)
except Exception as e:
app.logger.error("%s is not accessable.", webfinger_url)
return redirect(default_img_url, code=302)
if webfinger.ok:
user_url = json.loads(webfinger.text)['aliases'][1]+".json"
else:
app.logger.error("%s is not accessable.", webfinger_url)
return redirect(default_img_url, code=302)
if request.args.get('no-cache') == 'true':
requests_cache.backends.sqlite.SQLiteCache('/tmp/cache.sqlite').delete(urls=[user_url])
# Get User Info
try:
user_json = session.get(user_url)
except Exception as e:
app.logger.error("%s is not accessable.", user_url)
return redirect(default_img_url, code=302)
if user_json.ok:
if "icon" in json.loads(user_json.text):
img_url = json.loads(user_json.text)['icon']['url']
else:
img_url = default_img_url
else:
app.logger.error("%s is not accessable.", user_url)
return redirect(default_img_url, code=302)
# Proxy Images
if request.args.get('proxied') == 'true':
if request.args.get('no-cache') == 'true':
requests_cache.backends.sqlite.SQLiteCache('/tmp/cache.sqlite').delete(urls=[img_url])
try:
img_resp = session.get(img_url)
except Exception as e:
app.logger.error("%s inaccessable.", img_url)
return redirect(img_url, code=302)
if img_resp.ok:
img = Response(img_resp.content, img_resp.status_code, {'Content-Type': img_resp.headers['Content-Type'], 'Cache-Control': 'max-age=3600'})
return img
else:
app.logger.error("%s inaccessable.", img_url)
return redirect(img_url, code=302)
else:
return redirect(img_url, code=302)
@app.route('/')
def serve_index():
return send_file('./docs/index.html')
@app.route('/README.md')
def serve_doc():
return send_file('./docs/README.md')
@app.route('/health', methods=['GET'])
def health_check():
return jsonify({'status': 'healthy'}), 200