mirror of
https://github.com/EstrellaXD/Auto_Bangumi.git
synced 2026-05-16 14:03:44 +08:00
change: Decoupling of notification module
This commit is contained in:
@@ -52,6 +52,9 @@ class RenameThread(ProgramStatus):
|
|||||||
while not self.stop_event.is_set():
|
while not self.stop_event.is_set():
|
||||||
with Renamer() as renamer:
|
with Renamer() as renamer:
|
||||||
renamed_info = renamer.rename()
|
renamed_info = renamer.rename()
|
||||||
|
with PostNotification() as notifier:
|
||||||
|
for info in renamed_info:
|
||||||
|
notifier.send_msg(info)
|
||||||
self.stop_event.wait(settings.program.rename_time)
|
self.stop_event.wait(settings.program.rename_time)
|
||||||
|
|
||||||
def rename_start(self):
|
def rename_start(self):
|
||||||
|
|||||||
@@ -3,10 +3,8 @@ import logging
|
|||||||
from module.downloader import DownloadClient
|
from module.downloader import DownloadClient
|
||||||
|
|
||||||
from module.parser import TitleParser
|
from module.parser import TitleParser
|
||||||
# from module.network import PostNotification
|
|
||||||
from module.models import SubtitleFile, EpisodeFile, Notification
|
from module.models import SubtitleFile, EpisodeFile, Notification
|
||||||
from module.conf import settings
|
from module.conf import settings
|
||||||
# from module.database import BangumiDatabase
|
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
@@ -47,24 +45,6 @@ class Renamer(DownloadClient):
|
|||||||
logger.error(f"[Renamer] Unknown rename method: {method}")
|
logger.error(f"[Renamer] Unknown rename method: {method}")
|
||||||
return file_info.media_path
|
return file_info.media_path
|
||||||
|
|
||||||
# @staticmethod
|
|
||||||
# def send_notification(bangumi_name, ep: EpisodeFile):
|
|
||||||
# with BangumiDatabase() as db:
|
|
||||||
# poster_path = db.match_poster(bangumi_name)
|
|
||||||
# poster_link = "https://mikanani.me" + poster_path
|
|
||||||
# n = Notification(
|
|
||||||
# official_title=bangumi_name,
|
|
||||||
# season=ep.season,
|
|
||||||
# episode=ep.episode,
|
|
||||||
# poster_link=poster_link,
|
|
||||||
# )
|
|
||||||
# with PostNotification() as notificator:
|
|
||||||
# status = notificator.send_msg(n)
|
|
||||||
# if status:
|
|
||||||
# logger.info(f"[Renamer] Notification sent: {ep.title} S{ep.season}E{ep.episode}")
|
|
||||||
# else:
|
|
||||||
# logger.warning(f"[Renamer] Notification failed: {ep.title} S{ep.season}E{ep.episode}")
|
|
||||||
|
|
||||||
def rename_file(
|
def rename_file(
|
||||||
self,
|
self,
|
||||||
torrent_name: str,
|
torrent_name: str,
|
||||||
@@ -87,9 +67,12 @@ class Renamer(DownloadClient):
|
|||||||
_hash=_hash, old_path=media_path, new_path=new_path
|
_hash=_hash, old_path=media_path, new_path=new_path
|
||||||
)
|
)
|
||||||
if renamed:
|
if renamed:
|
||||||
# if settings.notification.enable:
|
n = Notification(
|
||||||
# self.send_notification(bangumi_name, ep)
|
official_title=bangumi_name,
|
||||||
return ep
|
season=ep.season,
|
||||||
|
episode=ep.episode,
|
||||||
|
)
|
||||||
|
return n
|
||||||
else:
|
else:
|
||||||
logger.warning(f"[Renamer] {media_path} parse failed")
|
logger.warning(f"[Renamer] {media_path} parse failed")
|
||||||
if settings.bangumi_manage.remove_bad_torrent:
|
if settings.bangumi_manage.remove_bad_torrent:
|
||||||
@@ -169,9 +152,9 @@ class Renamer(DownloadClient):
|
|||||||
}
|
}
|
||||||
# Rename single media file
|
# Rename single media file
|
||||||
if len(media_list) == 1:
|
if len(media_list) == 1:
|
||||||
ep_info = self.rename_file(media_path=media_list[0], **kwargs)
|
notify_info = self.rename_file(media_path=media_list[0], **kwargs)
|
||||||
if ep_info:
|
if notify_info:
|
||||||
renamed_info.append(ep_info)
|
renamed_info.append(notify_info)
|
||||||
# Rename subtitle file
|
# Rename subtitle file
|
||||||
if len(subtitle_list) > 0:
|
if len(subtitle_list) > 0:
|
||||||
self.rename_subtitles(subtitle_list=subtitle_list, **kwargs)
|
self.rename_subtitles(subtitle_list=subtitle_list, **kwargs)
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ class Notification(BaseModel):
|
|||||||
official_title: str = Field(..., alias="official_title", title="番剧名")
|
official_title: str = Field(..., alias="official_title", title="番剧名")
|
||||||
season: int = Field(..., alias="season", title="番剧季度")
|
season: int = Field(..., alias="season", title="番剧季度")
|
||||||
episode: int = Field(..., alias="episode", title="番剧集数")
|
episode: int = Field(..., alias="episode", title="番剧集数")
|
||||||
poster_link: str | None = Field(None, alias="poster_link", title="番剧海报链接")
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
|||||||
@@ -4,16 +4,13 @@ from .plugin import *
|
|||||||
|
|
||||||
from module.models import Notification
|
from module.models import Notification
|
||||||
from module.conf import settings
|
from module.conf import settings
|
||||||
|
from module.database import BangumiDatabase
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
type = settings.notification.type
|
|
||||||
token = settings.notification.token
|
|
||||||
chat_id = settings.notification.chat_id
|
|
||||||
|
|
||||||
|
def getClient(type=settings.notification.type):
|
||||||
def getClient():
|
|
||||||
if type.lower() == "telegram":
|
if type.lower() == "telegram":
|
||||||
return TelegramNotification
|
return TelegramNotification
|
||||||
elif type.lower() == "server-chan":
|
elif type.lower() == "server-chan":
|
||||||
@@ -25,15 +22,37 @@ def getClient():
|
|||||||
|
|
||||||
|
|
||||||
class PostNotification(getClient()):
|
class PostNotification(getClient()):
|
||||||
def send_msg(self, info: Notification) -> bool:
|
def __init__(self):
|
||||||
text = (
|
super().__init__(
|
||||||
f"番剧名称:{info.official_title}\n"
|
token=settings.notification.token,
|
||||||
f"季度: 第{info.season}季\n"
|
chat_id=settings.notification.chat_id
|
||||||
f"更新集数: 第{info.episode}集\n"
|
|
||||||
f"{info.poster_link}\n"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _gen_message(notify: Notification) -> str:
|
||||||
|
with BangumiDatabase() as db:
|
||||||
|
poster_path = db.match_poster(notify.official_title)
|
||||||
|
if poster_path:
|
||||||
|
poster_link = "https://mikanani.me" + poster_path
|
||||||
|
text = f"""
|
||||||
|
番剧名称:{info.official_title}\n
|
||||||
|
季度: 第{info.season}季\n
|
||||||
|
更新集数: 第{info.episode}集\n
|
||||||
|
{poster_link}\n
|
||||||
|
"""
|
||||||
|
else:
|
||||||
|
text = """
|
||||||
|
番剧名称:{info.official_title}\n
|
||||||
|
季度: 第{info.season}季\n
|
||||||
|
更新集数: 第{info.episode}集\n
|
||||||
|
"""
|
||||||
|
return text
|
||||||
|
|
||||||
|
def send_msg(self, notify: Notification) -> bool:
|
||||||
|
text = self._gen_message(notify)
|
||||||
try:
|
try:
|
||||||
return self.post_msg(text)
|
self.post_msg(text)
|
||||||
|
logger.debug(f"Send notification: {text}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"Failed to send notification: {e}")
|
logger.warning(f"Failed to send notification: {e}")
|
||||||
return False
|
return False
|
||||||
@@ -44,7 +63,6 @@ if __name__ == "__main__":
|
|||||||
official_title="魔法纪录 魔法少女小圆外传",
|
official_title="魔法纪录 魔法少女小圆外传",
|
||||||
season=2,
|
season=2,
|
||||||
episode=1,
|
episode=1,
|
||||||
poster_link="https://mikanani.me/images/Bangumi/202107/3788b33f.jpg",
|
|
||||||
)
|
)
|
||||||
with PostNotification() as client:
|
with PostNotification() as client:
|
||||||
client.send_msg(info)
|
client.send_msg(info)
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
|
import logging
|
||||||
from module.network import RequestContent
|
from module.network import RequestContent
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class BarkNotification(RequestContent):
|
class BarkNotification(RequestContent):
|
||||||
def __init__(self):
|
def __init__(self, token, **kwargs):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.token = token
|
self.token = token
|
||||||
self.notification_url = "https://api.day.app/push"
|
self.notification_url = "https://api.day.app/push"
|
||||||
@@ -10,4 +15,4 @@ class BarkNotification(RequestContent):
|
|||||||
data = {"title": "AutoBangumi 番剧更新", "body": text, "device_key": self.token}
|
data = {"title": "AutoBangumi 番剧更新", "body": text, "device_key": self.token}
|
||||||
resp = self.post_data(self.notification_url, data)
|
resp = self.post_data(self.notification_url, data)
|
||||||
logger.debug(f"Bark notification: {resp.status_code}")
|
logger.debug(f"Bark notification: {resp.status_code}")
|
||||||
return resp.status_code == 200
|
return resp.status_code == 200
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
from module.network import RequestContent
|
from module.network import RequestContent
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class ServerChanNotification(RequestContent):
|
class ServerChanNotification(RequestContent):
|
||||||
"""Server酱推送"""
|
"""Server酱推送"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, token, **kwargs):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.notification_url = f"https://sctapi.ftqq.com/{token}.send"
|
self.notification_url = f"https://sctapi.ftqq.com/{token}.send"
|
||||||
|
|
||||||
@@ -15,4 +19,4 @@ class ServerChanNotification(RequestContent):
|
|||||||
}
|
}
|
||||||
resp = self.post_data(self.notification_url, data)
|
resp = self.post_data(self.notification_url, data)
|
||||||
logger.debug(f"ServerChan notification: {resp.status_code}")
|
logger.debug(f"ServerChan notification: {resp.status_code}")
|
||||||
return resp.status_code == 200
|
return resp.status_code == 200
|
||||||
|
|||||||
@@ -1,5 +1,12 @@
|
|||||||
class BarkNotification(RequestContent):
|
import logging
|
||||||
def __init__(self):
|
|
||||||
|
from module.network import RequestContent
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class SlackNotification(RequestContent):
|
||||||
|
def __init__(self, token, **kwargs):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.token = token
|
self.token = token
|
||||||
self.notification_url = "https://api.day.app/push"
|
self.notification_url = "https://api.day.app/push"
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
from module.network.request_contents import RequestContent
|
from module.network.request_contents import RequestContent
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class TelegramNotification(RequestContent):
|
class TelegramNotification(RequestContent):
|
||||||
def __init__(self):
|
def __init__(self, token, chat_id):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.notification_url = f"https://api.telegram.org/bot{token}/sendMessage"
|
self.notification_url = f"https://api.telegram.org/bot{token}/sendMessage"
|
||||||
self.chat_id = chat_id
|
self.chat_id = chat_id
|
||||||
@@ -14,4 +19,4 @@ class TelegramNotification(RequestContent):
|
|||||||
}
|
}
|
||||||
resp = self.post_data(self.notification_url, data)
|
resp = self.post_data(self.notification_url, data)
|
||||||
logger.debug(f"Telegram notification: {resp.status_code}")
|
logger.debug(f"Telegram notification: {resp.status_code}")
|
||||||
return resp.status_code == 200
|
return resp.status_code == 200
|
||||||
|
|||||||
Reference in New Issue
Block a user