-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisutar.py
71 lines (60 loc) · 2.08 KB
/
isutar.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
from flask import Flask, request, jsonify, abort
import MySQLdb.cursors
import os
import html
import urllib
app = Flask(__name__)
def dbh():
if hasattr(request, 'db'):
return request.db
else:
request.db = MySQLdb.connect(**{
'host': os.environ.get('ISUTAR_DB_HOST', 'localhost'),
'port': int(os.environ.get('ISUTAR_DB_PORT', '3306')),
'user': os.environ.get('ISUTAR_DB_USER', 'root'),
'passwd': os.environ.get('ISUTAR_DB_PASSWORD', ''),
'db': 'isutar',
'charset': 'utf8mb4',
'cursorclass': MySQLdb.cursors.DictCursor,
'autocommit': True,
})
cur = request.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.db
@app.teardown_request
def close_db(exception=None):
if hasattr(request, 'db'):
request.db.close()
@app.route("/initialize")
def get_initialize():
cur = dbh().cursor()
cur.execute('TRUNCATE star')
return jsonify(status = 'ok')
@app.route("/stars")
def get_stars():
cur = dbh().cursor()
cur.execute('SELECT * FROM star WHERE keyword = %s', (request.args['keyword'], ))
return jsonify(stars = cur.fetchall())
@app.route("/stars", methods=['POST'])
def post_stars():
keyword = request.args.get('keyword', "")
if keyword == None or keyword == "":
keyword = request.form['keyword']
origin = os.environ.get('ISUDA_ORIGIN', 'http://localhost:5000')
url = "%s/keyword/%s" % (origin, urllib.parse.quote(keyword))
try:
urllib.request.urlopen(url)
except urllib.error.HTTPError as e:
if e.status == 404:
abort(404)
else:
raise
cur = dbh().cursor()
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()