mirror of
https://github.com/calibrain/shelfmark.git
synced 2026-11-10 12:28:27 +00:00
## Add Soulseek (slskd) as a release source, building on #1335 This is @dskvr's work from #1335 rebased onto current `main`, with my changes on top. #1335 has been quiet since 2026-09-13 and I needed the changes below, so I carried it forward instead of starting over. Their two commits are the first two in this branch with their authorship intact, and the source plugin, handler, API client, settings tab, tests, `ReleaseProtocol.SOULSEEK`, test client compose file and frontend bits are all theirs. I haven't changed how a search turns into releases beyond what is listed here. If @dskvr would rather keep #1335 alive and take these as commits on their branch, I can do that instead and close this. ### What I changed **Downloads survive other apps using the same slskd.** The handler found its files by listing slskd's downloads, and another app on the same instance (SoulSync here) sends `DELETE /transfers/downloads/all/completed` after each of its batches. Once that ran, a finished download no longer appeared in the list and the task never saw it complete, even though the files were on disk. With the new `SLSKD_ISOLATE_DOWNLOADS` setting (on by default) each task: - enqueues one batch (`POST /transfers/downloads/batches`) into its own folder, `<SLSKD_DESTINATION_PREFIX>/<task id>` - polls each transfer by id, which slskd still answers after the list has been cleared - decides it is done from the files on disk (sizes, not names), with the `/events` log as a fallback when the transfers are gone and the files aren't visible yet - copies out of that folder, and removes it only if `SLSKD_KEEP_COMPLETED` is off and the import worked With isolation off the handler behaves as it did in #1335. If the batch endpoint isn't there (404/405) it falls back to the original per-file enqueue. **Source changes.** m4b/m4a files are their own release next to loose chapter files, so auto-download can prefer one file over a chapter set. Results from the query variants are merged per peer. The folder text is exposed as `extra["author"]` and `extra["title_raw"]`, which is what auto-download's strict match and pack detection read. `is_available()` also needs an API key now. **Smaller fixes.** Cancelling while files are still being located now removes the transfer records it created. In the settings tab, a cleared form field is no longer replaced by the saved value when testing the connection. The new settings are in `docs/environment-variables.md`. ### Testing `tests/slskd` has 192 passing and 6 live tests that skip without an instance. I wrote the new tests first and checked the handler and source rules by breaking them and confirming a test failed. Ruff is clean. On the whole suite the only failures are 4 in `tests/download` that import `seleniumbase`, which isn't installed in my environment, and they fail the same on `main`. I've been running this on my own deployment against slskd 0.26.0 behind a VPN, with SoulSync active on a second instance. A search returned a couple hundred results from about 70 peers, an epub came down and was imported, and auto-download fetched five audiobooks as single m4b files. That is a day of use, so I don't know how it behaves on other slskd versions or on Windows path mappings. --------- Co-authored-by: sandwich <dskvr@users.noreply.github.com> Co-authored-by: sandwich <299465+dskvr@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
128 lines
4.4 KiB
Python
128 lines
4.4 KiB
Python
"""slskd releases survive queueing, retry serialization and a full handler round-trip."""
|
|
|
|
from pathlib import Path
|
|
from threading import Event
|
|
|
|
import pytest
|
|
|
|
from shelfmark.core.models import DownloadTask
|
|
from shelfmark.download import orchestrator
|
|
from shelfmark.release_sources import get_handler
|
|
from shelfmark.release_sources.slskd.handler import SlskdHandler, spec_from_task
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _original_enqueue_path(monkeypatch):
|
|
"""These tests cover the original enqueue-and-poll-the-list path; isolated mode has its own
|
|
tests in test_handler_isolated.py."""
|
|
from shelfmark.release_sources.slskd.handler import SlskdHandler
|
|
|
|
monkeypatch.setattr(SlskdHandler, "_isolate_enabled", lambda self: False)
|
|
|
|
|
|
FILE = "@@share\\Books\\Author\\Book.epub"
|
|
|
|
|
|
def _release_data():
|
|
return {
|
|
"source": "slskd",
|
|
"source_id": "slskd:deadbeef",
|
|
"title": "Book",
|
|
"format": "epub",
|
|
"protocol": "soulseek",
|
|
"content_type": "ebook",
|
|
"extra": {
|
|
"username": "peer",
|
|
"directory": "@@share\\Books\\Author",
|
|
"files": [{"filename": FILE, "size": 1000}],
|
|
"upload_speed": 1,
|
|
},
|
|
}
|
|
|
|
|
|
def test_queue_release_persists_soulseek_spec(monkeypatch):
|
|
captured: dict[str, DownloadTask] = {}
|
|
|
|
def fake_add(task: DownloadTask) -> bool:
|
|
captured["task"] = task
|
|
return True
|
|
|
|
monkeypatch.setattr(orchestrator.config, "get", lambda _key, default=None, **_kw: default)
|
|
monkeypatch.setattr(orchestrator, "_source_unavailable_message", lambda _source: None)
|
|
monkeypatch.setattr(orchestrator.book_queue, "add", fake_add)
|
|
monkeypatch.setattr(orchestrator, "ws_manager", None)
|
|
|
|
ok, error = orchestrator.queue_release(_release_data(), 0)
|
|
|
|
assert ok, error
|
|
task = captured["task"]
|
|
assert task.source == "slskd"
|
|
assert task.task_id == "slskd:deadbeef"
|
|
assert task.retry_download_protocol == "soulseek"
|
|
assert task.retry_download_url is None
|
|
spec = spec_from_task(task)
|
|
assert spec is not None
|
|
assert spec.username == "peer"
|
|
assert spec.filenames == [FILE]
|
|
|
|
|
|
def test_retry_payload_round_trips_soulseek_spec():
|
|
handler = get_handler("slskd")
|
|
fields = handler.build_retry_resolution_fields(_release_data())
|
|
task = DownloadTask(task_id="slskd:deadbeef", source="slskd", title="Book", **fields)
|
|
|
|
payload = orchestrator.serialize_task_for_retry(task)
|
|
restored = orchestrator._restore_task_from_retry_payload(payload)
|
|
|
|
assert restored is not None
|
|
assert spec_from_task(restored) == spec_from_task(task)
|
|
|
|
|
|
def test_handler_round_trip_from_queued_task(monkeypatch, tmp_path):
|
|
"""Queue → task → handler download using the persisted context only (no cache)."""
|
|
import shelfmark.release_sources.slskd.handler as mod
|
|
|
|
handler = get_handler("slskd")
|
|
assert isinstance(handler, SlskdHandler)
|
|
fields = handler.build_retry_resolution_fields(_release_data())
|
|
task = DownloadTask(task_id="slskd:deadbeef", source="slskd", title="Book", **fields)
|
|
|
|
completed = tmp_path / "Author" / "Book.epub"
|
|
completed.parent.mkdir(parents=True)
|
|
completed.write_bytes(b"epub")
|
|
|
|
class Client:
|
|
def enqueue_downloads(self, username, files):
|
|
assert username == "peer"
|
|
return [{"id": "t1", "filename": files[0]["filename"]}]
|
|
|
|
def list_downloads(self, username):
|
|
return [
|
|
{
|
|
"id": "t1",
|
|
"filename": FILE,
|
|
"state": "Completed, Succeeded",
|
|
"bytesTransferred": 1000,
|
|
"removed": False,
|
|
}
|
|
]
|
|
|
|
def cancel_transfer(self, username, transfer_id, *, remove):
|
|
return True
|
|
|
|
def get_download_directory(self):
|
|
return str(tmp_path)
|
|
|
|
monkeypatch.setattr(mod.config, "get", lambda k, d=None: d)
|
|
monkeypatch.setattr(SlskdHandler, "_get_client", lambda self: Client())
|
|
monkeypatch.setattr(SlskdHandler, "_poll_interval", lambda self: 0)
|
|
monkeypatch.setattr(SlskdHandler, "_completed_path_timeout_seconds", lambda self: 0)
|
|
|
|
statuses: list[tuple[str, str | None]] = []
|
|
result = handler.download(task, Event(), lambda p: None, lambda s, m: statuses.append((s, m)))
|
|
|
|
assert result == str(completed)
|
|
assert Path(result).exists()
|
|
assert "error" not in {s for s, _ in statuses}
|
|
assert statuses[0] == ("resolving", "Sending to slskd")
|