2024-11-28 09:03:49 +00:00
|
|
|
"""
|
2020-06-23 16:34:34 +01:00
|
|
|
This file is part of tmdb-bot.
|
|
|
|
|
|
|
|
tmdb-bot is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License version 3 as published by
|
|
|
|
the Free Software Foundation.
|
|
|
|
|
|
|
|
tmdb-bot is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with tmdb-bot. If not, see <https://www.gnu.org/licenses/>.
|
2024-11-28 09:03:49 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
# pylint: disable=missing-class-docstring,missing-function-docstring
|
2020-06-23 16:34:34 +01:00
|
|
|
|
2022-12-20 18:45:57 +00:00
|
|
|
from time import time
|
2024-11-28 09:03:49 +00:00
|
|
|
|
|
|
|
from sqlalchemy import Column, Float, Integer, MetaData, String, Table, select
|
2020-06-23 16:34:34 +01:00
|
|
|
from sqlalchemy.engine.base import Engine
|
|
|
|
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2020-06-23 16:34:34 +01:00
|
|
|
class Database:
|
|
|
|
db: Engine
|
|
|
|
|
|
|
|
def __init__(self, db: Engine) -> None:
|
|
|
|
self.db = db
|
|
|
|
meta = MetaData()
|
|
|
|
meta.bind = db
|
|
|
|
|
2024-11-28 09:03:49 +00:00
|
|
|
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),
|
|
|
|
)
|
|
|
|
self.tmdb_poster_size = Table(
|
|
|
|
"tmdb_poster_size",
|
|
|
|
meta,
|
|
|
|
Column("id", Integer, primary_key=True, autoincrement=True),
|
|
|
|
Column("user_id", String(255), nullable=False),
|
|
|
|
Column("size", String(255), nullable=False),
|
|
|
|
)
|
|
|
|
self.tmdb_messages = Table(
|
|
|
|
"tmdb_messages",
|
|
|
|
meta,
|
|
|
|
Column("timestamp", Float, primary_key=True),
|
|
|
|
Column("event_id", String(64), nullable=False),
|
|
|
|
Column("result_json", String(255), nullable=False),
|
|
|
|
)
|
2020-06-23 16:34:34 +01:00
|
|
|
meta.create_all(db)
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2022-12-20 18:45:57 +00:00
|
|
|
def set_message(self, event_id, result_json):
|
|
|
|
with self.db.begin() as tx:
|
|
|
|
timestamp = time()
|
2024-11-28 09:03:49 +00:00
|
|
|
tx.execute(
|
|
|
|
self.tmdb_messages.insert().values(
|
|
|
|
timestamp=timestamp, event_id=event_id, result_json=result_json
|
|
|
|
)
|
|
|
|
)
|
2022-12-20 18:45:57 +00:00
|
|
|
|
|
|
|
def get_message(self, event_id):
|
2024-11-28 09:03:49 +00:00
|
|
|
rows = self.db.execute(
|
|
|
|
select([self.tmdb_messages.c.result_json]).where(
|
|
|
|
self.tmdb_messages.c.event_id == event_id
|
|
|
|
)
|
|
|
|
)
|
2022-12-20 18:45:57 +00:00
|
|
|
row = rows.fetchone()
|
|
|
|
if row:
|
2024-11-28 09:03:49 +00:00
|
|
|
return row["result_json"]
|
|
|
|
return None
|
2022-12-20 18:45:57 +00:00
|
|
|
|
2020-06-23 16:34:34 +01:00
|
|
|
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))
|
2024-11-28 09:03:49 +00:00
|
|
|
tx.execute(
|
|
|
|
self.language.insert().values(user_id=user_id, language=language)
|
|
|
|
)
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2020-06-23 16:34:34 +01:00
|
|
|
def get_language(self, user_id):
|
2024-11-28 09:03:49 +00:00
|
|
|
rows = self.db.execute(
|
|
|
|
select([self.language.c.language]).where(self.language.c.user_id == user_id)
|
|
|
|
)
|
2020-09-29 08:49:16 +01:00
|
|
|
row = rows.fetchone()
|
|
|
|
if row:
|
2024-11-28 09:03:49 +00:00
|
|
|
return row["language"]
|
|
|
|
return None
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2020-09-09 14:18:22 +01:00
|
|
|
def set_poster_size(self, user_id, size):
|
|
|
|
with self.db.begin() as tx:
|
2024-11-28 09:03:49 +00:00
|
|
|
tx.execute(
|
|
|
|
self.tmdb_poster_size.delete().where(
|
|
|
|
self.tmdb_poster_size.c.user_id == user_id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
tx.execute(
|
|
|
|
self.tmdb_poster_size.insert().values(user_id=user_id, size=size)
|
|
|
|
)
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2020-09-09 14:18:22 +01:00
|
|
|
def get_poster_size(self, user_id):
|
2024-11-28 09:03:49 +00:00
|
|
|
rows = self.db.execute(
|
|
|
|
select([self.tmdb_poster_size.c.size]).where(
|
|
|
|
self.tmdb_poster_size.c.user_id == user_id
|
|
|
|
)
|
|
|
|
)
|
2020-09-29 08:49:16 +01:00
|
|
|
row = rows.fetchone()
|
|
|
|
if row:
|
2024-11-28 09:03:49 +00:00
|
|
|
return row["size"]
|
|
|
|
return None
|