Skip to content

Commit

Permalink
Merge pull request #289 from EstrellaXD/3.0-dev
Browse files Browse the repository at this point in the history
3.0.2
  • Loading branch information
EstrellaXD authored Jun 4, 2023
2 parents c079b12 + 2a7f58d commit 10962a0
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 29 deletions.
2 changes: 1 addition & 1 deletion docs/wiki
Submodule wiki updated from 931b42 to d0bb98
6 changes: 4 additions & 2 deletions src/module/api/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ async def download_collection(
)
if data:
with SeasonCollector() as collector:
collector.collect_season(data, data.rss_link[0])
return {"status": "Success"}
if collector.collect_season(data, data.rss_link[0], proxy=True):
return {"status": "Success"}
else:
return {"status": "Failed to add torrent"}
else:
return {"status": "Failed to parse link"}

Expand Down
6 changes: 4 additions & 2 deletions src/module/downloader/client/qb_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,16 @@ def add_category(self, category):
def torrents_info(self, status_filter, category, tag=None):
return self._client.torrents_info(status_filter=status_filter, category=category, tag=tag)

def torrents_add(self, urls, save_path, category):
return self._client.torrents_add(
def torrents_add(self, urls, save_path, category, torrent_files=None):
resp = self._client.torrents_add(
is_paused=False,
urls=urls,
torrent_files=torrent_files,
save_path=save_path,
category=category,
use_auto_torrent_management=False
)
return resp == "Ok."

def torrents_delete(self, hash):
return self._client.torrents_delete(delete_files=True, torrent_hashes=hash)
Expand Down
16 changes: 12 additions & 4 deletions src/module/downloader/download_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
def auth(self):
self.authed = self.client.auth()
if self.authed:
logger.info("[Downloader] Authed.")
logger.debug("[Downloader] Authed.")
else:
logger.error("[Downloader] Auth failed.")

Expand Down Expand Up @@ -112,9 +112,17 @@ def delete_torrent(self, hashes):
logger.info(f"[Downloader] Remove torrents.")

def add_torrent(self, torrent: dict):
self.client.torrents_add(
urls=torrent["url"], save_path=torrent["save_path"], category="Bangumi"
)
if self.client.torrents_add(
urls=torrent.get("urls"),
torrent_files=torrent.get("torrent_files"),
save_path=torrent.get("save_path"),
category="Bangumi"
):
logger.debug(f"[Downloader] Add torrent: {torrent.get('save_path')}")
return True
else:
logger.error(f"[Downloader] Add torrent failed: {torrent.get('save_path')}")
return False

def move_torrent(self, hashes, location):
self.client.move_torrent(hashes=hashes, new_location=location)
Expand Down
23 changes: 15 additions & 8 deletions src/module/manager/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,30 @@


class SeasonCollector(DownloadClient):
def add_season_torrents(self, data: BangumiData, torrents):
for torrent in torrents:
def add_season_torrents(self, data: BangumiData, torrents, torrent_files=None):
if torrent_files:
download_info = {
"url": torrent.torrent_link,
"torrent_files": torrent_files,
"save_path": self._gen_save_path(data),
}
self.add_torrent(download_info)
return self.add_torrent(download_info)
else:
download_info = {
"urls": [torrent.torrent_link for torrent in torrents],
"save_path": self._gen_save_path(data),
}
return self.add_torrent(download_info)

def collect_season(self, data: BangumiData, link: str = None):
def collect_season(self, data: BangumiData, link: str = None, proxy: bool = False):
logger.info(f"Start collecting {data.official_title} Season {data.season}...")
with SearchTorrent() as st:
if not link:
torrents = st.search_season(data)
else:
torrents = st.get_torrents(link)
self.add_season_torrents(data, torrents)
logger.info("Completed!")
torrents = st.get_torrents(link, _filter="|".join(data.filter))
if proxy:
torrent_files = [st.get_content(torrent.torrent_link) for torrent in torrents]
return self.add_season_torrents(data, torrents, torrent_files=torrent_files)

def subscribe_season(self, data: BangumiData):
with BangumiDatabase() as db:
Expand Down
4 changes: 2 additions & 2 deletions src/module/manager/renamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ def rename_subtitles(
if not renamed:
logger.warning(f"[Renamer] {subtitle_path} rename failed")

def rename(self):
def rename(self) -> list[Notification]:
# Get torrent info
logger.debug("[Renamer] Start rename process.")
rename_method = settings.bangumi_manage.rename_method
torrents_info = self.get_torrent_info()
renamed_info = []
renamed_info: list[Notification] = []
for info in torrents_info:
media_list, subtitle_list = self.check_files(info)
bangumi_name, season = self._path_to_bangumi(info.save_path)
Expand Down
5 changes: 2 additions & 3 deletions src/module/network/request_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ def get_torrents(
_url: str,
_filter: str = "|".join(settings.rss_parser.filter),
retry: int = 3,
) -> [TorrentInfo]:
) -> list[TorrentInfo]:
try:
soup = self.get_xml(_url, retry)
torrent_titles, torrent_urls, torrent_homepage = mikan_parser(soup)

torrents = []
torrents: list[TorrentInfo] = []
for _title, torrent_url, homepage in zip(
torrent_titles, torrent_urls, torrent_homepage
):
Expand Down
6 changes: 6 additions & 0 deletions src/module/network/request_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class RequestURL:
def __init__(self):
self.header = {"user-agent": "Mozilla/5.0", "Accept": "application/xml"}
self._socks5_proxy = False

def get_url(self, url, retry=3):
try_time = 0
Expand Down Expand Up @@ -77,6 +78,7 @@ def __enter__(self):
"http": url,
}
elif settings.proxy.type == "socks5":
self._socks5_proxy = True
socks.set_default_proxy(
socks.SOCKS5,
addr=settings.proxy.host,
Expand All @@ -91,4 +93,8 @@ def __enter__(self):
return self

def __exit__(self, exc_type, exc_val, exc_tb):
if self._socks5_proxy:
socks.set_default_proxy()
socket.socket = socks.socksocket
self._socks5_proxy = False
self.session.close()
17 changes: 13 additions & 4 deletions src/module/notification/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@
logger = logging.getLogger(__name__)


def getClient(type=settings.notification.type):
def getClient(type: str):
if type.lower() == "telegram":
return TelegramNotification
elif type.lower() == "server-chan":
return ServerChanNotification
elif type.lower() == "bark":
return BarkNotification
elif type.lower() == "wecom":
return WecomNotification
else:
return None


class PostNotification(getClient()):
class PostNotification:
def __init__(self):
super().__init__(
Notifier = getClient(settings.notification.type)
self.notifier = Notifier(
token=settings.notification.token,
chat_id=settings.notification.chat_id
)
Expand All @@ -46,12 +49,18 @@ def _gen_message(notify: Notification) -> str:
def send_msg(self, notify: Notification) -> bool:
text = self._gen_message(notify)
try:
self.post_msg(text)
self.notifier.post_msg(text)
logger.debug(f"Send notification: {notify.official_title}")
except Exception as e:
logger.warning(f"Failed to send notification: {e}")
return False

def __enter__(self):
self.notifier.__enter__()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.notifier.__exit__(exc_type, exc_val, exc_tb)

if __name__ == "__main__":
info = Notification(
Expand Down
3 changes: 2 additions & 1 deletion src/module/notification/plugin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .bark import BarkNotification
from .server_chan import ServerChanNotification
from .telegram import TelegramNotification
from .telegram import TelegramNotification
from .wecom import WecomNotification
35 changes: 35 additions & 0 deletions src/module/notification/plugin/wecom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import logging
from module.network import RequestContent

logger = logging.getLogger(__name__)


class WecomNotification(RequestContent):
"""企业微信推送 基于图文消息"""

def __init__(self, token, chat_id, **kwargs):
super().__init__()
#Chat_id is used as noti_url in this push tunnel
self.notification_url = f"{chat_id}"
self.token = token

def post_msg(self, text: str) -> bool:
##Change message format to match Wecom push better
info = text.split(":")
print(info)
title = "【番剧更新】" + info[1].split("\n")[0].strip()
msg = info[2].split("\n")[0].strip()+" "+info[3].split("\n")[0].strip()
picurl = info[3].split("\n")[1].strip()
#Default pic to avoid blank in message. Resolution:1068*455
if picurl == "":
picurl = "https://article.biliimg.com/bfs/article/d8bcd0408bf32594fd82f27de7d2c685829d1b2e.png"
data = {
"key":self.token,
"type": "news",
"title": title,
"msg": msg,
"picurl":picurl
}
resp = self.post_data(self.notification_url, data)
logger.debug(f"Wecom notification: {resp.status_code}")
return resp.status_code == 200
4 changes: 2 additions & 2 deletions src/module/parser/analyser/torrent_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
]

SUBTITLE_LANG = {
"zh-tw": ["TC", "CHT", "繁", "zh-tw"],
"zh": ["SC", "CHS", "简", "zh"],
"zh-tw": ["TC", "CHT", "cht", "繁", "zh-tw"],
"zh": ["SC", "CHS", "chs", "简", "zh"],
}


Expand Down

0 comments on commit 10962a0

Please sign in to comment.