fix: support v2 and hybrid qBittorrent identities
This commit is contained in:
@@ -174,7 +174,7 @@ def _v1_files(info: dict[bytes, Any]) -> list[MetaFile]:
|
||||
|
||||
|
||||
def _v2_files(info: dict[bytes, Any]) -> list[MetaFile]:
|
||||
name = _component(info.get(b"name.utf-8", info.get(b"name")))
|
||||
_component(info.get(b"name.utf-8", info.get(b"name")))
|
||||
tree = info.get(b"file tree")
|
||||
if not isinstance(tree, dict):
|
||||
raise BencodeError("v2 metainfo has no file tree")
|
||||
@@ -186,7 +186,7 @@ def _v2_files(info: dict[bytes, Any]) -> list[MetaFile]:
|
||||
if not isinstance(leaf, dict):
|
||||
raise BencodeError("invalid v2 file leaf")
|
||||
result.append(MetaFile(
|
||||
"/".join([name] + components),
|
||||
"/".join(components),
|
||||
_length(leaf.get(b"length")),
|
||||
_padding(leaf),
|
||||
))
|
||||
|
||||
@@ -79,8 +79,9 @@ def remove_qb_entry(
|
||||
marker = store.get_job_artifact(job_id, "qb-entry-removed")
|
||||
if marker is not None:
|
||||
return
|
||||
if qbittorrent.get_resource(torrent_hash) is not None:
|
||||
qbittorrent.delete_entry(torrent_hash)
|
||||
resource = qbittorrent.get_resource(torrent_hash)
|
||||
if resource is not None:
|
||||
qbittorrent.delete_entry(resource.summary.qb_torrent_id)
|
||||
store.put_job_artifact(
|
||||
job_id, "qb-entry-removed", {"torrent_hash": torrent_hash}
|
||||
)
|
||||
|
||||
@@ -686,6 +686,12 @@ class ClientJobExecutor:
|
||||
self.qb_api_root.as_posix(),
|
||||
info_hash,
|
||||
)
|
||||
resource = self.qbittorrent.get_resource(info_hash)
|
||||
if resource is None:
|
||||
raise JobExecutionError(
|
||||
"added qBittorrent resource did not become visible"
|
||||
)
|
||||
qb_torrent_id = resource.summary.qb_torrent_id
|
||||
target_union = _union_selection(
|
||||
definition.transfer.target_baseline_files,
|
||||
definition.transfer.transfer_delta_files,
|
||||
@@ -694,9 +700,11 @@ class ClientJobExecutor:
|
||||
total_files = len(
|
||||
decode_message_metainfo(metainfo_path).files
|
||||
)
|
||||
self.qbittorrent.set_selection(info_hash, selected, total_files)
|
||||
self.qbittorrent.set_selection(
|
||||
qb_torrent_id, selected, total_files
|
||||
)
|
||||
self.qbittorrent.recheck_and_wait(
|
||||
info_hash,
|
||||
qb_torrent_id,
|
||||
selected,
|
||||
timeout=self.verification_timeout,
|
||||
poll_interval=self.poll_interval,
|
||||
@@ -708,7 +716,7 @@ class ClientJobExecutor:
|
||||
),
|
||||
)
|
||||
if should_start:
|
||||
self.qbittorrent.start(info_hash)
|
||||
self.qbittorrent.start(qb_torrent_id)
|
||||
verified = self.qbittorrent.get_resource(info_hash)
|
||||
if verified is None:
|
||||
raise JobExecutionError(
|
||||
@@ -753,18 +761,19 @@ class ClientJobExecutor:
|
||||
info_hash = _info_hash(definition)
|
||||
present = self.qbittorrent.get_resource(info_hash)
|
||||
if present is not None:
|
||||
qb_torrent_id = present.summary.qb_torrent_id
|
||||
if baseline.get("present"):
|
||||
selected = baseline.get("selected_file_indices")
|
||||
if isinstance(selected, list) and selected:
|
||||
self.qbittorrent.set_selection(
|
||||
info_hash, selected, len(present.files)
|
||||
qb_torrent_id, selected, len(present.files)
|
||||
)
|
||||
if baseline.get("stopped"):
|
||||
self.qbittorrent.stop(info_hash)
|
||||
self.qbittorrent.stop(qb_torrent_id)
|
||||
else:
|
||||
self.qbittorrent.start(info_hash)
|
||||
self.qbittorrent.start(qb_torrent_id)
|
||||
else:
|
||||
self.qbittorrent.delete_entry(info_hash)
|
||||
self.qbittorrent.delete_entry(qb_torrent_id)
|
||||
compensate_materialized_files(
|
||||
job_id=definition.job_id,
|
||||
qb_root=self.qb_root,
|
||||
|
||||
@@ -76,21 +76,11 @@ class QBittorrentReader:
|
||||
return result
|
||||
|
||||
def get_resource(self, torrent_hash: str) -> NormalizedResource | None:
|
||||
torrents = self._json(
|
||||
"/api/v2/torrents/info", {"hashes": torrent_hash.lower()}
|
||||
)
|
||||
if not isinstance(torrents, list):
|
||||
raise QBittorrentError("qBittorrent lookup response is invalid")
|
||||
exact = [
|
||||
torrent for torrent in torrents
|
||||
if isinstance(torrent, dict)
|
||||
and str(torrent.get("hash", "")).lower() == torrent_hash.lower()
|
||||
]
|
||||
if not exact:
|
||||
torrent = self._lookup_torrent(torrent_hash)
|
||||
if torrent is None:
|
||||
return None
|
||||
if len(exact) != 1:
|
||||
raise QBittorrentError("qBittorrent lookup is ambiguous")
|
||||
return self._normalize(exact[0], torrent_hash)
|
||||
qb_torrent_id = str(torrent["hash"]).lower()
|
||||
return self._normalize(torrent, qb_torrent_id)
|
||||
|
||||
def add_stopped(
|
||||
self,
|
||||
@@ -164,6 +154,7 @@ class QBittorrentReader:
|
||||
selected_file_indices: list[int] | tuple[int, ...],
|
||||
total_file_count: int,
|
||||
) -> None:
|
||||
torrent_hash = self._require_torrent_hash(torrent_hash)
|
||||
selected = sorted(set(selected_file_indices))
|
||||
if total_file_count < 1:
|
||||
raise QBittorrentError("torrent file count must be positive")
|
||||
@@ -189,6 +180,7 @@ class QBittorrentReader:
|
||||
)
|
||||
|
||||
def stop(self, torrent_hash: str) -> None:
|
||||
torrent_hash = self._require_torrent_hash(torrent_hash)
|
||||
try:
|
||||
self._post_form(
|
||||
"/api/v2/torrents/stop", {"hashes": torrent_hash}
|
||||
@@ -201,6 +193,7 @@ class QBittorrentReader:
|
||||
)
|
||||
|
||||
def start(self, torrent_hash: str) -> None:
|
||||
torrent_hash = self._require_torrent_hash(torrent_hash)
|
||||
try:
|
||||
self._post_form(
|
||||
"/api/v2/torrents/start", {"hashes": torrent_hash}
|
||||
@@ -234,20 +227,10 @@ class QBittorrentReader:
|
||||
)
|
||||
|
||||
def _torrent_is_present(self, torrent_hash: str) -> bool:
|
||||
torrents = self._json(
|
||||
"/api/v2/torrents/info", {"hashes": torrent_hash}
|
||||
)
|
||||
if not isinstance(torrents, list):
|
||||
raise QBittorrentError(
|
||||
"qBittorrent lookup response is invalid"
|
||||
)
|
||||
return any(
|
||||
isinstance(item, dict)
|
||||
and str(item.get("hash", "")).lower() == torrent_hash.lower()
|
||||
for item in torrents
|
||||
)
|
||||
return self._lookup_torrent(torrent_hash) is not None
|
||||
|
||||
def delete_entry(self, torrent_hash: str) -> None:
|
||||
torrent_hash = self._require_torrent_hash(torrent_hash)
|
||||
self._post_form(
|
||||
"/api/v2/torrents/delete",
|
||||
{"hashes": torrent_hash, "deleteFiles": "false"},
|
||||
@@ -263,6 +246,7 @@ class QBittorrentReader:
|
||||
cancel_check: Callable[[], None] | None = None,
|
||||
progress_callback: Callable[[float], None] | None = None,
|
||||
) -> RecheckResult:
|
||||
torrent_hash = self._require_torrent_hash(torrent_hash)
|
||||
selected = tuple(sorted(set(selected_file_indices)))
|
||||
if not selected:
|
||||
raise QBittorrentError("recheck selection cannot be empty")
|
||||
@@ -324,19 +308,55 @@ class QBittorrentReader:
|
||||
time.sleep(min(poll_interval, max(0, deadline - time.monotonic())))
|
||||
|
||||
def _torrent_record(self, torrent_hash: str) -> dict[str, Any]:
|
||||
torrents = self._json(
|
||||
"/api/v2/torrents/info", {"hashes": torrent_hash}
|
||||
)
|
||||
torrent = self._lookup_torrent(torrent_hash)
|
||||
if torrent is None:
|
||||
raise QBittorrentError("qBittorrent target torrent is missing")
|
||||
return torrent
|
||||
|
||||
def _require_torrent_hash(self, torrent_hash: str) -> str:
|
||||
requested = torrent_hash.lower()
|
||||
if len(requested) != 64:
|
||||
return requested
|
||||
torrent = self._lookup_torrent(torrent_hash)
|
||||
if torrent is None:
|
||||
raise QBittorrentError("qBittorrent target torrent is missing")
|
||||
return str(torrent["hash"]).lower()
|
||||
|
||||
def _lookup_torrent(self, torrent_hash: str) -> dict[str, Any] | None:
|
||||
requested = torrent_hash.lower()
|
||||
# qBittorrent 5 uses the truncated v2 hash as `hash` for both pure-v2
|
||||
# and hybrid torrents. Its `hashes` filter does not match a hybrid's
|
||||
# v1 alias, so identity lookup must inspect all advertised identities.
|
||||
torrents = self._json("/api/v2/torrents/info")
|
||||
if not isinstance(torrents, list):
|
||||
raise QBittorrentError("qBittorrent lookup response is invalid")
|
||||
exact = [
|
||||
item for item in torrents
|
||||
if isinstance(item, dict)
|
||||
and str(item.get("hash", "")).lower() == torrent_hash.lower()
|
||||
and requested in {
|
||||
str(item.get("hash", "")).lower(),
|
||||
str(item.get("infohash_v1", "")).lower(),
|
||||
str(item.get("infohash_v2", "")).lower(),
|
||||
}
|
||||
]
|
||||
if len(exact) != 1:
|
||||
raise QBittorrentError("qBittorrent target torrent is missing")
|
||||
return exact[0]
|
||||
if len(exact) == 1:
|
||||
return exact[0]
|
||||
if len(exact) > 1:
|
||||
raise QBittorrentError("qBittorrent lookup is ambiguous")
|
||||
truncated = [
|
||||
item for item in torrents
|
||||
if (
|
||||
len(requested) == 64
|
||||
and isinstance(item, dict)
|
||||
and len(str(item.get("hash", ""))) == 40
|
||||
and requested.startswith(str(item["hash"]).lower())
|
||||
)
|
||||
]
|
||||
if len(truncated) == 1:
|
||||
return truncated[0]
|
||||
if len(truncated) > 1:
|
||||
raise QBittorrentError("qBittorrent lookup is ambiguous")
|
||||
return None
|
||||
|
||||
def _downloaded_bytes(self, torrent_hash: str) -> int:
|
||||
properties = self._json(
|
||||
|
||||
Reference in New Issue
Block a user