fix(navidrome): count scrobbles by elapsed playback time (#7609)

This commit is contained in:
Cat
2026-11-25 21:44:11 +08:00
committed by GitHub
parent a25555ff68
commit e2175d929c
2 changed files with 57 additions and 5 deletions

View File

@@ -744,8 +744,17 @@ class Navidrome:
"""
通过 Subsonic `scrobble` 接口同步 Navidrome 正在播放状态与完播计数。
起播时上报 `submission=false` 激活正在播放列表;停止时若满足 Last.fm/Subsonic 规则
(已播 >= 50% 或累计听满 4 分钟)且尚未计次,则提交 `submission=true`。
起播时上报 `submission=false` 激活正在播放列表。停止时优先按客户端上报的累计
收听时长判断,达到曲目时长的 50% 或 4 分钟且尚未计次时,提交 `submission=true`。
:param item_id: Navidrome 曲目标识
:param position_ms: 播放位置(毫秒);仅在未上报累计时长时用于估算
:param duration_ms: 曲目总时长(毫秒);缺失时只按 4 分钟判断
:param state: 播放会话事件状态
:param playback_time_ms: 本次播放累计收听时长(毫秒);None 表示客户端未提供
:param started_reported: 是否已经上报过起播
:param scrobbled: 是否已经提交过本次播放计数
:return: 上报结果及本次播放是否已计次
"""
_ = (
media_source_id,
@@ -776,9 +785,9 @@ class Navidrome:
if state == "stopped":
if scrobbled:
return {"reported": True, "mode": "scrobble", "started_sent": started_reported, "scrobbled": True}
reached_half = bool(duration_ms and duration_ms > 0 and (position_ms / duration_ms) >= 0.5)
reached_four_min = (playback_time_ms is not None and playback_time_ms >= 240_000) or position_ms >= 240_000
if reached_half or reached_four_min:
listened_ms = position_ms if playback_time_ms is None else playback_time_ms
reached_half = bool(duration_ms and duration_ms > 0 and listened_ms * 2 >= duration_ms)
if reached_half or listened_ms >= 240_000:
ok = self._call("scrobble", id=item_id, submission="true", time=int(time.time() * 1000)) is not None
return {
"reported": ok,

View File

@@ -335,6 +335,49 @@ def test_navidrome_subsonic_scrobble_now_playing_and_threshold() -> None:
assert calls[-1][1]["id"] == "song-1"
@pytest.mark.parametrize(
("position_ms", "duration_ms", "playback_time_ms", "should_scrobble"),
[
pytest.param(480_000, 600_000, 2_000, False, id="seek-ahead"),
pytest.param(250_000, None, 0, False, id="reported-zero"),
pytest.param(10_000, 180_000, 90_000, True, id="elapsed-half"),
pytest.param(10_000, 900_000, 240_000, True, id="elapsed-four-minutes"),
pytest.param(250_000, None, None, True, id="legacy-position"),
],
)
def test_navidrome_scrobble_prefers_elapsed_listening_time(
monkeypatch: pytest.MonkeyPatch,
position_ms: int,
duration_ms: Optional[int],
playback_time_ms: Optional[int],
should_scrobble: bool,
) -> None:
"""
有累计播放时长时按实际收听计次;拖动进度条不算收听,旧客户端仍可按位置估算。
"""
client = object.__new__(Navidrome)
client.user = "music_user"
request = Mock(return_value={"status": "ok"})
monkeypatch.setattr(client, "_call", request)
result = client.report_progress(
"song-1",
position_ms=position_ms,
duration_ms=duration_ms,
state="stopped",
playback_time_ms=playback_time_ms,
)
assert result["reported"] is True
assert result["scrobbled"] is should_scrobble
if should_scrobble:
request.assert_called_once()
assert request.call_args.args == ("scrobble",)
assert request.call_args.kwargs["submission"] == "true"
else:
request.assert_not_called()
def test_upstream_stream_strips_extra_headers_on_cross_origin_redirect(monkeypatch: pytest.MonkeyPatch) -> None:
"""验证上游流代理在同源主机透传会话头,遇跨源 302 重定向自动剥离会话头。"""
captured_calls: list[tuple[str, dict[str, str]]] = []