From daf030667fd29e2f0c94e6215662c8fb471c59cb Mon Sep 17 00:00:00 2001 From: Audionut Date: Tue, 20 Aug 2024 21:59:41 +1000 Subject: [PATCH] ANT - better .torrent contraints --- src/trackers/ANT.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/trackers/ANT.py b/src/trackers/ANT.py index 1bc0abdc6..670d63595 100644 --- a/src/trackers/ANT.py +++ b/src/trackers/ANT.py @@ -76,10 +76,28 @@ async def upload(self, meta): torrent_filename = "BASE" torrent = Torrent.read(f"{meta['base_dir']}/tmp/{meta['uuid']}/BASE.torrent") total_size = sum(file.size for file in torrent.files) - piece_size = math.ceil(total_size / 999) - piece_size = max(131072, min(piece_size, 16777216)) - if torrent.pieces > 1000: - console.print("[red]Torrent has more than 1000 pieces. Generating a new .torrent") + def calculate_pieces_and_file_size(total_size, piece_size): + num_pieces = math.ceil(total_size / piece_size) + torrent_file_size = 20 + (num_pieces * 20) # Approximate size: 20 bytes header + 20 bytes per piece + return num_pieces, torrent_file_size + + # Start with 4 MiB piece size and adjust if necessary + piece_size = 4194304 # 4 MiB + num_pieces, torrent_file_size = calculate_pieces_and_file_size(total_size, piece_size) + while not (1000 <= num_pieces <= 2000 and torrent_file_size <= 81920): # 80 KiB = 81920 bytes + if num_pieces < 1000: + piece_size //= 2 + if piece_size < 16384: # 16 KiB is the smallest allowed by the BitTorrent spec + piece_size = 16384 + break + elif num_pieces > 2000 or torrent_file_size > 81920: + piece_size *= 2 + num_pieces, torrent_file_size = calculate_pieces_and_file_size(total_size, piece_size) + + if not (1000 <= num_pieces <= 2000): + console.print("[red]Unable to generate a .torrent with the required number of pieces and file size constraints") + else: + console.print("[yellow]Regenerating torrent to fit within 1000-2000 pieces and 80 KiB size limit.") from src.prep import Prep prep = Prep(screens=meta['screens'], img_host=meta['imghost'], config=self.config) prep.create_torrent(meta, Path(meta['path']), "ANT", piece_size_max=piece_size)