sane-bt-search: calculate bitmagnet URI by using the InfoHash field instead of parsing MagnetUri

This commit is contained in:
2025-07-07 22:27:52 +00:00
parent 25aa82b038
commit 33263c27ff

View File

@@ -199,6 +199,7 @@ class Torrent:
size: int
tracker: Tracker
title: str
info_hash: str | None
magnet: str | None
http_dl_uri: str | None # probably a .torrent file but it COULD be a referral to a magnet:// URI
tracker_uri: str | None
@@ -225,8 +226,19 @@ class Torrent:
def from_dict(d: dict) -> 'Torrent':
logger.debug(f"Torrent.from_dict: fields: { ' '.join(d.keys()) }")
for k, v in d.items():
if k not in ("CategoryDesc", "Seeders", "PublishDate", "Size", "Tracker", "Title", "MagnetUri", "Guid", "Link", "Details") and \
v != None and v != "" and v != [] and v != {}:
if k not in (
"CategoryDesc",
"Details",
"Guid",
"InfoHash",
"Link",
"MagnetUri",
"PublishDate",
"Seeders",
"Size",
"Title",
"Tracker",
) and v != None and v != "" and v != [] and v != {}:
logger.debug(f" {k} = {v}")
seeders = d.get("Seeders")
@@ -234,6 +246,7 @@ class Torrent:
size = d.get("Size")
tracker = d.get("Tracker")
title = d.get("Title")
info_hash = d.get("InfoHash")
magnet = d.get("MagnetUri") or d.get("Guid")
http_dl_uri = d.get("Link")
tracker_uri = d.get("Details")
@@ -255,13 +268,24 @@ class Torrent:
tracker_uri = tracker_uri.replace('https://bakabt.me//', 'https://bakabt.me/')
# quirk: bitmagnet doesn't populate the torrent page, except as a link to the website homepage.
# TODO: fix and upstream this into either Jackett or Bitmagnet
# also fix bitmagnet seeders count while here
if tracker_uri == 'http://10.0.1.6:3333/':
infohash = magnet.replace('magnet:?xt=urn:btih:', '').split('&')[0]
tracker_uri = f"https://bitmagnet.uninsane.org/webui/torrents/permalink/{infohash}"
tracker_uri = f"https://bitmagnet.uninsane.org/webui/torrents/permalink/{info_hash}"
if seeders is not None and pub_date is not None and title is not None and (magnet is not None or http_dl_uri is not None):
pub_date = parse_time(pub_date)
return Torrent(seeders, pub_date, size, Tracker.by_name(tracker), title, magnet, http_dl_uri, tracker_uri, categories=categories)
return Torrent(
seeders=seeders,
pub_date=pub_date,
size=size,
tracker=Tracker.by_name(tracker),
title=title,
info_hash=info_hash,
magnet=magnet,
http_dl_uri=http_dl_uri,
tracker_uri=tracker_uri,
categories=categories,
)
def to_dict(self) -> dict:
# N.B.: not all fields: needs to be kept in sync with consumers like mx-sanebot
@@ -338,6 +362,7 @@ def main(args: list[str]):
parser.add_argument('--sort-by', default=SortMethod.Balanced, type=SortMethod, help='how to rank matches (seeders, tracker)')
parser.add_argument('--json', action='store_true', help='output results in json')
# TODO: add `choices`, populated from the API name of each KNOWN_TRACKERS.
# (i.e. 'TrackerId' instead of 'Tracker' field)
parser.add_argument('--tracker', type=str, action='append', help='query only this tracker(s) e.g. "bitmagnet" or "bakabt"')
parser.add_argument('--verbose', action='store_true')
parser.add_argument('--book', action='store_true', help='show only book (ebook or audiobook) results')