forked from ajdavis/proporti.onl
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
352 lines (293 loc) · 11 KB
/
server.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import logging
import os
from urllib.parse import urlparse
import requests
import webfinger
from authlib.integrations.flask_client import (
OAuth,
OAuthError,
) # pip install Authlib
from flask import ( # pip install Flask
Flask,
flash,
redirect,
render_template,
request,
session,
url_for,
)
from mastodon import Mastodon, MastodonNetworkError, MastodonNotFoundError
from wtforms import Form, SelectField, StringField # pip install WTForms
from werkzeug.middleware.proxy_fix import ProxyFix
from analyze import (
Cache,
analyze_followers,
analyze_following,
analyze_timeline,
div,
dry_run_analysis,
get_mastodon_api,
get_user_from_handle,
parse_mastodon_handle,
get_following_lists,
)
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
APP_NAME = "mastodon-gender-distribution"
DEPLOY_URL = os.environ.get("DEPLOY_URL", "http://127.0.0.1:8000")
TRACKING_ID = os.environ.get("TRACKING_ID")
app = Flask(APP_NAME)
app.config["SECRET_KEY"] = os.environ["COOKIE_SECRET"]
app.config["DRY_RUN"] = False
app.config["PREFERRED_URL_SCHEME"] = "https"
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)
oauth = OAuth(app)
@app.route("/login")
def login():
# Get handle from login field
handle = request.args.get("handle", "alexkalopsia@mastodon.social")
# Look for actual instance url using webfinger request. This is done because
# a user username@instance.name could exist in an instance that is located
# on subdomain.instance.name
resource = f"acct:{handle}"
webfinger_data = webfinger.finger(resource)
profile_url = next(
(
link["href"]
for link in webfinger_data.get("links", [])
if link.get("rel") == "self"
),
None,
)
parsed_url = urlparse(profile_url)
instance = parsed_url.hostname
metadata = None
try:
response = requests.get(
f"https://{instance}/.well-known/oauth-authorization-server"
)
response.raise_for_status()
metadata = response.json()
except requests.exceptions.RequestException as e:
print(f"Failed to fetch metadata: {e}")
token_endpoint = (
metadata.get("token_endpoint")
if metadata
else f"https://{instance}/oauth/token"
)
auth_endpoint = (
metadata.get("authorization_endpoint")
if metadata
else f"https://{instance}/oauth/authorize"
)
scopes = ["read:accounts", "read:statuses", "read:lists", "read:follows"]
print(f"DEPLOY TO: {DEPLOY_URL}")
redirect_uri = url_for(
"oauth_authorized",
_external=True,
)
client_id, client_secret = Mastodon.create_app(
"mastodon-gender-distribution",
api_base_url=f"https://{instance}",
redirect_uris=[redirect_uri],
scopes=scopes,
)
session["client_id"] = client_id
session["client_secret"] = client_secret
session["instance"] = instance
# Store user-facing handle instance
_, handle_instance = parse_mastodon_handle(handle)
session["handle_instance"] = handle_instance
oauth.register(
name=instance,
client_id=session["client_id"],
client_secret=session["client_secret"],
api_base_url=f"https://{instance}/api/v1",
access_token_url=token_endpoint,
authorize_url=auth_endpoint,
client_kwargs={"scope": " ".join(scopes)},
fetch_token=lambda: session.get(
"mastodon_token"
), # DON'T DO IT IN PRODUCTION
)
client = oauth.create_client(instance)
return client.authorize_redirect(redirect_uri)
@app.route("/logout")
def logout():
session.clear()
flash("Logged out.")
return redirect("/")
@app.errorhandler(OAuthError)
def handle_error(error):
flash("You denied the request to sign in.")
return redirect("/")
@app.route("/authorized")
def oauth_authorized():
# Get existing oauth client
instance = session["instance"]
client = oauth.create_client(instance)
token = client.authorize_access_token()
response = client.get(
f"https://{instance}/api/v1/accounts/verify_credentials"
)
try:
profile = response.json()
except ValueError as e:
print("JSON decode error:", e)
return "Failed to decode JSON response"
# Update handle to user-facing instance, as opposed to the
# one obtained via webfinger
username = profile["username"]
handle_instance = session["handle_instance"]
handle = f"{username}@{handle_instance}"
session["mastodon_user"] = handle
session["mastodon_token"] = token["access_token"]
try:
session["lists"] = get_following_lists(
profile["id"],
token["access_token"],
instance,
)
except Exception:
app.logger.exception("Error in get_following_lists, ignoring")
session["lists"] = []
return redirect(url_for("index"))
class LoginForm(Form):
login_acct = StringField("Mastodon user handle")
class AnalyzeForm(Form):
analyze_acct = StringField("Mastodon user handle")
lst = SelectField("List")
@app.route("/", methods=["GET", "POST"])
def index():
tok = session.get("mastodon_token")
results = {}
list_name = list_id = error = form = None
if request.method == "GET":
if session.get("mastodon_user"):
form = AnalyzeForm()
form.lst.choices = [("none", "No list")]
# Populate lists if there are any stored in the session
if session.get("lists"):
form.lst.choices += [
(str(list["id"]), list["name"])
for list in session["lists"]
]
else:
form = LoginForm()
elif request.method == "POST":
form_type = request.form.get("form_type")
if form_type == "login":
form = LoginForm(request.form)
if form.validate():
handle = form.login_acct.data
session["mastodon_user"] = handle
print(f"Validated as {handle}")
flash(f"Logged in successfully as {handle}!", "success")
return redirect(url_for("index"))
elif form_type == "analyze":
form = AnalyzeForm(request.form)
form.lst.choices = [("none", "No list")]
if session.get("lists"):
form.lst.choices += [
(str(list["id"]), list["name"])
for list in session["lists"]
]
# We take the form handle, and replace the instance with
# the correct one obtained with webfinger
handle = form.analyze_acct.data
username = handle.split("@")[0]
instance = session["instance"]
handle = f"{username}@{instance}"
session_user = session.get("mastodon_user")
different_user = form.analyze_acct.data != session_user
if form.validate() and form.analyze_acct.data:
if app.config["DRY_RUN"]:
list_name = None
following, followers, timeline = dry_run_analysis()
results = {
"following": following,
"followers": followers,
"timeline": timeline,
}
else:
# Get selected list
if (
session.get("lists")
and form.lst
and form.lst.data != "none"
):
list_id = int(form.lst.data)
list_name = [
list["name"]
for list in session["lists"]
if int(list["id"]) == list_id
][0]
try:
_, instance = parse_mastodon_handle(handle)
api = get_mastodon_api(tok, instance)
cache = Cache()
user = get_user_from_handle(handle, api)
if user:
if different_user and user.indexable is False:
raise Exception(
f"User {form.analyze_acct.data} is not indexable.\n"
f"If the account is yours, you can change the setting on: "
f"Settings > Public profile > Privacy and reach > "
f"Include public posts in search results"
)
results = {
"following": analyze_following(
user.id, list_id, api, cache
),
"followers": analyze_followers(
user.id, api, cache
),
"timeline": analyze_timeline(
user.id, list_id, api, cache
),
}
for key, value in results.items():
if not value:
raise Exception(
f"Failed to fetch results "
f"for user {handle}."
)
else:
raise Exception(f"Failed to find user {handle}.")
except Exception as exc:
import traceback
traceback.print_exc()
if isinstance(exc, MastodonNotFoundError):
error = f"Could not find user {form.analyze_acct.data}."
elif isinstance(exc, MastodonNetworkError):
error = (
f"Could not connect to the Mastodon server {instance}.\n"
"Please check the instance name or try again later."
)
else:
error = exc
if error is not None:
error = str(error).replace("\n", "<br>")
pass
return render_template(
"index.html",
form=form,
results=results,
error=error,
div=div,
list_name=list_name,
TRACKING_ID=TRACKING_ID,
)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--debug", action="store_true")
parser.add_argument("--dry-run", action="store_true")
parser.add_argument("port", nargs="?", type=int)
args = parser.parse_args()
if args.port is None:
port = int(os.environ.get("PORT", 8000))
else:
port = args.port
app.config["DRY_RUN"] = args.dry_run
app.run(port=port, debug=args.debug)