flacfetch 0.37.0__tar.gz → 0.37.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. {flacfetch-0.37.0/flacfetch.egg-info → flacfetch-0.37.1}/PKG-INFO +1 -1
  2. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/downloaders/spotify.py +39 -4
  3. {flacfetch-0.37.0 → flacfetch-0.37.1/flacfetch.egg-info}/PKG-INFO +1 -1
  4. {flacfetch-0.37.0 → flacfetch-0.37.1}/pyproject.toml +1 -1
  5. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_spotify.py +83 -0
  6. {flacfetch-0.37.0 → flacfetch-0.37.1}/ARCHITECTURE.md +0 -0
  7. {flacfetch-0.37.0 → flacfetch-0.37.1}/LICENSE +0 -0
  8. {flacfetch-0.37.0 → flacfetch-0.37.1}/MANIFEST.in +0 -0
  9. {flacfetch-0.37.0 → flacfetch-0.37.1}/PLAN.md +0 -0
  10. {flacfetch-0.37.0 → flacfetch-0.37.1}/README.md +0 -0
  11. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/__init__.py +0 -0
  12. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/__init__.py +0 -0
  13. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/auth.py +0 -0
  14. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/main.py +0 -0
  15. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/models.py +0 -0
  16. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/routes/__init__.py +0 -0
  17. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/routes/cache.py +0 -0
  18. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/routes/config.py +0 -0
  19. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/routes/credentials.py +0 -0
  20. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/routes/download.py +0 -0
  21. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/routes/health.py +0 -0
  22. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/routes/search.py +0 -0
  23. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/routes/spotify.py +0 -0
  24. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/routes/torrents.py +0 -0
  25. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/services/__init__.py +0 -0
  26. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/services/credential_check.py +0 -0
  27. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/services/disk_manager.py +0 -0
  28. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/services/download_manager.py +0 -0
  29. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/services/health_check.py +0 -0
  30. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/services/search_cache.py +0 -0
  31. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/services/secret_manager_utils.py +0 -0
  32. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/api/services/spotify_budget.py +0 -0
  33. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/core/album_match.py +0 -0
  34. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/core/categorize.py +0 -0
  35. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/core/config.py +0 -0
  36. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/core/interfaces.py +0 -0
  37. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/core/log.py +0 -0
  38. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/core/manager.py +0 -0
  39. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/core/matching.py +0 -0
  40. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/core/models.py +0 -0
  41. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/credential_keeper/__init__.py +0 -0
  42. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/credential_keeper/__main__.py +0 -0
  43. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/credential_keeper/browser.py +0 -0
  44. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/credential_keeper/google_login.py +0 -0
  45. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/credential_keeper/keeper.py +0 -0
  46. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/credential_keeper/librespot.py +0 -0
  47. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/credential_keeper/probe.py +0 -0
  48. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/credential_keeper/refresh_request.py +0 -0
  49. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/credential_keeper/spotify.py +0 -0
  50. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/credential_keeper/youtube.py +0 -0
  51. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/downloaders/torrent.py +0 -0
  52. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/downloaders/torrent_coordinator.py +0 -0
  53. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/downloaders/youtube.py +0 -0
  54. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/interface/cli.py +0 -0
  55. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/interface/cli_remote.py +0 -0
  56. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/providers/gazelle.py +0 -0
  57. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/providers/ops.py +0 -0
  58. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/providers/red.py +0 -0
  59. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/providers/spotify.py +0 -0
  60. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch/providers/youtube.py +0 -0
  61. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch.egg-info/SOURCES.txt +0 -0
  62. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch.egg-info/dependency_links.txt +0 -0
  63. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch.egg-info/entry_points.txt +0 -0
  64. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch.egg-info/requires.txt +0 -0
  65. {flacfetch-0.37.0 → flacfetch-0.37.1}/flacfetch.egg-info/top_level.txt +0 -0
  66. {flacfetch-0.37.0 → flacfetch-0.37.1}/requirements.txt +0 -0
  67. {flacfetch-0.37.0 → flacfetch-0.37.1}/setup.cfg +0 -0
  68. {flacfetch-0.37.0 → flacfetch-0.37.1}/setup.py +0 -0
  69. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/_route_paths.py +0 -0
  70. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_album_search.py +0 -0
  71. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_api_auth.py +0 -0
  72. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_api_config.py +0 -0
  73. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_api_deep_health.py +0 -0
  74. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_api_health_ytdlp.py +0 -0
  75. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_api_integration.py +0 -0
  76. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_api_models.py +0 -0
  77. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_api_routes.py +0 -0
  78. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_api_services.py +0 -0
  79. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_cache_api.py +0 -0
  80. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_cli_cookies.py +0 -0
  81. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_cli_remote.py +0 -0
  82. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_cookie_refresh_request.py +0 -0
  83. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_core.py +0 -0
  84. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_credential_keeper.py +0 -0
  85. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_gazelle.py +0 -0
  86. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_keeper_leaves_youtube.py +0 -0
  87. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_keeper_probe.py +0 -0
  88. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_logging.py +0 -0
  89. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_manager.py +0 -0
  90. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_matching.py +0 -0
  91. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_models.py +0 -0
  92. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_ops.py +0 -0
  93. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_provider_errors.py +0 -0
  94. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_red.py +0 -0
  95. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_search_api.py +0 -0
  96. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_spotify_budget.py +0 -0
  97. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_torrent_coordinator.py +0 -0
  98. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_torrent_downloader.py +0 -0
  99. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_torrent_shared_album.py +0 -0
  100. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_transcode_hf_noise.py +0 -0
  101. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_youtube.py +0 -0
  102. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_youtube_cookies.py +0 -0
  103. {flacfetch-0.37.0 → flacfetch-0.37.1}/tests/test_youtube_provider.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flacfetch
