forked from ishefi/semantle-he
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.py
130 lines (107 loc) · 3.87 KB
/
handlers.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
from datetime import datetime
from datetime import timedelta
import json
import tornado.web
# from common.session import get_mongo
# from common.session import get_redis
from logic import CacheSecretLogic
from logic import EasterEggLogic
from logic import VectorLogic
def get_handlers():
return [
(r"/?", IndexHandler),
(r"/yesterday-top-1000/?", YesterdayClosestHandler),
(r"/api/distance/?", DistanceHandler),
(r"/secrets/?", AllSecretsHandler),
(r"/faq/?", FaqHandler),
]
class BaseHandler(tornado.web.RequestHandler):
_DELTA = None
_SECRET_CACHE = {}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.mongo = self.application.mongo
self.redis = self.application.redis
self.date = datetime.utcnow().date() - self.DELTA
self.logic = VectorLogic(self.mongo, self.date)
secret = self.logic.secret_logic.get_secret()
self.cache_logic = CacheSecretLogic(
self.mongo, self.redis, secret=secret, dt=self.date
)
@property
def DELTA(self):
if self._DELTA is None:
self._DELTA = timedelta(days=self.application.days_delta)
return self._DELTA
async def reply(self, content):
content = json.dumps(content)
self.set_header("Content-Type", "application/json")
self.set_header("Content-Length", len(content))
self.write(content)
await self.finish()
class IndexHandler(BaseHandler):
FIRST_DATE = datetime(2022, 2, 21).date()
async def get(self):
cache = await self.cache_logic.cache
closest1 = await self.logic.get_similarity(cache[-2])
closest10 = await self.logic.get_similarity(cache[-12])
closest1000 = await self.logic.get_similarity(cache[0])
number = (self.date - self.FIRST_DATE).days + 1
yestersecret = await VectorLogic(
self.mongo, self.date - timedelta(days=1)
).secret_logic.get_secret()
await self.render(
'static/index.html',
number=number,
closest1=closest1,
closest10=closest10,
closest1000=closest1000,
yesterdays_secret=yestersecret,
)
class DistanceHandler(BaseHandler):
async def get(self):
word = self.get_argument('word')
word = word.replace("'", "")
if egg := EasterEggLogic.get_easter_egg(word):
reply = {
"similarity": 99.99,
"distance": -1,
"egg": egg
}
else:
sim = await self.logic.get_similarity(word)
cache_score = await self.cache_logic.get_cache_score(word)
reply = {
"similarity": sim,
"distance": cache_score,
}
await self.reply(reply)
class YesterdayClosestHandler(BaseHandler):
async def get(self):
cache = await self.cache_logic.cache
yesterday_sims = await self.logic.get_similarities(cache)
await self.render(
'static/closest1000.html',
yesterday=sorted(yesterday_sims.items(), key=lambda ws: ws[1], reverse=1),
)
@property
def DELTA(self):
return self.DELTA + timedelta(days=1)
class AllSecretsHandler(BaseHandler):
async def get(self):
secrets = await self.logic.secret_logic.get_all_secrets()
api_key = self.get_argument('api_key', None)
if api_key != self.application.api_key:
raise tornado.web.HTTPError(403)
await self.render(
'static/all_secrets.html',
secrets=sorted(secrets, key=lambda ws: ws[1], reverse=1),
)
class FaqHandler(BaseHandler):
DELTA = timedelta(days=1)
async def get(self):
cache = await self.cache_logic.cache
await self.render(
'static/faq.html',
yesterday=cache[-11:],
)