-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathask.py
290 lines (222 loc) · 7.56 KB
/
ask.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
from flask import (
Flask, request, render_template, send_from_directory, abort, redirect)
from flask_sqlalchemy import SQLAlchemy
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
# from flask_migrate import Migrate
from mastodon import Mastodon
import re
import random
import string
import datetime
from dateutil.tz import tzlocal
from config import Config
app = Flask(__name__)
app.config.from_object(Config)
BOT_NAME = Config.BOT_NAME
MAST_URL = Config.MAST_URL
WORK_URL = Config.WORK_URL
TOKEN = Config.TOKEN
CLIENT_ID = Config.CLIENT_ID
CLIENT_SEC = Config.CLIENT_SEC
MENTION_BOT_TEMP = re.compile(
r'<span class=\"h-card\">'
'<a [^>]*href=\"{}/{}\"[^>]*>'
'@<span>.*?</span>'
'</a></span>'.format(MAST_URL, BOT_NAME)
)
DELETE_TEMP = re.compile(r'<p>\s*删除\s*</p>')
REDIRECT_URI = WORK_URL + '/askMe/auth'
th = Mastodon(
access_token=TOKEN,
api_base_url=MAST_URL
)
limiter = Limiter(
key_func=get_remote_address,
app=app,
default_limits=["50 / minute"],
)
db = SQLAlchemy(app)
# migrate = Migrate(app, db)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
acct = db.Column(db.String(64))
disp = db.Column(db.String(64))
avat = db.Column(db.String(256))
url = db.Column(db.String(128))
secr = db.Column(db.String(16))
root = db.Column(db.BigInteger)
def __init__(self, acct):
self.acct = acct
def __repr__(self):
return f"@{self.acct}[{self.disp}]"
class Question(db.Model):
id = db.Column(db.Integer, primary_key=True)
acct = db.Column(db.String(64))
content = db.Column(db.String(400))
time = db.Column(db.DateTime)
toot = db.Column(db.BigInteger)
def __init__(self, acct, content, toot):
self.acct = acct
self.content = content
self.toot = toot
self.time = datetime.datetime.now()
def __repr__(self):
return f"[{self.acct}]{self.content}({self.toot})"
@app.route('/askMe/js/<path:path>')
def send_js(path):
return send_from_directory('static/js', path)
@app.route('/askMe/img/<path:path>')
def send_img(path):
return send_from_directory('static/img', path)
@app.route('/askMe/')
def root():
return app.send_static_file('index.html')
@app.route('/askMe/footer.html')
def root_footer():
return app.send_static_file('footer.html')
@app.route('/askMe/auth')
@limiter.limit("10 / minute")
def set_inbox_auth():
code = request.args.get('code')
autoSend = request.args.get('autoSend', '')
secr = request.args.get('secr', '')
if secr and not re.match('[a-z]{0,16}', secr):
abort(422)
client = Mastodon(
client_id=CLIENT_ID,
client_secret=CLIENT_SEC,
api_base_url=MAST_URL
)
client.log_in(
code=code,
redirect_uri=(
'{}?autoSend={}&secr={}'
).format(REDIRECT_URI, autoSend, secr),
scopes=[
'read:accounts',
'write:statuses'
] if autoSend else ['read:accounts']
)
info = client.account_verify_credentials()
acct = info.acct
u = User.query.filter_by(acct=acct).first()
if not u:
u = User(acct)
db.session.add(u)
u.secr = secr or u.secr or ''.join(random.choice(
string.ascii_lowercase) for i in range(16))
u.disp = info.display_name
u.url = info.url
u.avat = info.avatar
if autoSend:
client.status_post(
f"[自动发送] 我创建了一个匿名提问箱,欢迎提问~\n"
f"{WORK_URL}/askMe/{acct}/{u.secr}",
visibility='public')
db.session.commit()
return redirect(f"/askMe/{acct}/{u.secr}")
@app.route('/askMe/inbox', methods=['POST'])
@limiter.limit("10 / minute")
def set_inbox():
acct = request.form.get('username')
if not re.match('[A-Za-z0-9_]{1,30}(@[a-z\\.-_]+)?', acct):
return 'id', 422
r = th.conversations()
for conv in r:
status = conv.last_status
account = status.account
if acct == account.acct:
pt = status.content.strip()
x = re.findall(r'新建(\[[a-z]{1,32}\])?', pt)
if not x:
return '私信格式无效,请检查并重新发送', 422
secr = x[0][1:-1] if x[0] else ''.join(
random.choice(string.ascii_lowercase) for i in range(16))
u = User.query.filter_by(acct=acct).first()
if not u:
u = User(acct)
db.session.add(u)
u.disp = account.display_name
u.url = account.url
u.avat = account.avatar
u.secr = secr
db.session.commit()
th.status_post(
f"@{acct} 设置成功! 当前提问箱链接 "
f"{WORK_URL}/askMe/{acct}/{secr}\n"
f"(如需在微信等无链接预览的平台分享,建议先发给自己,"
"点开,再点击分享到朋友圈等)",
in_reply_to_id=status.id,
visibility='direct'
)
return acct + '/' + secr
return '未找到私信,请确认已发送且是最近发送', 404
@app.route('/askMe/<acct>/<secr>/')
def inbox(acct, secr):
u = User.query.filter_by(acct=acct, secr=secr).first_or_404()
qs = [{
'content': q.content,
'toot': q.toot,
'time': q.time.replace(tzinfo=tzlocal())
} for q in Question.query.filter_by(acct=acct).all()
]
return render_template('inbox.html', acct=u.acct, disp=(
u.disp or u.acct), url=u.url, avat=u.avat, qs=qs)
@app.route('/askMe/<acct>/<secr>/new', methods=['POST'])
@limiter.limit("50 / hour; 1 / 2 second")
def new_question(acct, secr):
u = User.query.filter_by(acct=acct, secr=secr).first()
if not u:
abort(404)
content = request.form.get('question')
if not content or len(content) > 400:
abort(422)
if not Question.query.filter_by(acct=acct, content=content).first():
if not u.root:
toot = th.status_post(
f"@{acct} 欢迎使用匿名提问箱。"
"未来的新提问会集中显示在这里,方便管理。",
visibility='direct')
u.root = toot.id
toot = th.status_post(
f"@{acct} 叮~ 有新提问:\n\n{content}",
in_reply_to_id=u.root,
visibility='direct'
)
q = Question(acct, content, toot.id)
db.session.add(q)
db.session.commit()
return redirect(".")
def render_content(text, emojis):
text = MENTION_BOT_TEMP.sub('', text)
for emoji in emojis:
text = text.replace(
':%s:' % emoji.shortcode,
'<img class="emoji" src="%s">' % emoji.url
)
return text
@app.route('/askMe/<acct>/<secr>/<int:toot>')
def question_info(acct, secr, toot):
q = Question.query.filter_by(acct=acct, toot=toot).first()
if not q or not User.query.filter_by(acct=acct, secr=secr).first():
abort(404)
context = th.status_context(toot)
replies = [
{
'disp': (t.account.display_name or t.account.acct),
'url': t.account.url,
'content': render_content(t.content, t.emojis),
'time': str(t.created_at),
'media': t.media_attachments,
}
for t in context.descendants
]
if replies and DELETE_TEMP.match(replies[-1].get('content')):
db.session.delete(q)
db.session.commit()
th.status_delete(toot)
return '该提问已被删除', 404
return {'replies': replies}
if __name__ == '__main__':
app.run(debug=True)