mirror of
https://github.com/EstrellaXD/Auto_Bangumi.git
synced 2026-04-13 18:11:03 +08:00
Make dataconnctor a orm class
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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()
|
||||
@@ -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")
|
||||
Reference in New Issue
Block a user