You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.5 KiB
Python
51 lines
1.5 KiB
Python
2 years ago
|
import asyncio
|
||
|
from typing import TypedDict
|
||
|
from aiohttp import web
|
||
|
import asyncpg
|
||
|
import config
|
||
|
import api.route
|
||
|
import utils.web
|
||
|
from service.database import DatabaseService
|
||
|
from service.mediawiki_api import MediaWikiApi
|
||
|
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
|
||
|
|
||
|
from service.tiktoken import TikTokenService
|
||
|
|
||
|
async def index(request: web.Request):
|
||
|
return utils.web.api_response(1, data={"message": "Isekai toolkit API"}, request=request)
|
||
|
|
||
|
async def init_mw_api(app: web.Application):
|
||
|
mw_api = MediaWikiApi.create()
|
||
|
if config.MW_BOT_LOGIN_USERNAME and config.MW_BOT_LOGIN_PASSWORD:
|
||
|
await mw_api.robot_login(config.MW_BOT_LOGIN_USERNAME, config.MW_BOT_LOGIN_PASSWORD)
|
||
|
|
||
|
site_meta = await mw_api.get_site_meta()
|
||
|
|
||
|
print("Connected to Wiki %s, Robot username: %s" % (site_meta["sitename"], site_meta["user"]))
|
||
|
|
||
|
async def init_database(app: web.Application):
|
||
|
dbs = await DatabaseService.create(app)
|
||
|
print("Database connected.")
|
||
|
|
||
|
async def init_tiktoken(app: web.Application):
|
||
|
await TikTokenService.create()
|
||
|
print("Tiktoken model loaded.")
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
loop = asyncio.get_event_loop()
|
||
|
|
||
|
app = web.Application()
|
||
|
|
||
|
if config.DATABASE:
|
||
|
app.on_startup.append(init_database)
|
||
|
|
||
|
if config.MW_API:
|
||
|
app.on_startup.append(init_mw_api)
|
||
|
|
||
|
if config.OPENAI_TOKEN:
|
||
|
app.on_startup.append(init_tiktoken)
|
||
|
|
||
|
app.router.add_route('*', '/', index)
|
||
|
api.route.init(app)
|
||
|
web.run_app(app, host='0.0.0.0', port=config.PORT, loop=loop)
|
||
|
|