diff --git a/CHANGELOG b/CHANGELOG index 626c9502..58bc754f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ - pre-commit updated to 3.3.3 - requests updated to 2.30.0 - ruamel.yaml updated to 0.17.26 +- Adds new dependency bencodepy to generate hash for cross-seed # Bug Fixes - Changes HardLink Logic (Thanks to @ColinHebert for the suggestion) Fixes #291 @@ -10,5 +11,7 @@ - Fixes Remove Orphan crashing when multiprocessing (Thanks to @buthed010203 #289) - Fixes Remove Orphan from crashing in Windows (Fixes #275) - Fixes #292 +- Fixes #201 +- Fixes #279 **Full Changelog**: https://github.com/StuffAnThings/qbit_manage/compare/v3.6.2...v3.6.3 diff --git a/modules/core/cross_seed.py b/modules/core/cross_seed.py index cbad7faa..ffbd6152 100644 --- a/modules/core/cross_seed.py +++ b/modules/core/cross_seed.py @@ -2,6 +2,7 @@ from collections import Counter from modules import util +from modules.torrent_hash_generator import TorrentHashGenerator logger = util.logger @@ -67,7 +68,23 @@ def cross_seed(self): self.client.torrents.add( torrent_files=src, save_path=dest, category=category, tags="cross-seed", is_paused=True ) - util.move_files(src, dir_cs_out) + self.qbt.torrentinfo[t_name]["count"] += 1 + try: + torrent_hash_generator = TorrentHashGenerator(src) + torrent_hash = torrent_hash_generator.generate_torrent_hash() + util.move_files(src, dir_cs_out) + except Exception as e: + logger.warning(f"Unable to generate torrent hash from cross-seed {t_name}: {e}") + try: + if torrent_hash: + torrent_info = self.qbt.get_torrents({"torrent_hashes": torrent_hash}) + except Exception as e: + logger.warning(f"Unable to find hash {torrent_hash} in qbt: {e}") + if torrent_info: + torrent = torrent_info[0] + self.qbt.torrentvalid.append(torrent) + self.qbt.torrentinfo[t_name]["torrents"].append(torrent) + self.qbt.torrent_list.append(torrent) else: logger.print_line(f"Found {t_name} in {dir_cs} but original torrent is not complete.", self.config.loglevel) logger.print_line("Not adding to qBittorrent", self.config.loglevel) diff --git a/modules/torrent_hash_generator.py b/modules/torrent_hash_generator.py new file mode 100644 index 00000000..d30bda8d --- /dev/null +++ b/modules/torrent_hash_generator.py @@ -0,0 +1,25 @@ +import hashlib + +import bencodepy + + +class TorrentHashGenerator: + def __init__(self, torrent_file_path): + self.torrent_file_path = torrent_file_path + + def generate_torrent_hash(self): + try: + with open(self.torrent_file_path, "rb") as torrent_file: + torrent_data = torrent_file.read() + + try: + torrent_info = bencodepy.decode(torrent_data) + info_data = bencodepy.encode(torrent_info[b"info"]) + info_hash = hashlib.sha1(info_data).hexdigest() + return info_hash + except KeyError: + raise ValueError("Invalid .torrent file format. 'info' key not found.") + except FileNotFoundError: + raise FileNotFoundError(f"Torrent file '{self.torrent_file_path}' not found.") + except Exception as e: + raise Exception(f"Error: {e}") diff --git a/qbit_manage.py b/qbit_manage.py index 0250a757..b1bd71d6 100755 --- a/qbit_manage.py +++ b/qbit_manage.py @@ -393,6 +393,12 @@ def finished_run(): if cfg.commands["tag_update"]: stats["tagged"] += Tags(qbit_manager).stats + # Set Cross Seed + if cfg.commands["cross_seed"]: + cross_seed = CrossSeed(qbit_manager) + stats["added"] += cross_seed.stats_added + stats["tagged"] += cross_seed.stats_tagged + # Remove Unregistered Torrents and tag errors if cfg.commands["rem_unregistered"] or cfg.commands["tag_tracker_error"]: rem_unreg = RemoveUnregistered(qbit_manager) @@ -403,12 +409,6 @@ def finished_run(): stats["untagged_tracker_error"] += rem_unreg.stats_untagged stats["tagged"] += rem_unreg.stats_tagged - # Set Cross Seed - if cfg.commands["cross_seed"]: - cross_seed = CrossSeed(qbit_manager) - stats["added"] += cross_seed.stats_added - stats["tagged"] += cross_seed.stats_tagged - # Recheck Torrents if cfg.commands["recheck"]: recheck = ReCheck(qbit_manager) diff --git a/requirements.txt b/requirements.txt index 3df979a8..a5f2395f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +bencodepy==0.9.5 flake8==6.0.0 pre-commit==3.3.2 qbittorrent-api==2023.4.47