1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-24 03:54:20 +00:00
royalnet/keipack/stars/api_kei.py

39 lines
1.3 KiB
Python
Raw Normal View History

2019-11-29 20:18:01 +00:00
import random
import datetime
from typing import *
2019-11-11 12:40:26 +00:00
from starlette.requests import Request
from starlette.responses import *
2019-11-29 20:18:01 +00:00
from royalnet.constellation import *
2019-11-11 12:40:26 +00:00
from royalnet.utils import *
2019-11-29 23:45:41 +00:00
from ..tables import *
from ..utils import Emotion
2019-11-11 12:40:26 +00:00
class ApiKei(PageStar):
path = "/api/kei"
2019-11-29 23:45:41 +00:00
methods = ["POST"]
async def _generate(self, person, form, session) -> Tuple[Emotion, str]:
return Emotion.HAPPY, f'Prova'
2019-11-11 12:40:26 +00:00
async def page(self, request: Request) -> JSONResponse:
async with self.session_acm() as session:
2019-11-29 23:45:41 +00:00
form = await request.form()
person = session.query(self.alchemy.get(KeiPerson)).filter_by(kpid=form["kpid"]).one_or_none()
if person is None:
person = self.alchemy.get(KeiPerson)(kpid=form["kpid"])
session.add(person)
message = self.alchemy.get(KeiMessage)(kei_person=person, message=form["message"])
session.add(message)
await asyncify(session.commit)
try:
emotion, text = await self._generate(person, form, session)
except Exception as e:
print(e)
emotion, text = Emotion.NEUTRAL, "...?"
2019-11-11 12:40:26 +00:00
return JSONResponse({
2019-11-29 23:45:41 +00:00
"emotion": str(emotion),
2019-11-11 12:40:26 +00:00
"text": text,
})