make DownloadClient mother class for all need to use that

This commit is contained in:
EstrellaXD
2023-05-07 11:59:11 +08:00
parent 5c71b9d35d
commit 98787552ca
6 changed files with 42 additions and 31 deletions

View File

@@ -21,18 +21,14 @@ def reset_log():
async def rss_loop(
rss_link: str,
rss_analyser: RSSAnalyser,
download_client: DownloadClient,
season_get: FullSeasonGet,
eps_complete: bool = False,
wait_time: int = 7200,
settings: Config,
):
datas = rss_analyser.run(rss_link)
if datas:
download_client.add_rules(datas, rss_link)
if eps_complete:
season_get.eps_complete(datas, download_client)
await asyncio.sleep(wait_time)
with RSSAnalyser(settings) as rss:
rss.rss_to_datas(rss_link)
if settings.bangumi_manage.eps_complete:
with FullSeasonGet(settings) as season:
season.eps_complete()
await asyncio.sleep(settings.program.sleep_time)
async def rename_loop(renamer: Renamer, wait_time: int = 360):

View File

@@ -16,6 +16,14 @@ class DownloadClient:
self.download_path = settings.downloader.path
self.group_tag = settings.bangumi_manage.group_tag
def __enter__(self):
if not self.authed:
self.auth()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.client.logout()
def auth(self):
self.client.auth()
self.authed = True

View File

@@ -33,6 +33,9 @@ class QbDownloader:
)
time.sleep(5)
def logout(self):
self._client.auth_log_out()
@qb_connect_failed_wait
def prefs_init(self, prefs):
return self._client.app_set_preferences(prefs=prefs)

View File

@@ -10,8 +10,9 @@ from module.models import BangumiData, Config
logger = logging.getLogger(__name__)
class FullSeasonGet:
class FullSeasonGet(DownloadClient):
def __init__(self, settings: Config):
super().__init__(settings)
self.SEARCH_KEY = [
"group",
"title_raw",
@@ -61,27 +62,27 @@ class FullSeasonGet:
downloads.append(download_info)
return downloads
def download_season(self, data: BangumiData, download_client: DownloadClient):
def download_season(self, data: BangumiData):
logger.info(f"Start collecting {data.official_title} Season {data.season}...")
torrents = self.get_season_torrents(data)
downloads = self.collect_season_torrents(data, torrents)
for download in downloads:
download_client.add_torrent(download)
self.add_torrent(download)
logger.info("Completed!")
data.eps_collect = False
def eps_complete(self, datas: list[BangumiData], download_client: DownloadClient):
def eps_complete(self, datas: list[BangumiData]):
for data in datas:
if data.eps_collect:
self.download_season(data, download_client)
self.download_season(data)
def download_collection(
self, data: BangumiData, link, download_client: DownloadClient
self, data: BangumiData, link
):
with RequestContent() as req:
torrents = req.get_torrents(link)
downloads = self.collect_season_torrents(data, torrents)
logger.info(f"Starting download {data.official_title} Season {data.season}...")
for download in downloads:
download_client.add_torrent(download)
self.add_torrent(download)
logger.info("Completed!")

View File

@@ -12,9 +12,9 @@ from module.models import Config
logger = logging.getLogger(__name__)
class Renamer:
def __init__(self, download_client: DownloadClient, settings: Config):
self._client = download_client
class Renamer(DownloadClient):
def __init__(self, settings: Config):
super().__init__(settings)
self._renamer = TitleParser()
self._notification = PostNotification()
self.settings = settings
@@ -28,7 +28,7 @@ class Renamer:
logger.debug(f"Checked {torrent_count} files")
def get_torrent_info(self, category="Bangumi"):
recent_info = self._client.get_torrent_info(category=category)
recent_info = self.get_torrent_info(category=category)
torrent_count = len(recent_info)
return recent_info, torrent_count
@@ -66,7 +66,7 @@ class Renamer:
)
if compare_name != new_path:
try:
self._client.rename_torrent_file(
self.rename_torrent_file(
_hash=info.hash, old_path=media_path, new_path=new_path
)
self._notification.send_msg(bangumi_name, f"{new_path}已经更新,已自动重命名。")
@@ -103,7 +103,7 @@ class Renamer:
)
if torrent_name != new_name:
try:
self._client.rename_torrent_file(
self.rename_torrent_file(
_hash=_hash, old_path=media_path, new_path=new_name
)
except Exception as e:
@@ -114,7 +114,7 @@ class Renamer:
logger.debug(e)
# Delete bad torrent.
self.delete_bad_torrent(info, remove_bad_torrents)
self._client.set_category(category="BangumiCollection", hashes=_hash)
self.set_category(category="BangumiCollection", hashes=_hash)
def rename_subtitles(
self,
@@ -137,7 +137,7 @@ class Renamer:
)
if old_name != new_name:
try:
self._client.rename_torrent_file(
self.rename_torrent_file(
_hash=_hash, old_path=subtitle_path, new_path=new_name
)
except Exception as e:
@@ -147,7 +147,7 @@ class Renamer:
def delete_bad_torrent(self, info, remove_bad_torrent: bool):
if remove_bad_torrent:
self._client.delete_torrent(info.hash)
self.delete_torrent(info.hash)
logger.info(f"{info.name} have been deleted.")
@staticmethod

View File

@@ -5,12 +5,14 @@ from module.network import RequestContent
from module.parser import TitleParser
from module.models import BangumiData, Config
from module.database import DataOperator
from module.core import DownloadClient
logger = logging.getLogger(__name__)
class RSSAnalyser:
class RSSAnalyser(DownloadClient):
def __init__(self, settings: Config):
super().__init__(settings)
self._title_analyser = TitleParser()
self.settings = settings
@@ -27,12 +29,13 @@ class RSSAnalyser:
raw=raw_title, _id=_id, settings=self.settings
)
if data is not None and op.match_title(data.official_title) is None:
op.insert(data)
self.set_rule(data, rss_link)
data_list.append(data)
_id += 1
op.insert_list(data_list)
return data_list
def rss_to_data(self, url, _filter: bool = True) -> BangumiData:
def rss_to_data(self, url, _filter: bool = True):
with RequestContent() as req:
rss_torrents = req.get_torrents(url, _filter)
for torrent in rss_torrents:
@@ -40,7 +43,7 @@ class RSSAnalyser:
data = self._title_analyser.raw_parser(
torrent.name, settings=self.settings
)
return data
self.set_rule(data, url)
except Exception as e:
logger.debug(e)