2.5.11 若干调整

- 优化整理逻辑
- 通知组件
This commit is contained in:
EstrellaXD
2022-07-15 17:05:42 +08:00
parent ebf332b181
commit b306b6dc94
6 changed files with 128 additions and 4 deletions

View File

@@ -76,7 +76,7 @@
***计划开发的功能:***
- Web UI
- Web UI #57
- 更为智能细致的分类预设。
# 声明

View File

@@ -1,7 +1,6 @@
import re
import logging
import os
from attrdict import AttrDict
from downloader import getClient
from downloader.exceptions import ConflictError
@@ -88,7 +87,7 @@ class DownloadClient:
# logger.info("to rule.")
logger.debug("Finished.")
def get_torrent_info(self) -> [AttrDict]:
def get_torrent_info(self):
return self.client.torrents_info(
status_filter="completed", category="Bangumi"
)
@@ -122,7 +121,19 @@ class DownloadClient:
self.client.rss_add_feed(url=rss_link, item_path=item_path)
logger.info("Add RSS Feed successfully.")
def get_download_rules(self):
return self.client.get_download_rule()
def get_torrent_path(self, hashes):
return self.client.get_torrent_path(hashes)
if __name__ == "__main__":
from conf.const_dev import DEV_SETTINGS
settings.init(DEV_SETTINGS)
put = DownloadClient()
info = put.get_torrent_info()
for i in info:
print(i.name)

View File

@@ -0,0 +1,91 @@
import re
from dataclasses import dataclass
from pathlib import PurePath, PureWindowsPath
from core import DownloadClient
from conf import settings
from utils import json_config
@dataclass
class RuleInfo:
rule_name: str
season: int
folder_name: str
@dataclass
class RePathInfo:
name: str
season: int
hashes: list
class RePath:
def __init__(self, download_client: DownloadClient):
self._client = download_client
self.re_season = re.compile(r"S\d{1,2}")
@staticmethod
def get_data() -> list:
data = json_config.load(settings.info_path)
return data.get("bangumi_info")
@staticmethod
def analyse_path(path: str):
path_parts = PurePath(path).parts
folder_name = path_parts[-2]
season_folder = path_parts[-1]
season = int(re.search(r"\d{1,2}", season_folder).group())
return season, folder_name
def get_rule(self) -> [RuleInfo]:
rules = self._client.get_download_rules()
all_rule = []
for rule in rules:
path = rules.get(rule).savePath
season, folder_name = self.analyse_path(path)
all_rule.append(RuleInfo(rule, season, folder_name))
return all_rule
def get_difference(self, bangumi_data: list, rules: list):
different_data = []
for rule in rules:
for item in bangumi_data:
if item["official_title"] == self.re_season.sub("", rule.rule_name).strip():
if item["season"] != rule.season:
item["season"] = rule.season
item["official_title"] = self.re_season.sub("", rule.rule_name).strip()
different_data.append(item)
break
return different_data
def get_matched_torrents_list(self, difference_data: list) -> [RePathInfo]:
infos = self._client.get_torrent_info()
repath_list = []
for data in difference_data:
for info in infos:
if re.search(data["raw_title"], info.name):
repath_list.append(RePathInfo(data["name"], data["season"], [info.hash]))
return repath_list
def re_path(self, hashes, season):
old_path = self._client.get_torrent_path(hashes)
new_path = re.sub(r"Season \d", f"Season {season}", old_path)
self._client.move_torrent(hashes, new_path)
# def get_hashes(self):
def run(self, bangumi_data: list):
rules = self.get_rule()
different_data = self.get_difference(bangumi_data, rules)
repath_list = self.get_matched_torrents_list(different_data)
for group in repath_list:
self.re_path(group.hashes, group.season)
if __name__ == '__main__':
from conf.const_dev import DEV_SETTINGS
settings.init(DEV_SETTINGS)
client = DownloadClient()
r = RePath(client)
data = []
r.run(data)

View File

@@ -87,6 +87,12 @@ class QbDownloader:
def move_torrent(self, hashes, new_location):
self._client.torrents_set_location(new_location, hashes)
def get_download_rule(self):
return self._client.rss_rules()
def get_torrent_path(self, hash):
return self._client.torrents_info(hashes=hash)[0].save_path
if __name__ == "__main__":
try:
@@ -95,4 +101,5 @@ if __name__ == "__main__":
logger.debug("Please copy `const_dev.py` to `const_dev.py` to use custom settings")
settings.init(DEV_SETTINGS)
client = QbDownloader(settings.host_ip, settings.user_name, settings.password)
client.rss_remove_item("Mikan_RSS")
path = client.get_torrent_path("39adad0d0c82ebb3971810a7592e03138b7345d2")
print(path)

View File

@@ -2,6 +2,7 @@ import re
from dataclasses import dataclass
from network.request import RequestURL
from .notification import PostNotification
from conf import settings

View File

@@ -0,0 +1,14 @@
import requests
from conf import settings
class PostNotification:
def __init__(self):
self.token = settings.notification_token
self.notification_url = lambda message: f"https://api.pushbullet.com/v2/{self.token}/{message}"
def ifttt_post(self, message):
url = self.notification_url(message)
response = requests.get(url)
return response.status_code == 200