forked from CaiMiao/sparklebox-schinese
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_endpoints.py
339 lines (283 loc) · 11.4 KB
/
api_endpoints.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import tornado.web
import tornado.template
import tornado.escape
from dispatch import *
import os
import json
import starlight
import hashlib
import base64
import time
import pytz
import itertools
import enums
from datetime import datetime, timedelta
def tlable_make_assr(text):
if not text:
return "@!@!"
else:
salt = os.getenv("TLABLE_SALT").encode("utf8")
return base64.b64encode(hashlib.sha256(text.encode("utf8") + salt).digest()).decode("utf8")
def tlable(text, write=1):
text = text.replace("\n", " ")
if write:
return """<span class="tlable" data-summertriangle-assr="{1}">{0}</span>""".format(
tornado.escape.xhtml_escape(text), tlable_make_assr(text))
else:
return """<span class="tlable">{0}</span>""".format(
tornado.escape.xhtml_escape(text))
@route("/api/v1/read_tl")
class TranslateReadAPI(tornado.web.RequestHandler):
""" Queries database for cs translation entries """
@tornado.web.asynchronous
def post(self):
try:
load = json.loads(self.request.body.decode("utf8"))
except ValueError:
self.set_status(400)
return self.finish()
else:
if not isinstance(load, list):
self.set_status(400)
return self.finish()
unique = list(set(load))
if not unique:
self.set_header("Content-Type", "application/json; charset=utf-8")
self.write("{}")
self.finish()
self.settings["tle"].translate(self.complete, *unique)
def complete(self, ret):
from_db = {tlo.key: tlo.english for tlo in ret if tlo.english != tlo.key}
self.set_header("Content-Type", "application/json; charset=utf-8")
self.write(json.dumps(from_db))
self.finish()
@route("/api/v1/send_tl")
class TranslateWriteAPI(tornado.web.RequestHandler):
""" Save a contributed string to database.
A security token is present to prevent spamming of random keys,
but otherwise all strings will be accepted. """
BAD_WORDS = ["undefined", "null", ""]
def post(self):
try:
load = json.loads(self.request.body.decode("utf8"))
except ValueError:
self.set_status(400)
return
key = load.get("key", "")
s = load.get("tled", "").strip()
assr = load.get("security")
#print(key, s, assr)
if not (key and s and assr) or tlable_make_assr(key) != assr:
self.set_status(400)
return
if s in self.BAD_WORDS:
self.set_status(400)
return
# ** resets the string
if s == "**":
s = key
self.settings["tle"].set_translation(
load.get("key"), s, self.request.remote_ip)
self.settings["analytics"].analyze_request(self.request, self.__class__.__name__,
{"key": key, "value": s})
class objproxy(object):
def __init__(self, d):
self.d = d
def __getattr__(self, key):
return self.d[key]
def extend_skill(self, d):
d["explain_en"] = starlight.en.describe_skill(objproxy(d))
d["skill_type"] = enums.skill_type(d["skill_type"])
duration_table = d["dur"].args[0]
probabil_table = d["chance"].args[0]
d["effect_length"] = [duration_table[d["available_time_type"]].available_time_min,
duration_table[d["available_time_type"]].available_time_max]
del d["available_time_type"]
d["proc_chance"] = [probabil_table[d["probability_type"]].probability_min,
probabil_table[d["probability_type"]].probability_max]
del d["probability_type"]
def extend_lead_skill(self, d):
d["explain_en"] = starlight.en.describe_lead_skill(objproxy(d))
d["target_attribute"] = enums.lskill_target_attr(d["target_attribute"])
d["target_param"] = enums.lskill_target_param(d["target_param"])
def extend_card(self, d):
d["rarity"] = d["rarity_dep"]
del d["rarity_dep"]
d["spread_image_ref"] = "/".join((self.settings["image_host"], "spread", "{0}.png".format(d["id"]))) \
if d["has_spread"] else None
d["card_image_ref"] = "/".join((self.settings["image_host"], "card", "{0}.png".format(d["id"])))
d["sprite_image_ref"] = "/".join((self.settings["image_host"], "chara2", str(d["chara_id"]), "{0}.png".format(d["pose"])))
d["attribute"] = enums.api_char_type(d["attribute"])
def extend_char(self, d):
d["type"] = enums.api_char_type(d["type"])
EXTEND_FUNC = {"skill_data_t": extend_skill,
"leader_skill_data_t": extend_lead_skill,
"card_data_t": extend_card,
"chara_data_t": extend_char}
KEY_BLACKLIST = {
"skill_data_t": ["chance", "dur"]
}
class APIUtilMixin(object):
@staticmethod
def stub_object(obj):
name_to_api = {"chara_data_t": "char_t",
"skill_data_t": "skill_t",
"leader_skill_data_t": "leader_skill_t",
"card_data_t": "card_t"}
get_spec = {"chara_data_t": lambda x: x.chara_id}
typename = obj.__class__.__name__
if typename in name_to_api:
return {"ref": "/api/v1/{0}/{1}".format(
name_to_api.get(typename),
get_spec.get(typename, lambda x: x.id)(obj))}
def fix_namedtuples(self, original_typename, d, cfg):
for key in d.keys():
if hasattr(d[key], "_asdict"):
if cfg["stubs"] == "yes":
stub = self.stub_object(d[key])
else:
stub = None
d[key] = stub or self.fix_namedtuples(d[key].__class__.__name__, d[key]._asdict(), cfg)
elif isinstance(d[key], dict):
d[key] = self.fix_namedtuples(d[key].__class__.__name__, d[key], cfg)
elif isinstance(d[key], list):
d[key] = list(self.fix_namedtuples_list(d[key], cfg))
EXTEND_FUNC.get(original_typename, lambda self, x: None)(self, d)
for key in KEY_BLACKLIST.get(original_typename, []):
del d[key]
return d
def fix_namedtuples_list(self, alist, cfg):
for obj in alist:
if hasattr(obj, "_asdict"):
if cfg["stubs"] == "yes":
stub = self.stub_object(obj)
else:
stub = None
yield stub or self.fix_namedtuples(obj.__class__.__name__, obj._asdict(), cfg)
elif isinstance(obj, dict):
yield self.fix_namedtuples(obj.__class__.__name__, obj, cfg)
elif isinstance(obj, list):
yield list(self.fix_namedtuples_list(d[key], cfg))
else:
yield obj
@route(r"/api/v1/([a-z_]+)_t/(.+)")
class ObjectAPI(HandlerSyncedWithMaster, APIUtilMixin):
SELECTOR_ALL = object()
SELECTOR_RANDOM = object()
def real_expand_spec(self, spec):
return [ int(spec) ]
def expand_spec(self, spec):
comp = filter(bool, spec.split(","))
real_spec = []
for component in comp:
real_spec.extend(self.real_expand_spec(component))
return real_spec
def get(self, objectid, spec):
try:
ids = self.expand_spec(spec)
except Exception as e:
self.set_status(400)
self.write({"error": str(e)})
return
self.do_object_request(objectid, ids)
def collect_chars(self, ids, cfg):
result = []
for char in starlight.data.charas(ids):
if hasattr(char, "_asdict"):
d = self.fix_namedtuples(char.__class__.__name__, char._asdict(), cfg)
result.append(d)
else:
result.append(char)
return result
def collect_skill(self, ids, cfg):
result = []
for skill in starlight.data.skills(ids):
if hasattr(skill, "_asdict"):
d = self.fix_namedtuples(skill.__class__.__name__, skill._asdict(), cfg)
result.append(d)
else:
result.append(skill)
return result
def collect_lskill(self, ids, cfg):
result = []
for skill in starlight.data.lead_skills(ids):
if hasattr(skill, "_asdict"):
d = self.fix_namedtuples(skill.__class__.__name__, skill._asdict(), cfg)
result.append(d)
else:
result.append(skill)
return result
def collect_cards(self, ids, cfg):
result = []
for card in starlight.data.cards(ids):
if hasattr(card, "_asdict"):
d = self.fix_namedtuples(card.__class__.__name__, card._asdict(), cfg)
result.append(d)
else:
result.append(card)
return result
def do_object_request(self, kind, ids):
cfg = {
"stubs": self.get_argument("stubs", "no"),
"datetime": self.get_argument("datetime", "unix")
}
handlers = {"card": self.collect_cards,
"char": self.collect_chars,
"skill": self.collect_skill,
"leader_skill": self.collect_lskill}
h = handlers.get(kind)
if not h:
self.set_status(400)
self.write({"error": "you requested an unknown object type '{0}_t'".format(kind)})
return
self.set_header("Content-Type", "application/json; charset=utf-8")
if self.settings["is_dev"]:
json.dump({"result": h(ids, cfg)}, self, ensure_ascii=0, sort_keys=1, indent=2)
else:
json.dump({"result": h(ids, cfg)}, self)
@route(r"/api/v1/happening/(now|-?[0-9+])")
class HappeningAPI(HandlerSyncedWithMaster, APIUtilMixin):
def fix_datetime(self, obj):
if isinstance(obj, datetime):
fmt = self.get_argument("datetime", "unix")
if fmt == "unix":
return obj.timestamp()
else:
return obj.isoformat()
raise TypeError()
def get(self, timespec):
if timespec == "now":
timespec = datetime.utcnow()
else:
try:
timespec = int(timespec)
except ValueError as e:
self.set_status(400)
self.write({"error": str(e)})
return
timespec = datetime.utcfromtimestamp(timespec)
timespec = pytz.utc.localize(timespec)
cfg = {
"stubs": self.get_argument("stubs", "no"),
"datetime": self.get_argument("datetime", "unix")
}
self.set_header("Content-Type", "application/json; charset=utf-8")
payload = self.fix_namedtuples("",
{"events": starlight.data.events(timespec),
"gachas": starlight.data.gachas(timespec)}, cfg)
if self.settings["is_dev"]:
json.dump(payload, self, ensure_ascii=0, sort_keys=1, indent=2, default=self.fix_datetime)
else:
json.dump(payload, self, default=self.fix_datetime)
@route(r"/api/private/va_table")
class VATable(HandlerSyncedWithMaster):
def post(self):
try:
load = json.loads(self.request.body.decode("utf8"))
except ValueError:
self.set_status(400)
return
has_title_call = load["has_title_call"]
va_ids = load["va_ids"]
unique = list(set(va_ids))
self.render("va_table_partial.html", include_title_call=has_title_call, va_id=unique, **self.settings)