- Fix remove torrent bug

This commit is contained in:
EstrellaXD
2023-04-24 15:10:58 +08:00
parent b8906f53f4
commit eb9f51a755

View File

@@ -27,64 +27,6 @@ class Renamer:
torrent_count = len(recent_info)
return recent_info, torrent_count
@staticmethod
def split_path(path: str):
suffix = os.path.splitext(path)[-1]
path = path.replace(settings.downloader.path, "")
path_parts = PurePath(path).parts \
if PurePath(path).name != path \
else PureWindowsPath(path).parts
path_name = path_parts[-1]
try:
if re.search(r"S\d{1,2}|[Ss]eason", path_parts[-2]) is not None:
season = int(re.search(r"\d{1,2}", path_parts[-2]).group())
else:
season = 1
except Exception as e:
logger.debug(e)
logger.debug("No Season info")
season = 1
folder_name = path_parts[1] if path_parts[0] == "/" else path_parts[0]
try:
download_path = path_parts[1]
except IndexError:
download_path = ""
return path_name, season, folder_name, suffix, download_path
def run(self):
recent_info, torrent_count = self.get_torrent_info()
rename_count = 0
for info in recent_info:
name = info.name
torrent_hash = info.hash
path_name, season, folder_name, suffix, _ = self.split_path(info.content_path)
if path_name is folder_name:
logger.warning("Wrong bangumi path, please check your qbittorrent settings.")
else:
try:
new_name = self._renamer.download_parser(name, folder_name, season, suffix, settings.bangumi_manage.rename_method)
if path_name != new_name:
old_path = info.content_path.replace(info.save_path, "")[len(os.path.sep):]
self.client.rename_torrent_file(torrent_hash, old_path, new_name)
rename_count += 1
else:
continue
except Exception as e:
logger.warning(f"{path_name} rename failed")
logger.warning(f"Folder name: {folder_name}, Season: {season}, Suffix: {suffix}")
logger.debug(e)
if settings.bangumi_manage.remove_bad_torrent:
self.client.delete_torrent(torrent_hash)
self.print_result(torrent_count, rename_count)
def set_folder(self):
recent_info, _ = self.get_torrent_info()
for info in recent_info:
torrent_hash = info.hash
_, season, folder_name, _, download_path = self.split_path(info.content_path)
new_path = os.path.join(settings.downloader.path, folder_name, f"Season {season}")
self.client.move_torrent(torrent_hash, new_path)
@staticmethod
def check_files(info, suffix_type: str = "media"):
if suffix_type == "subtitle":
@@ -112,6 +54,8 @@ class Renamer:
logger.warning(f"{old_name} rename failed")
logger.warning(f"Folder name: {folder_name}, Season: {season}, Suffix: {suffix}")
logger.debug(e)
# Delete bad torrent
self.delete_bad_torrent(info)
def rename_collection(self, info, media_list: list[str]):
folder_name, season = self.get_folder_and_season(info.save_path)
@@ -129,6 +73,8 @@ class Renamer:
logger.warning(f"{old_name} rename failed")
logger.warning(f"Folder name: {folder_name}, Season: {season}, Suffix: {suffix}")
logger.debug(e)
# Delete bad torrent.
self.delete_bad_torrent(info)
self.client.set_category(category="BangumiCollection", hashes=_hash)
def rename_subtitles(self, subtitle_list: list[str], media_old_name, media_new_name, _hash):
@@ -139,6 +85,10 @@ class Renamer:
self.client.rename_torrent_file(_hash, subtitle_file, new_subtitle_name)
logger.info(f"Rename subtitles for {media_old_name} to {media_new_name}")
def delete_bad_torrent(self, info):
if settings.bangumi_manage.remove_bad_torrent:
self.client.delete_torrent(info.hash)
logger.info(f"{info.name} have been deleted.")
@staticmethod
def get_folder_and_season(save_path: str):
@@ -167,23 +117,17 @@ class Renamer:
recent_info, torrent_count = self.get_torrent_info()
rename_count = 0
for info in recent_info:
try:
media_list = self.check_files(info)
if len(media_list) == 1:
self.rename_file(info, media_list[0])
rename_count += 1
# TODO: Rename subtitles
elif len(media_list) > 1:
logger.info("Start rename collection")
self.rename_collection(info, media_list)
rename_count += len(media_list)
else:
logger.warning(f"{info.name} has no media file")
except Exception as e:
logger.warning(f"{info.name} rename failed")
logger.debug(e)
if settings.bangumi_manage.remove_bad_torrent:
self.client.delete_torrent(info.hash)
media_list = self.check_files(info)
if len(media_list) == 1:
self.rename_file(info, media_list[0])
rename_count += 1
# TODO: Rename subtitles
elif len(media_list) > 1:
logger.info("Start rename collection")
self.rename_collection(info, media_list)
rename_count += len(media_list)
else:
logger.warning(f"{info.name} has no media file")
if __name__ == '__main__':