From 4b98279fa6b953b8ba693fda8b3d5b76c040878e Mon Sep 17 00:00:00 2001 From: Lomion Date: Mon, 22 Jun 2020 16:21:18 +0200 Subject: [PATCH] Got language database to work --- maubot.yaml | 2 +- tmdb/tmdb.py | 64 ++++++++++++++++++++++++---------------------------- 2 files changed, 31 insertions(+), 35 deletions(-) diff --git a/maubot.yaml b/maubot.yaml index 31d9324..9784f72 100644 --- a/maubot.yaml +++ b/maubot.yaml @@ -1,6 +1,6 @@ maubot: 0.1.0 id: lomion.tmdb -version: 0.0.2 +version: 0.0.3 license: AGPL 3.0 modules: - tmdb diff --git a/tmdb/tmdb.py b/tmdb/tmdb.py index cbba73b..035bfb2 100644 --- a/tmdb/tmdb.py +++ b/tmdb/tmdb.py @@ -11,8 +11,33 @@ from sqlalchemy import (Column, String, Integer, ForeignKey, Table, MetaData, select, and_) from sqlalchemy.engine.base import Engine -class TmdbBot(Plugin): +class Database: + db: Engine + def __init__(self, db: Engine) -> None: + self.db = db + + meta = MetaData() + meta.bind = db + + self.language = Table("tmdb_language", meta, + Column("id", Integer, primary_key=True, autoincrement=True), + Column("user_id", String(255), nullable=False), + Column("language", String(255), nullable=False),) + meta.create_all(db) + + def set_language(self, user_id, language): + with self.db.begin() as tx: + tx.execute(self.language.delete().where(self.language.c.user_id == user_id)) + tx.execute(self.language.insert().values(user_id=user_id, language=language)) + + def get_language(self, user_id): + rows = self.db.execute(select([self.language.c.language]) + .where(self.language.c.user_id == user_id)) + return rows.fetchone() + +class TmdbBot(Plugin): + db: Database async def start(self) -> None: await super().start() @@ -49,6 +74,9 @@ class TmdbBot(Plugin): @command.argument("message", pass_raw=True, required=True) async def movie_id(self, evt: MessageEvent, message: str = "") -> None: movie = Movie() + language = self.db.get_language(evt.sender) + if language: + movie.set_language(language) movie.query_details(message) await self.send_movie_info(evt, movie) @@ -57,13 +85,9 @@ class TmdbBot(Plugin): @command.argument("message", pass_raw=True, required=True) async def movie_search(self, evt: MessageEvent, message: str = "") -> None: movie = Movie() - self.db = Database(self.database) language = self.db.get_language(evt.sender) if language: movie.set_language(language) - else: - self.db.set_language(evt.sender, 'de') - movie.set_language('de') movie.search_title(message) if movie.valid: await self.send_movie_info(evt, movie) @@ -76,36 +100,8 @@ class TmdbBot(Plugin): @command.new("movie-language", help="Set language for lookup") @command.argument("message", pass_raw=True, required=True) async def movie_language(self, evt: MessageEvent, message: str = "") -> None: - self.db = Database(self.database) self.db.set_language(evt.sender, message) content = TextMessageEventContent( msgtype=MessageType.NOTICE, format=Format.HTML, - body=f"Language set to {language}!") + body=f"Language set to {message}!") await evt.respond(content) - -class Database: - def __init__(self, db: Engine) -> None: - self.db = db - - meta = MetaData() - meta.bind = db - - self.language = Table("tmdb_language", meta, - Column("id", Integer, primary_key=True, autoincrement=True), - Column("user_id", String(255), nullable=False), - Column("language", String(255), nullable=False),) - meta.create_all() - - def set_language(self, user_id, language): - with self.db.begin() as tx: - tx.execute(self.language.delete().where(self.language.c.user_id == user_id)) - tx.execute(self.language.insert().values(user_id=user_id, language=language)) - - def get_language(self, user_id): - rows = self.db.execute(select([self.language.c.language]) - .where(self.language.c.user_id == user_id)) - if len(rows) == 1: - row = rows[0] - return row[2] - else: - return None