From 4e592f77ab928e90545bcad9045490d15bba0d41 Mon Sep 17 00:00:00 2001 From: L4cache <42721712+L4cache@users.noreply.github.com> Date: Sun, 31 May 2026 02:15:46 +0800 Subject: [PATCH] support multiple tiers of tracker --- torrentfile/cli.py | 10 ++++++++-- torrentfile/edit.py | 15 ++++++++++----- torrentfile/torrent.py | 9 ++++++++- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/torrentfile/cli.py b/torrentfile/cli.py index 7a33a35..8d8dedb 100644 --- a/torrentfile/cli.py +++ b/torrentfile/cli.py @@ -280,7 +280,10 @@ def execute(args: List[str] = None) -> List[str]: metavar="", nargs="+", default=[], - help="one or more space-seperated tracker url(s)", + help=""" + One or more space-seperated tracker url(s), each will be in its own + tier, if a url is prefixed by '+', it will be added to previous tier. + """, ) create_parser.add_argument( @@ -443,7 +446,10 @@ def execute(args: List[str] = None) -> List[str]: dest="announce", metavar="", nargs="+", - help="replace current trackers with one or more urls", + help=""" + Replace current trackers with one or more urls, each will be in its own + tier, if a url is prefixed by '+', it will be added to previous tier. + """, ) edit_parser.add_argument( diff --git a/torrentfile/edit.py b/torrentfile/edit.py index 12272a7..d8297d8 100644 --- a/torrentfile/edit.py +++ b/torrentfile/edit.py @@ -95,12 +95,17 @@ def edit_torrent(metafile: str, args: dict) -> dict: if "announce" in args: val = args.get("announce", None) if isinstance(val, str): - vallist = val.split() - meta["announce"] = vallist[0] - meta["announce-list"] = [vallist] - elif isinstance(val, list): + val = val.split() + if isinstance(val, list): + if val[0].startswith("+"): + val[0] = val[0][1:] meta["announce"] = val[0] - meta["announce-list"] = [val] + meta["announce-list"] = [] + for i in val: + if i.startswith("+"): + meta["announce-list"][-1].append(i[1:]) + else: + meta["announce-list"].append([i]) if "url-list" in args: val = args.get("url-list") diff --git a/torrentfile/torrent.py b/torrentfile/torrent.py index c99deb7..d42803d 100644 --- a/torrentfile/torrent.py +++ b/torrentfile/torrent.py @@ -322,7 +322,14 @@ def __init__( self.announce, self.announce_list = announce, [[announce]] elif isinstance(announce, Sequence): - self.announce, self.announce_list = announce[0], [announce] + if announce[0].startswith("+"): + announce[0] = announce[0][1:] + self.announce, self.announce_list = announce[0], [] + for i in announce: + if i.startswith("+"): + self.announce_list[-1].append(i[1:]) + else: + self.announce_list.append([i]) self.align = align