-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
157 lines (138 loc) · 4.88 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
import json
import math
import time
import web
import leancloud
from leancloud import User
from grabber import translate_baidu
from util import get_hint
app = web.application()
TryWords = leancloud.Object.extend('TryWords')
GetWords = leancloud.Object.extend('GetWords')
def diac_trim(text):
ulist = [u for u in text if ord(u) < 128]
return ''.join(ulist)
class Home:
def GET(self):
it = web.input()
text = it.get('text', '')
puretext = diac_trim(text)
context = it.get('context', '')
ret = ['<a href="https://diac.leanapp.cn/static/add.html?word=%s&context=%s&explain=%s" target="_blank">%s</a>' % (text, context, x[0], x[0]) for x in translate_baidu(puretext)]
return '; '.join(ret)
class Login:
def POST(self):
it = web.input()
username = it.get('username', '')
password = it.get('password', '')
user = User()
try:
user.login(username, password)
token = user.get_session_token()
web.setcookie('token', token)
return {'code': 0, 'token': token}
except leancloud.errors.LeanCloudError:
return {'code': 1}
class AddWord:
def POST(self):
it = web.input()
try:
word = it.get('word')
context = it.get('context')
expl = it.get('expl')
token = it.get('token')
now = int(it.get('now', time.time()))
user = User().become(token)
except leancloud.errors.LeanCloudError:
return {'code': 1}
delay = math.exp(math.log(30) / (user.get('avgGetCount') - 0.1)) * 86400
recall_day = int((now + 8 * 3600 + delay) / 86400)
obj = TryWords()
obj.set('word', word)
obj.set('context', context)
obj.set('userId', user.id)
obj.set('expl', expl)
obj.set('delay', delay)
obj.set('tryCount', 0)
obj.set('recallDay', recall_day)
obj.set('startTime', now)
obj.save()
return {'code': 0}
class TestWord:
def POST(self):
it = web.input()
try:
grade = it.get('grade')
token = it.get('token')
tryId = it.get('tryId')
now = int(it.get('now', time.time()))
user = User().become(token)
except leancloud.errors.LeanCloudError:
return {'code': 1}
obj = TryWords.query.get(tryId)
delay = obj.get('delay')
tryCount = obj.get('tryCount')
if grade == 'c':
delay = delay / 2.0
elif grade == 'b':
delay = delay / 1.1
elif grade == 'a':
if delay > 30 * 86400 - 1:
getObj = GetWords()
getObj.set('userId', user.id)
getObj.set('wordAndExpl', obj.get('word')+'|'+obj.get('expl'))
getObj.set('tryCount', obj.get('tryCount') + 1)
getObj.set('totalTime', now - obj.get('startTime'))
getObj.save()
obj.destroy()
return {'code': 0}
delay *= math.exp(math.log(30) / (user.get('avgGetCount') - 0.1))
delay = min(delay, 40 * 86400)
recall_day = int((now + 8 * 3600 + delay) / 86400)
obj.set('delay', delay)
obj.set('tryCount', tryCount + 1)
obj.set('recallDay', recall_day)
obj.save()
return {'code': 0}
class ListWord:
def GET(self):
it = web.input()
try:
now = int(it.get('now', time.time()))
token = it.get('token')
user = User().become(token)
except leancloud.errors.LeanCloudError:
return {'code': 1}
today = int((now + 8 * 3600) / 86400)
cnt = TryWords.query.less_than_or_equal_to("recallDay", today).count()
if cnt > 0:
obj = TryWords.query.less_than_or_equal_to("recallDay", today).limit(1).first()
hint = get_hint(obj.get("word"))
return {"code": 0,
"count": cnt,
"testId": obj.id,
"word": obj.get("word").lower(),
"context": obj.get("context"),
"explain": obj.get("expl"),
"tryCount": obj.get("tryCount"),
"delay": obj.get("delay"),
"hint": hint,
}
else:
return {"code": 0, "count": cnt}
def all_wrapper(laber):
ret = laber()
web.header('Access-Control-Allow-Origin', '*')
if isinstance(ret, dict):
web.header('Content-Type', 'application/json')
return json.dumps(ret)
return ret
app.add_mapping("/btrans", Home)
app.add_mapping("/login", Login)
app.add_mapping("/word/add", AddWord)
app.add_mapping("/word/test", TestWord)
app.add_mapping("/word/list", ListWord)
app.add_processor(all_wrapper)
if __name__ == "__main__":
print app.request("/btrans/satirised").data
#app.run()