This repository has been archived on 2023-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
MirrorTea/mirrortea/__main__.py

82 lines
2.1 KiB
Python
Raw Normal View History

2023-01-07 20:11:46 +00:00
import asyncio
2023-01-08 13:01:33 +00:00
import os
2023-01-08 20:39:15 +00:00
import sqlite3
2023-01-08 12:55:14 +00:00
import sys
2023-01-07 21:13:27 +00:00
import aiogram as telegram
import nio as matrix
2023-01-07 20:11:46 +00:00
from matrix import MatrixLoop
from telegram import TelegramLoop
from mirrortea.init_db import (
MATRIX_ROOMS_SQL,
TELEGRAM_USER_MATRIX_CHATS_SQL,
TELEGRAM_USERS_SQL,
)
2023-01-08 22:13:27 +00:00
2023-01-08 19:28:13 +00:00
def main():
2023-01-08 19:31:11 +00:00
config = Config(
db_path=os.environ["DB_PATH"],
matrix_bot_id=os.environ["MATRIX_BOT_ID"],
matrix_homeserver_url=os.environ["MATRIX_HOMESERVER_URL"],
matrix_owner_id=os.environ["MATRIX_OWNER_ID"],
matrix_password=os.environ["MATRIX_PASSWORD"],
telegram_bot_token=os.environ["TELEGRAM_BOT_TOKEN"],
2023-01-08 19:31:11 +00:00
)
asyncio.run(Application(config).run())
2023-01-08 19:31:11 +00:00
class Config:
def __init__(self, **kwargs):
self.db_path = kwargs["db_path"]
self.matrix_bot_id = kwargs["matrix_bot_id"]
self.matrix_homeserver_url = kwargs["matrix_homeserver_url"]
self.matrix_owner_id = kwargs["matrix_owner_id"]
self.matrix_password = kwargs["matrix_password"]
self.telegram_bot_token = kwargs["telegram_bot_token"]
# выглядит ужасно :(
2023-01-07 21:42:02 +00:00
2023-01-08 19:28:13 +00:00
class Application:
2023-01-08 19:31:11 +00:00
def __init__(self, config):
self.config = config
2023-01-08 20:53:15 +00:00
self.sqlite_adapter = SqliteAdapter(self, config.db_path)
self.matrix_loop = MatrixLoop(self)
self.telegram_loop = TelegramLoop(self)
2023-01-08 19:28:13 +00:00
async def run(self):
try:
await self.matrix_loop.prepare()
await asyncio.gather(
self.matrix_loop.run(),
self.telegram_loop.run(),
)
finally:
if self.matrix_loop:
await self.matrix_loop.finish()
2023-01-08 18:43:37 +00:00
2023-01-08 13:54:22 +00:00
2023-01-08 20:39:15 +00:00
class SqliteAdapter:
def __init__(self, app, path):
self.app = app
self.path = path
self.conn = sqlite3.connect(path)
self._create_tables()
def _create_tables(self):
for table in [
TELEGRAM_USER_MATRIX_CHATS_SQL,
TELEGRAM_USERS_SQL,
MATRIX_ROOMS_SQL,
]:
self.conn.execute(table)
self.conn.commit()
2023-01-08 20:39:15 +00:00
if __name__ == "__main__":
2023-01-08 19:28:13 +00:00
main()