3
- Version: 0.37.0
3
+ Version: 0.37.1
4
4
  Summary: Search and download high-quality audio from multiple sources
5
5
  Home-page: https://github.com/yourusername/flacfetch
6
6
  Author: Your Name
@@ -37,6 +37,15 @@ SAMPLE_RATE = 44100
37
37
  CHANNELS = 2
38
38
  BIT_DEPTH = 16
39
39
 
40
+ # Used when a track's duration can't be resolved (see _resolve_duration_secs).
41
+ DEFAULT_DURATION_SECS = 300
42
+
43
+ # A capture longer than the track by more than this is refused: it means
44
+ # librespot played on past the requested track (e.g. Spotify autoplay queued a
45
+ # "similar" track), so the PCM contains someone else's audio. Durations are
46
+ # floored to whole seconds, so allow a small margin.
47
+ MAX_CAPTURE_OVERRUN_SECS = 5
48
+
40
49
  # Serialize all Spotify captures across the process.
41
50
  #
42
51
  # A Spotify download works by registering a librespot Spotify Connect device on
@@ -153,6 +162,11 @@ class SpotifyDownloader(Downloader):
153
162
  def _resolve_duration_secs(
154
163
  self, sp: "spotipy.Spotify", track_id: str, release: Release
155
164
  ) -> int:
165
+ return self._resolve_duration(sp, track_id, release)[0]
166
+
167
+ def _resolve_duration(
168
+ self, sp: "spotipy.Spotify", track_id: str, release: Release
169
+ ) -> tuple[int, bool]:
156
170
  """Resolve track duration in seconds.
157
171
 
158
172
  The PCM capture loop compares captured bytes against an expected size
@@ -164,18 +178,20 @@ class SpotifyDownloader(Downloader):
164
178
  Prefer the duration already on the release (from search). Otherwise
165
179
  look it up via the Web API, which is reliable and cheap. Only fall
166
180
  back to the 300s default if both fail.
181
+
182
+ Returns ``(seconds, known)``; ``known`` is False for the default guess.
167
183
  """
168
184
  if release.duration_seconds:
169
- return release.duration_seconds
185
+ return release.duration_seconds, True
170
186
  try:
