Skip to content

Commit

Permalink
Fixes #201, Fixes #279
Browse files Browse the repository at this point in the history
  • Loading branch information
bobokun committed May 22, 2023
1 parent aee6a32 commit 9684bf3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
19 changes: 18 additions & 1 deletion modules/core/cross_seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from collections import Counter

from modules import util
from modules.torrent_hash_generator import TorrentHashGenerator

logger = util.logger

Expand Down Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions modules/torrent_hash_generator.py
Original file line number Diff line number Diff line change
@@ -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}")
12 changes: 6 additions & 6 deletions qbit_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
bencodepy==0.9.5
flake8==6.0.0
pre-commit==3.3.2
qbittorrent-api==2023.4.47
Expand Down

0 comments on commit 9684bf3

Please sign in to comment.