From 5fb5489abdb618fec8af03a317b5a529b97fde33 Mon Sep 17 00:00:00 2001 From: EstrellaXD Date: Sat, 13 May 2023 21:05:04 +0800 Subject: [PATCH] Make dataconnctor a orm class --- src/module/database/connector.py | 12 +++++++ src/module/database/torrent.py | 43 +++++++++++++++++++++++++ src/module/rss/filter.py | 55 ++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) diff --git a/src/module/database/connector.py b/src/module/database/connector.py index 18b381df..3807e32a 100644 --- a/src/module/database/connector.py +++ b/src/module/database/connector.py @@ -28,6 +28,18 @@ class DataConnector: self._conn.commit() logger.debug(f"Create / Update table {table_name}.") + def _insert(self, table_name: str, db_data: dict): + columns = ", ".join(db_data.keys()) + values = ", ".join([f":{key}" for key in db_data.keys()]) + self._cursor.execute(f"INSERT INTO {table_name} ({columns}) VALUES ({values})", db_data) + self._conn.commit() + + def _insert_list(self, table_name: str, data_list: list[dict]): + columns = ", ".join(data_list[0].keys()) + values = ", ".join([f":{key}" for key in data_list[0].keys()]) + self._cursor.executemany(f"INSERT INTO {table_name} ({columns}) VALUES ({values})", data_list) + self._conn.commit() + @staticmethod def __python_to_sqlite_type(value) -> str: if isinstance(value, int): diff --git a/src/module/database/torrent.py b/src/module/database/torrent.py index e69de29b..623addf3 100644 --- a/src/module/database/torrent.py +++ b/src/module/database/torrent.py @@ -0,0 +1,43 @@ +import logging + +from .connector import DataConnector + +logger = logging.getLogger(__name__) + +class TorrentDatabase(DataConnector): + + def update_table(self): + table_name = "torrent" + db_data = self.__data_to_db() + self._update_table(table_name, db_data) + + def __data_to_db(self, data: SaveTorrent): + db_data = data.dict() + for key, value in db_data.items(): + if isinstance(value, bool): + db_data[key] = int(value) + elif isinstance(value, list): + db_data[key] = ",".join(value) + return db_data + + def __db_to_data(self, db_data: dict): + for key, item in db_data.items(): + if isinstance(item, int): + if key not in ["id", "offset", "season", "year"]: + db_data[key] = bool(item) + elif key in ["filter", "rss_link"]: + db_data[key] = item.split(",") + return SaveTorrent(**db_data) + + def if_downloaded(self, torrent_url: str, torrent_name: str) -> bool: + self._cursor.execute("SELECT * FROM torrent WHERE torrent_url = ? OR torrent_name = ?", + (torrent_url, torrent_name)) + return bool(self._cursor.fetchone()) + + def insert(self, data: SaveTorrent): + db_data = self.__data_to_db(data) + columns = ", ".join(db_data.keys()) + values = ", ".join([f":{key}" for key in db_data.keys()]) + self._cursor.execute(f"INSERT INTO torrent ({columns}) VALUES ({values})", db_data) + logger.debug(f"Add {data.torrent_name} into database.") + self._conn.commit() \ No newline at end of file diff --git a/src/module/rss/filter.py b/src/module/rss/filter.py index e69de29b..9691c4b6 100644 --- a/src/module/rss/filter.py +++ b/src/module/rss/filter.py @@ -0,0 +1,55 @@ +import logging + +from module.database import BangumiDatabase +from module.downloader import DownloadClient +from module.network import RequestContent +from module.conf import settings +from module.models import BangumiData + +logger = logging.getLogger(__name__) + + +def matched(torrent_title: str): + with BangumiDatabase() as db: + return db.match_torrent(torrent_title) + + +def save_path(data: BangumiData): + folder = f"{data.official_title}({data.year})" if data.year else f"{data.official_title}" + season = f"Season {data.season}" + return path.join( + settings.downloader.path, + folder, + season, + ) + + +def add_download(data: BangumiData, torrent: TorrentInfo): + torrent = { + "url": torrent.url, + "save_path": save_path(data), + } + with DownloadClient() as client: + client.add_torrent(torrent) + with TorrentDatabase() as db: + db.add_torrent(torrent) + + +def downloaded(torrent: TorrentInfo): + with TorrentDatabase() as db: + return db.if_downloaded(torrent) + + +def get_downloads(rss_link: str): + with RequestContent() as req: + torrents = req.get_torrents(rss_link) + for torrent in torrents: + if not downloaded(torrent): + data = matched(torrent.title) + if data: + add_download(data, torrent) + logger.info(f"Add {torrent.title} to download list") + else: + logger.debug(f"{torrent.title} not matched") + else: + logger.debug(f"{torrent.title} already downloaded") \ No newline at end of file