171
187
  duration_ms = sp.track(track_id).get("duration_ms")
172
188
  if duration_ms:
173
189
  secs = duration_ms // 1000
174
190
  logger.debug(f"Resolved duration from Spotify API: {secs}s")
175
- return secs
191
+ return secs, True
176
192
  except Exception as e:
177
193
  logger.warning(f"Failed to look up track duration, using default: {e}")
178
- return 300
194
+ return DEFAULT_DURATION_SECS, False
179
195
 
180
196
  def download(
181
197
  self,
@@ -246,7 +262,7 @@ class SpotifyDownloader(Downloader):
246
262
  except Exception as e:
247
263
  raise SpotifyDownloadError(f"Authentication failed: {e}") from e
248
264
 
249
- duration_secs = self._resolve_duration_secs(sp, track_id, release)
265
+ duration_secs, duration_known = self._resolve_duration(sp, track_id, release)
250
266
 
251
267
  # Unique device name per capture. A single Spotify account can register
252
268
  # several Connect devices at once, so if a previous librespot has not
@@ -267,6 +283,12 @@ class SpotifyDownloader(Downloader):
267
283
  "--backend", "pipe",
268
284
  "--bitrate", "320",
269
285
  "--disable-discovery",
286
+ # Never play past the requested track. librespot decodes faster than
287
+ # real time, so anything autoplay queues after our track would be
288
+ # piped straight into this capture. Since Spotify started sending
289
+ # Web API plays as a "spotify:webapi:<id>" context (2026-11), a
290
+ # librespot that handles that context also autoplays after it.
291
+ "--autoplay", "off",
270
292
  # Volume settings for full-quality recording:
271
293
  "--initial-volume", "100", # Start at max volume
272
294
  "--volume-ctrl", "fixed", # No dynamic volume adjustments
@@ -383,6 +405,19 @@ class SpotifyDownloader(Downloader):
383
405
  captured_s = pcm_size / (SAMPLE_RATE * 4)
384
406
  logger.debug(f"Captured {pcm_size / (1024*1024):.2f} MB PCM")
385
407
 
408
+ # Refuse a capture that ran on past the requested track: the extra
409
+ # audio is a different song. Skipped when the duration is only the
410
+ # default guess.
411
+ if (
412
+ duration_known
413
+ and captured_s > duration_secs + MAX_CAPTURE_OVERRUN_SECS
414
+ ):
415
+ raise SpotifyDownloadError(
416
+ f"Captured {captured_s:.1f}s of audio but {track_uri} is "
417
+ f"{duration_secs}s long; librespot played past the track, "
418
+ "refusing to save audio that includes another song"
419
+ )
420
+
386
421
  # Convert PCM to a per-download temp FLAC, then atomically move it
387
422
  # into place. The atomic rename guarantees the returned path is
388
423
  # always a complete file even if a concurrent download targets the
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flacfetch
3
- Version: 0.37.0
3
+ Version: 0.37.1
4
4
  Summary: Search and download high-quality audio from multiple sources
5
5
  Home-page: https://github.com/yourusername/flacfetch
6
6
  Author: Your Name
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "flacfetch"
7
- version = "0.37.0"
7
+ version = "0.37.1"
8
8
  description = "Search and download high-quality audio from multiple sources"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10,<4.0"
@@ -989,3 +989,86 @@ class TestAudioFormatOutput:
989
989
  vorbis_320 = Quality(format=AudioFormat.VORBIS, bitrate=320)
990
990
 
991
991
  assert vorbis_320 < flac
992
+
993
+
994
+ class TestSpotifyCaptureStopsAtTrackEnd:
995
+ """A capture must contain only the requested track.
996
+
997
+ 2026-11-19: Spotify began sending Web API plays to Connect devices as a
998
+ ``spotify:webapi:<id>`` context. librespot 0.8.0 fails those plays (the
999
+ Web API reports "403 Restriction violated"); a librespot that handles them
1000
+ also autoplays a similar track afterwards, and because librespot decodes
1001
+ faster than real time, a test capture of a 191 s track came back 378 s long.
1002
+ """
1003
+
1004
+ def _run(self, tmp_path, captured_secs, duration_secs=200, api_duration_ms=None):
1005
+ creds_dir = tmp_path / "librespot"
1006
+ creds_dir.mkdir()
1007
+ (creds_dir / "credentials.json").write_text("{}")
1008
+ provider = MagicMock()
1009
+ provider._get_client.return_value.track.return_value = {"duration_ms": api_duration_ms}
1010
+ downloader = SpotifyDownloader(provider=provider)
1011
+ downloader._librespot_path = "/fake/librespot"
1012
+ release = Release(
1013
+ title="Test",
1014
+ artist="Test",
1015
+ quality=Quality(AudioFormat.FLAC),
1016
+ source_name="Spotify",
1017
+ download_url="spotify:track:4PTG3Z6ehGkBFwjybzWkR8",
1018
+ duration_seconds=duration_secs,
1019
+ )
1020
+
1021
+ def fake_wait_for_download(pcm_path, *_args, **_kwargs):
1022
+ pcm_path.write_bytes(b"\x00" * int(captured_secs * 44100 * 4))
1023
+
1024
+ def _convert(pcm_path, flac_tmp_path):
1025
+ flac_tmp_path.write_bytes(b"")
1026
+ return True
1027
+
1028
+ proc = MagicMock()
1029
+ proc.poll.return_value = None
1030
+ with patch(
1031
+ "flacfetch.downloaders.spotify.get_librespot_credentials_dir",
1032
+ return_value=str(creds_dir),
1033
+ ), patch(
1034
+ "flacfetch.downloaders.spotify.subprocess.Popen", return_value=proc
1035
+ ) as mock_popen, patch.object(
1036
+ downloader, "_wait_for_device", return_value={"id": "dev"}
1037
+ ), patch.object(
1038
+ downloader, "_wait_for_track_load", return_value=True
1039
+ ), patch.object(
1040
+ downloader, "_wait_for_download", side_effect=fake_wait_for_download
1041
+ ), patch.object(downloader, "_stop_librespot"), patch.object(
1042
+ downloader, "_convert_pcm_to_flac", side_effect=_convert
1043
+ ):
1044
+ try:
1045
+ result = downloader.download(release, str(tmp_path / "out"))
1046
+ except SpotifyDownloadError as e:
1047
+ return mock_popen.call_args.args[0], e, None
1048
+ return mock_popen.call_args.args[0], None, result
1049
+
1050
+ def test_librespot_runs_with_autoplay_off(self, tmp_path):
1051
+ cmd, _err, _result = self._run(tmp_path, captured_secs=200)
1052
+ i = cmd.index("--autoplay")
1053
+ assert cmd[i + 1] == "off"
1054
+
1055
+ def test_capture_matching_track_length_succeeds(self, tmp_path):
1056
+ _cmd, err, result = self._run(tmp_path, captured_secs=200.9)
1057
+ assert err is None
1058
+ assert result.captured_s == pytest.approx(200.9, abs=0.01)
1059
+
1060
+ def test_capture_running_past_track_is_refused(self, tmp_path):
1061
+ _cmd, err, result = self._run(tmp_path, captured_secs=378)
1062
+ assert result is None
1063
+ assert "played past the track" in str(err)
1064
+ assert not list((tmp_path / "out").glob("*.flac"))
1065
+
1066
+ def test_overrun_check_skipped_when_duration_is_only_the_default(self, tmp_path):
1067
+ # No duration on the release and none from the API -> 300 s guess.
1068
+ _cmd, err, _result = self._run(tmp_path, captured_secs=420, duration_secs=None)
1069
+ assert err is None
1070
+
1071
+ def test_overrun_check_applies_to_a_real_300s_track(self, tmp_path):
1072
+ _cmd, err, result = self._run(tmp_path, captured_secs=420, duration_secs=300)
1073
+ assert result is None
1074
+ assert "played past the track" in str(err)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes