-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisuda.py
315 lines (256 loc) · 9.45 KB
/
isuda.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
from flask import Flask, request, jsonify, abort, render_template, redirect, session, url_for
import MySQLdb.cursors
import redis
import hashlib
import html
import json
import math
import os
import pathlib
import random
import re
import string
import urllib
static_folder = pathlib.Path(__file__).resolve().parent.parent / 'public'
app = Flask(__name__, static_folder = str(static_folder), static_url_path='')
app.secret_key = 'tonymoris'
_config = {
'db_host': os.environ.get('ISUDA_DB_HOST', 'localhost'),
'db_port': int(os.environ.get('ISUDA_DB_PORT', '3306')),
'db_socket': os.environ.get('ISUDA_DB_SOCKET', '/var/run/mysqld/mysqld.sock'),
'db_user': os.environ.get('ISUDA_DB_USER', 'root'),
'db_password': os.environ.get('ISUDA_DB_PASSWORD', ''),
'isupam_origin': os.environ.get('ISUPAM_ORIGIN', 'http://localhost:5050'),
}
def config(key):
if key in _config:
return _config[key]
else:
raise "config value of %s undefined" % key
def redis_pool():
if hasattr(request, 'redis'):
return request.redis
# FIXME ここらへん環境変数化したい
r = redis.Redis(unix_socket_path='/var/run/redis/redis.sock')
return r
def dbh():
if hasattr(request, 'isuda_db'):
return request.isuda_db
request.isuda_db = MySQLdb.connect(**{
'unix_socket': config('db_socket'),
'user': config('db_user'),
'passwd': config('db_password'),
'db': 'isuda',
'charset': 'utf8mb4',
'cursorclass': MySQLdb.cursors.DictCursor,
'autocommit': True,
})
cur = request.isuda_db.cursor()
# cur.execute("SET SESSION sql_mode='TRADITIONAL,NO_AUTO_VALUE_ON_ZERO,ONLY_FULL_GROUP_BY'")
# cur.execute('SET NAMES utf8mb4')
return request.isuda_db
@app.teardown_request
def close_db(exception=None):
if hasattr(request, 'isuda_db'):
request.isuda_db.close()
@app.template_filter()
def ucfirst(str):
return str[0].upper() + str[-len(str) + 1:]
def set_name(func):
import functools
@functools.wraps(func)
def wrapper(*args, **kwargs):
if "user_id" in session:
request.user_id = user_id = session['user_id']
cur = dbh().cursor()
cur.execute('SELECT name FROM user WHERE id = %s', (user_id, ))
user = cur.fetchone()
if user == None:
abort(403)
request.user_name = user['name']
return func(*args, **kwargs)
return wrapper
def authenticate(func):
import functools
@functools.wraps(func)
def wrapper(*args, **kwargs):
if not hasattr(request, 'user_id'):
abort(403)
return func(*args, **kwargs)
return wrapper
@app.route('/initialize')
def get_initialize():
cur = dbh().cursor()
cur.execute('DELETE FROM entry WHERE id > 7101')
cur.execute('TRUNCATE star')
rp = redis_pool()
result = rp.get("entry_count")
if result == None:
cur.execute('SELECT COUNT(1) AS count FROM entry')
row = cur.fetchone()
result = str(row['count'], encoding='utf-8')
rp.set("entry_count", result)
return jsonify(result = 'ok')
@app.route('/')
@set_name
def get_index():
PER_PAGE = 10
page = int(request.args.get('page', '1'))
cur = dbh().cursor()
cur.execute('SELECT id, keyword, description FROM entry ORDER BY updated_at DESC LIMIT %s OFFSET %s', (PER_PAGE, PER_PAGE * (page - 1),))
entries = cur.fetchall()
for entry in entries:
# FIXME ここN+1です
entry['html'] = htmlify(entry)
entry['stars'] = load_stars(entry['keyword'])
result = int(redis_pool().get("entry_count"))
total_entries = result
last_page = int(math.ceil(total_entries / PER_PAGE))
pages = range(max(1, page - 5), min(last_page, page+5) + 1)
return render_template('index.html', entries = entries, page = page, last_page = last_page, pages = pages)
@app.route('/robots.txt')
def get_robot_txt():
abort(404)
@app.route('/keyword', methods=['POST'])
@set_name
@authenticate
def create_keyword():
keyword = request.form['keyword']
if keyword == None or len(keyword) == 0:
abort(400)
user_id = request.user_id
description = request.form['description']
if is_spam_contents(description) or is_spam_contents(keyword):
abort(400)
cur = dbh().cursor()
sql = """
INSERT INTO entry (author_id, keyword, description, created_at, updated_at)
VALUES (%s,%s,%s,NOW(), NOW())
ON DUPLICATE KEY UPDATE
author_id = %s, keyword = %s, description = %s, updated_at = NOW()
"""
cur.execute(sql, (user_id, keyword, description, user_id, keyword, description))
cur.execute("SELECT id, description from entry where description like '%" + keyword + "%'")
entries = cur.fetchall()
rp = redis_pool()
for e in entries:
rp.delete('description_{}'.format(e['id']))
rp.incr("entry_count")
return redirect('/')
@app.route('/register')
@set_name
def get_register():
return render_template('authenticate.html', action = 'register')
@app.route('/register', methods=['POST'])
def post_register():
name = request.form['name']
pw = request.form['password']
if name == None or name == '' or pw == None or pw == '':
abort(400)
user_id = register(dbh().cursor(), name, pw)
session['user_id'] = user_id
return redirect('/')
def register(cur, user, password):
salt = random_string(20)
cur.execute("INSERT INTO user (name, salt, password, created_at) VALUES (%s, %s, %s, NOW())", (user, salt, hashlib.sha1((salt + "password").encode('utf-8')).hexdigest(),))
cur.execute("SELECT LAST_INSERT_ID() AS last_insert_id")
return cur.fetchone()['last_insert_id']
def random_string(n):
return ''.join([random.choice(string.ascii_letters + string.digits) for i in range(n)])
@app.route('/login')
@set_name
def get_login():
return render_template('authenticate.html', action = 'login')
@app.route('/login', methods=['POST'])
def post_login():
name = request.form['name']
cur = dbh().cursor()
cur.execute("SELECT id, password, salt FROM user WHERE name = %s", (name, ))
row = cur.fetchone()
if row == None or row['password'] != hashlib.sha1((row['salt'] + request.form['password']).encode('utf-8')).hexdigest():
abort(403)
session['user_id'] = row['id']
return redirect('/')
@app.route('/logout')
def get_logout():
session.pop('user_id', None)
return redirect('/')
@app.route('/keyword/<keyword>')
@set_name
def get_keyword(keyword):
if keyword == '':
abort(400)
cur = dbh().cursor()
cur.execute('SELECT id, keyword, description FROM entry WHERE keyword = %s', (keyword,))
entry = cur.fetchone()
if entry == None:
abort(404)
entry['html'] = htmlify(entry)
entry['stars'] = load_stars(entry['keyword'])
return render_template('keyword.html', entry = entry)
@app.route('/keyword/<keyword>', methods=['POST'])
@set_name
@authenticate
def delete_keyword(keyword):
if keyword == '':
abort(400)
cur = dbh().cursor()
cur.execute('SELECT id FROM entry WHERE keyword = %s', (keyword, ))
row = cur.fetchone()
if row == None:
abort(404)
redis_pool().delete('description_{}'.format(row['id']))
cur.execute('DELETE FROM entry WHERE keyword = %s', (keyword,))
return redirect('/')
def htmlify(entry):
content = entry['description']
if content == None or content == '':
return ''
rp = redis_pool()
result = rp.get("description_{}".format(entry['id']))
if result != None:
return str(result, encoding='utf-8')
cur = dbh().cursor()
cur.execute('SELECT keyword FROM entry ORDER BY CHARACTER_LENGTH(keyword) DESC')
keywords = cur.fetchall()
keyword_re = re.compile("(%s)" % '|'.join([ re.escape(k['keyword']) for k in keywords]))
kw2sha = {}
def replace_keyword(m):
kw2sha[m.group(0)] = "isuda_%s" % hashlib.sha1(m.group(0).encode('utf-8')).hexdigest()
return kw2sha[m.group(0)]
result = re.sub(keyword_re, replace_keyword, content)
result = html.escape(result)
for kw, hash in kw2sha.items():
url = url_for('get_keyword', keyword = kw)
link = "<a href=\"%s\">%s</a>" % (url, html.escape(kw))
result = re.sub(re.compile(hash), link, result)
result = re.sub(re.compile("\n"), "<br />", result)
redis_pool().set('description_{}'.format(entry['id']), result)
return result
def load_stars(keyword):
cur = dbh().cursor()
cur.execute('SELECT user_name FROM star WHERE keyword = %s', (keyword,))
star = cur.fetchall()
return star
def is_spam_contents(content):
with urllib.request.urlopen(config('isupam_origin'), urllib.parse.urlencode({ "content": content }).encode('utf-8')) as res:
data = json.loads(res.read().decode('utf-8'))
return not data['valid']
return False
@app.route("/stars", methods=['POST'])
def post_stars():
keyword = request.args.get('keyword', "")
if keyword == None or keyword == "":
keyword = request.form['keyword']
cur = dbh().cursor()
cur.execute('SELECT id FROM entry WHERE keyword = %s', (keyword,))
entry = cur.fetchone()
if entry == None:
abort(404)
user = request.args.get('user', "")
if user == None or user == "":
user = request.form['user']
cur.execute('INSERT INTO star (keyword, user_name, created_at) VALUES (%s, %s, NOW())', (keyword, user))
return jsonify(result = 'ok')
if __name__ == "__main__":
app.run()