-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
160 lines (117 loc) · 4.67 KB
/
app.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
import os
from flask import Flask, jsonify, request, url_for, redirect
from flaskext.mysql import MySQL
import pymysql
from configparser import ConfigParser
import datetime
config = ConfigParser()
config.read(os.path.join(os.path.dirname(__file__), 'doodledoo.ini'))
app = Flask(__name__)
app.config['MYSQL_DATABASE_HOST'] = config.get('doodledoo', 'dbhost')
app.config['MYSQL_DATABASE_USER'] = config.get('doodledoo', 'dbuser')
app.config['MYSQL_DATABASE_PASSWORD'] = config.get('doodledoo', 'dbpassword')
app.config['MYSQL_DATABASE_DB'] = config.get('doodledoo', 'dbname')
MAXFETCH = config.getint('doodledoo', 'maxfetch')
MINCOMPLAIN = config.getint('doodledoo', 'mincomplain')
MAXUSERCOMPLAIN = config.getint('doodledoo', 'maxusercomplain')
mysql = MySQL()
mysql.init_app(app)
def fetchOneDict(cursor):
data = cursor.fetchone()
if data is None:
return None
desc = cursor.description
d = {}
for (name, value) in zip(desc, data):
d[name[0]] = value
return d
@app.route('/check')
def check():
try:
mysql.connect()
except:
return jsonify({"error": "internal server error"})
return jsonify({"error": None})
@app.route('/')
def root():
return redirect(url_for('check'))
@app.route('/<target>/complain')
def complain(target):
if type(target) != str or len(target) != 2:
return jsonify({"error": "no locale given"})
con = mysql.connect()
cursor = con.cursor()
sender = request.remote_addr
cursor.execute("SELECT id FROM complaints WHERE sender=%s", (sender))
cs = cursor.fetchall()
if cs is not None and len(cs) >= MAXUSERCOMPLAIN:
return jsonify({"error": "maximum complaints exceeded"})
word = request.args.get('word')
reason = request.args.get('reason')
if word is None or reason is None:
return jsonify({"error": "invalid parameters"})
cursor.execute("SELECT id from languages WHERE locale=%s AND isready=1",
(target))
language = fetchOneDict(cursor)
if language is None:
return jsonify({"error": "locale not found"})
cursor.execute("SELECT id FROM translations WHERE language=%s AND "
"txt=%s", (language["id"], word))
trans = fetchOneDict(cursor)
if not trans:
return jsonify({"error": "word not found"})
try:
cursor.execute("INSERT INTO complaints (translation, reason, sender, "
"time) VALUES (%s, %s, %s, %s)", (trans["id"], reason,
sender, datetime.
datetime.now()))
con.commit()
except pymysql.err.IntegrityError:
pass
return jsonify({"error": None})
@app.route('/<target>/<int:count>')
def index(target, count):
if type(target) != str or len(target) != 2:
return jsonify({"error": "no locale given"})
con = mysql.connect()
cursor = con.cursor()
cursor.execute("SELECT id, lastseen from sendercache WHERE addr=%s",
(request.remote_addr))
sender = fetchOneDict(cursor)
now = datetime.datetime.now()
if sender is None:
cursor.execute("INSERT INTO sendercache (addr, lastseen) VALUES "
"(%s, %s)", (request.remote_addr, now))
con.commit()
else:
if sender["lastseen"] + datetime.timedelta(seconds=10) > now:
return jsonify({"error": "requests in timedelta exceeded"})
else:
cursor.execute("UPDATE sendercache SET lastseen = %s WHERE id=%s",
(now, sender["id"]))
con.commit()
cursor.execute("SELECT id, issource, isready from languages "
"WHERE locale=%s", (target))
language = fetchOneDict(cursor)
if language is None:
cursor.execute("INSERT INTO languages (locale, issource, isready) "
"VALUES (%s, 0, 0)", (target))
con.commit()
return jsonify({"error": "language has been requested, try again "
"in a few minutes"})
elif language["isready"] == 0:
return jsonify({"error": "language has been requested, try again "
"in a few minutes"})
else:
if count > MAXFETCH:
count = MAXFETCH
cursor.execute("SELECT txt FROM translations WHERE language=%s "
"AND active=1 ORDER BY RAND() LIMIT %s",
(language["id"], count))
data = cursor.fetchall()
if data is None:
return jsonify({"error": "internal server error"})
else:
return jsonify({"error": None, "words": data})
if __name__ == "__main__":
app.run(host='::')