flacfetch 0.37.0__tar.gz → 0.37.2__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.2}/PKG-INFO +1 -1
  2. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/core/matching.py +12 -2
  3. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/downloaders/spotify.py +39 -4
  4. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/providers/gazelle.py +99 -43
  5. {flacfetch-0.37.0 → flacfetch-0.37.2/flacfetch.egg-info}/PKG-INFO +1 -1
  6. {flacfetch-0.37.0 → flacfetch-0.37.2}/pyproject.toml +1 -1
  7. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_gazelle.py +56 -0
  8. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_matching.py +12 -0
  9. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_spotify.py +83 -0
  10. {flacfetch-0.37.0 → flacfetch-0.37.2}/ARCHITECTURE.md +0 -0
  11. {flacfetch-0.37.0 → flacfetch-0.37.2}/LICENSE +0 -0
  12. {flacfetch-0.37.0 → flacfetch-0.37.2}/MANIFEST.in +0 -0
  13. {flacfetch-0.37.0 → flacfetch-0.37.2}/PLAN.md +0 -0
  14. {flacfetch-0.37.0 → flacfetch-0.37.2}/README.md +0 -0
  15. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/__init__.py +0 -0
  16. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/__init__.py +0 -0
  17. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/auth.py +0 -0
  18. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/main.py +0 -0
  19. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/models.py +0 -0
  20. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/routes/__init__.py +0 -0
  21. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/routes/cache.py +0 -0
  22. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/routes/config.py +0 -0
  23. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/routes/credentials.py +0 -0
  24. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/routes/download.py +0 -0
  25. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/routes/health.py +0 -0
  26. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/routes/search.py +0 -0
  27. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/routes/spotify.py +0 -0
  28. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/routes/torrents.py +0 -0
  29. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/services/__init__.py +0 -0
  30. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/services/credential_check.py +0 -0
  31. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/services/disk_manager.py +0 -0
  32. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/services/download_manager.py +0 -0
  33. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/services/health_check.py +0 -0
  34. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/services/search_cache.py +0 -0
  35. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/services/secret_manager_utils.py +0 -0
  36. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/api/services/spotify_budget.py +0 -0
  37. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/core/album_match.py +0 -0
  38. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/core/categorize.py +0 -0
  39. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/core/config.py +0 -0
  40. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/core/interfaces.py +0 -0
  41. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/core/log.py +0 -0
  42. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/core/manager.py +0 -0
  43. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/core/models.py +0 -0
  44. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/credential_keeper/__init__.py +0 -0
  45. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/credential_keeper/__main__.py +0 -0
  46. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/credential_keeper/browser.py +0 -0
  47. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/credential_keeper/google_login.py +0 -0
  48. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/credential_keeper/keeper.py +0 -0
  49. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/credential_keeper/librespot.py +0 -0
  50. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/credential_keeper/probe.py +0 -0
  51. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/credential_keeper/refresh_request.py +0 -0
  52. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/credential_keeper/spotify.py +0 -0
  53. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/credential_keeper/youtube.py +0 -0
  54. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/downloaders/torrent.py +0 -0
  55. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/downloaders/torrent_coordinator.py +0 -0
  56. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/downloaders/youtube.py +0 -0
  57. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/interface/cli.py +0 -0
  58. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/interface/cli_remote.py +0 -0
  59. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/providers/ops.py +0 -0
  60. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/providers/red.py +0 -0
  61. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/providers/spotify.py +0 -0
  62. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch/providers/youtube.py +0 -0
  63. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch.egg-info/SOURCES.txt +0 -0
  64. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch.egg-info/dependency_links.txt +0 -0
  65. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch.egg-info/entry_points.txt +0 -0
  66. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch.egg-info/requires.txt +0 -0
  67. {flacfetch-0.37.0 → flacfetch-0.37.2}/flacfetch.egg-info/top_level.txt +0 -0
  68. {flacfetch-0.37.0 → flacfetch-0.37.2}/requirements.txt +0 -0
  69. {flacfetch-0.37.0 → flacfetch-0.37.2}/setup.cfg +0 -0
  70. {flacfetch-0.37.0 → flacfetch-0.37.2}/setup.py +0 -0
  71. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/_route_paths.py +0 -0
  72. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_album_search.py +0 -0
  73. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_api_auth.py +0 -0
  74. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_api_config.py +0 -0
  75. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_api_deep_health.py +0 -0
  76. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_api_health_ytdlp.py +0 -0
  77. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_api_integration.py +0 -0
  78. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_api_models.py +0 -0
  79. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_api_routes.py +0 -0
  80. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_api_services.py +0 -0
  81. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_cache_api.py +0 -0
  82. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_cli_cookies.py +0 -0
  83. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_cli_remote.py +0 -0
  84. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_cookie_refresh_request.py +0 -0
  85. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_core.py +0 -0
  86. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_credential_keeper.py +0 -0
  87. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_keeper_leaves_youtube.py +0 -0
  88. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_keeper_probe.py +0 -0
  89. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_logging.py +0 -0
  90. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_manager.py +0 -0
  91. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_models.py +0 -0
  92. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_ops.py +0 -0
  93. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_provider_errors.py +0 -0
  94. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_red.py +0 -0
  95. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_search_api.py +0 -0
  96. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_spotify_budget.py +0 -0
  97. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_torrent_coordinator.py +0 -0
  98. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_torrent_downloader.py +0 -0
  99. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_torrent_shared_album.py +0 -0
  100. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_transcode_hf_noise.py +0 -0
  101. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_youtube.py +0 -0
  102. {flacfetch-0.37.0 → flacfetch-0.37.2}/tests/test_youtube_cookies.py +0 -0
  103. {flacfetch-0.37.0 → flacfetch-0.37.2}/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.2
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
@@ -1,6 +1,14 @@
1
1
  import re
2
2
  from difflib import SequenceMatcher
3
3
 
4
+ # Straight, curly and backtick apostrophes. People type "lets" / "dont" without
5
+ # them while release files say "Let's" / "Don’t", so matching ignores them.
6
+ _APOSTROPHES = re.compile(r"['\u2018\u2019`]")
7
+
8
+
9
+ def _strip_apostrophes(text: str) -> str:
10
+ return _APOSTROPHES.sub("", text)
11
+
4
12
 
5
13
  def clean_filename(filename: str) -> str:
6
14
  # Remove file extension
@@ -9,6 +17,8 @@ def clean_filename(filename: str) -> str:
9
17
  # Remove track numbers like "01 - ", "01. ", "1-", "A1 "
10
18
  filename = re.sub(r'^(\d{1,3}[ .-]+)+', '', filename)
11
19
  filename = re.sub(r'^[A-D]\d{1,2}[ .-]+', '', filename)
20
+ # Scene-style names use underscores for spaces ("lets_get_retarded")
21
+ filename = filename.replace('_', ' ')
12
22
  # Remove " - " separators which might remain
13
23
  filename = filename.replace(' - ', ' ')
14
24
  return filename.strip()
@@ -18,8 +28,8 @@ def calculate_match_score(track_title: str, filename: str) -> float:
18
28
  Returns a score 0.0 - 1.0 indicating how well the filename matches the track title.
19
29
  1.0 = Exact match (ignoring case/punctuation)
20
30
  """
21
- cleaned_name = clean_filename(filename).lower()
22
- target = track_title.lower()
31
+ cleaned_name = _strip_apostrophes(clean_filename(filename).lower())
32
+ target = _strip_apostrophes(track_title.lower())
23
33
 
24
34
  # Exact match check
25
35
  if cleaned_name == target:
@@ -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
@@ -34,6 +34,27 @@ ALBUM_SEARCH_MAX_GROUPS = 3
34
34
  # Quotes: ' (apostrophe breaks searches like "I'm With You")
35
35
  SPHINX_SPECIAL_CHARS = frozenset(r'()|\\-@~&<>!"/*$^=?:[],.;\'')
36
36
 
37
+ # Curly apostrophes split words in the tracker index just like straight ones.
38
+ APOSTROPHE_LIKE_CHARS = frozenset('\u2018\u2019`')
39
+
40
+ # Contractions people type without the apostrophe ("lets get it started").
41
+ # The tracker index splits "Let's" into "let" + "s", so the filelist search for
42
+ # "lets" finds nothing; the fallback search spells these out as "let s".
43
+ # Only words that are not also ordinary words (so no "ill", "well", "were").
44
+ APOSTROPHE_LESS_CONTRACTIONS = {
45
+ "aint": "ain t", "arent": "aren t", "cant": "can t", "couldnt": "couldn t",
46
+ "couldve": "could ve", "didnt": "didn t", "doesnt": "doesn t", "dont": "don t",
47
+ "hadnt": "hadn t", "hasnt": "hasn t", "havent": "haven t", "hes": "he s",
48
+ "heres": "here s", "im": "i m", "isnt": "isn t", "its": "it s", "ive": "i ve",
49
+ "lets": "let s", "mustnt": "mustn t", "shes": "she s", "shouldnt": "shouldn t",
50
+ "shouldve": "should ve", "thats": "that s", "theres": "there s", "theyd": "they d",
51
+ "theyll": "they ll", "theyre": "they re", "theyve": "they ve", "wasnt": "wasn t",
52
+ "weve": "we ve", "werent": "weren t", "whats": "what s", "wheres": "where s",
53
+ "whos": "who s", "wont": "won t", "wouldnt": "wouldn t", "wouldve": "would ve",
54
+ "yall": "y all", "youd": "you d", "youll": "you ll", "youre": "you re",
55
+ "youve": "you ve",
56
+ }
57
+
37
58
 
38
59
  class GazelleProvider(Provider):
39
60
  """Base class for Gazelle-based private music trackers.
@@ -161,7 +182,7 @@ class GazelleProvider(Provider):
161
182
  are NOT modified - Sphinx handles these via charset_table and ngram_chars.
162
183
  """
163
184
  sanitized = query
164
- for char in SPHINX_SPECIAL_CHARS:
185
+ for char in SPHINX_SPECIAL_CHARS | APOSTROPHE_LIKE_CHARS:
165
186
  sanitized = sanitized.replace(char, ' ')
166
187
 
167
188
  # Collapse multiple spaces into one
@@ -170,6 +191,19 @@ class GazelleProvider(Provider):
170
191
  logger.debug(f"Sanitized filelist query: '{query}' -> '{sanitized}'")
171
192
  return sanitized
172
193
 
194
+ @staticmethod
195
+ def _expand_contractions(sanitized_title: str) -> Optional[str]:
196
+ """Spell out apostrophe-less contractions the way the index stores them.
197
+
198
+ "lets get retarded" -> "let s get retarded". Returns None when the title
199
+ has none, so the caller knows there is no fallback search to run.
200
+ """
201
+ words = sanitized_title.split()
202
+ expanded = [APOSTROPHE_LESS_CONTRACTIONS.get(w.lower(), w) for w in words]
203
+ if expanded == words:
204
+ return None
205
+ return " ".join(expanded)
206
+
173
207
  def _find_best_target_file(self, file_list_str: str, track_title: str) -> tuple[Optional[str], Optional[int], float]:
174
208
  """Find the best matching file in a torrent's file list.
175
209
 
@@ -1062,57 +1096,79 @@ class GazelleProvider(Provider):
1062
1096
  # Sanitize the filelist query to remove Sphinx special operators
1063
1097
  sanitized_title = self._sanitize_filelist_query(query.title) if query.title else ""
1064
1098
 
1099
+ try:
1100
+ releases = self._search_browse_filelist(query, sanitized_title)
1101
+ # "lets" only finds files that also drop the apostrophe (often a
1102
+ # stray compilation), never the album's "Let's", so always search
1103
+ # the spelled-out form too and merge.
1104
+ expanded = self._expand_contractions(sanitized_title)
1105
+ if expanded:
1106
+ logger.info(f"{self.name}: also searching contraction form '{expanded}'")
1107
+ seen = {r.source_id for r in releases}
1108
+ try:
1109
+ extra = self._search_browse_filelist(query, expanded)
1110
+ except Exception as e:
1111
+ # Never lose the first search's matches to the extra one.
1112
+ logger.warning(f"{self.name}: contraction search failed, keeping first results: {e}")
1113
+ extra = []
1114
+ for r in extra:
1115
+ if r.source_id not in seen:
1116
+ seen.add(r.source_id)
1117
+ releases.append(r)
1118
+ return releases
1119
+ except requests.RequestException as e:
1120
+ logger.error(f"Connection error to {self.name}: {e}")
1121
+ return []
1122
+ except Exception as e:
1123
+ logger.exception(f"Unexpected error in {self.name}Provider: {e}")
1124
+ return []
1125
+
1126
+ def _search_browse_filelist(self, query: TrackQuery, filelist: str) -> list[Release]:
1127
+ """One browse by artist + filelist words, then file lists for the top groups."""
1065
1128
  params = {
1066
1129
  "action": "browse",
1067
1130
  "artistname": query.artist,
1068
- "filelist": sanitized_title,
1131
+ "filelist": filelist,
1069
1132
  "format": "FLAC"
1070
1133
  }
1071
1134
 
1072
1135
  logger.debug(f"Searching {self.name} with params: {params}")
1073
1136
 
1074
- try:
1075
- ordered_group_ids, token_eligibility = self._browse(params)
1076
- logger.debug(f"Found {len(ordered_group_ids)} groups in {self.name} response")
1137
+ ordered_group_ids, token_eligibility = self._browse(params)
1138
+ logger.debug(f"Found {len(ordered_group_ids)} groups in {self.name} response")
1077
1139
 
1078
- limited_group_ids = ordered_group_ids[:self.search_limit]
1140
+ limited_group_ids = ordered_group_ids[:self.search_limit]
1079
1141
 
1080
- if len(ordered_group_ids) > self.search_limit:
1081
- logger.info(f"Limiting detailed fetch to top {self.search_limit} groups (out of {len(ordered_group_ids)} found)")
1142
+ if len(ordered_group_ids) > self.search_limit:
1143
+ logger.info(f"Limiting detailed fetch to top {self.search_limit} groups (out of {len(ordered_group_ids)} found)")
1082
1144
 
1083
- logger.info(f"Fetching details for up to {len(limited_group_ids)} groups to resolve file lists...")
1145
+ logger.info(f"Fetching details for up to {len(limited_group_ids)} groups to resolve file lists...")
1084
1146
 
1085
- releases = []
1086
- excellent_found = False
1087
- groups_fetched = 0
1088
-
1089
- for gid in limited_group_ids:
1090
- # Rate limit: sleep before each request
1091
- time.sleep(1.1)
1092
-
1093
- group_releases = self._fetch_group_details(gid, query.title, token_eligibility)
1094
- releases.extend(group_releases)
1095
- groups_fetched += 1
1096
-
1097
- # Check for early termination
1098
- if self.early_termination and not excellent_found:
1099
- for r in group_releases:
1100
- if (r.seeders and r.seeders >= self.early_termination_seeders and
1101
- r.release_type in self.early_termination_release_types):
1102
- excellent_found = True
1103
- logger.info(f"Found excellent result: {r.artist} - {r.title} ({r.seeders} seeders, {r.release_type})")
1104
- break
1105
-
1106
- # If we found an excellent result, fetch a few more groups then stop
1107
- if excellent_found and groups_fetched >= min(5, len(limited_group_ids)):
1108
- logger.info(f"Early termination: stopping after {groups_fetched} groups (found excellent result)")
1109
- break
1110
-
1111
- logger.info(f"Total matching tracks parsed from {self.name}: {len(releases)}")
1112
- return releases
1113
- except requests.RequestException as e:
1114
- logger.error(f"Connection error to {self.name}: {e}")
1115
- return []
1116
- except Exception as e:
1117
- logger.exception(f"Unexpected error in {self.name}Provider: {e}")
1118
- return []
1147
+ releases = []
1148
+ excellent_found = False
1149
+ groups_fetched = 0
1150
+
1151
+ for gid in limited_group_ids:
1152
+ # Rate limit: sleep before each request
1153
+ time.sleep(1.1)
1154
+
1155
+ group_releases = self._fetch_group_details(gid, query.title, token_eligibility)
1156
+ releases.extend(group_releases)
1157
+ groups_fetched += 1
1158
+
1159
+ # Check for early termination
1160
+ if self.early_termination and not excellent_found:
1161
+ for r in group_releases:
1162
+ if (r.seeders and r.seeders >= self.early_termination_seeders and
1163
+ r.release_type in self.early_termination_release_types):
1164
+ excellent_found = True
1165
+ logger.info(f"Found excellent result: {r.artist} - {r.title} ({r.seeders} seeders, {r.release_type})")
1166
+ break
1167
+
1168
+ # If we found an excellent result, fetch a few more groups then stop
1169
+ if excellent_found and groups_fetched >= min(5, len(limited_group_ids)):
1170
+ logger.info(f"Early termination: stopping after {groups_fetched} groups (found excellent result)")
1171
+ break
1172
+
1173
+ logger.info(f"Total matching tracks parsed from {self.name}: {len(releases)}")
1174
+ return releases
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flacfetch
3
- Version: 0.37.0
3
+ Version: 0.37.2
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.2"
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"
@@ -1080,3 +1080,59 @@ class TestDownloadBudget:
1080
1080
  p.fetch_artifact_by_id("42")
1081
1081
  ledger = p._load_download_ledger("42")
1082
1082
  assert [r["kind"] for r in ledger["requests"]] == ["token"]
1083
+
1084
+
1085
+ class TestContractionFallback:
1086
+ """Apostrophe-less contractions ("lets") miss the tracker index, which stores "let s"."""
1087
+
1088
+ @pytest.fixture
1089
+ def provider(self):
1090
+ return ConcreteGazelleProvider(api_key="test", base_url="https://test.example", cache_subdir="test")
1091
+
1092
+ @pytest.mark.parametrize("title,expected", [
1093
+ ("lets get retarded", "let s get retarded"),
1094
+ ("Dont Stop Believin", "don t Stop Believin"),
1095
+ ("Im Yours", "i m Yours"),
1096
+ ("Hello", None),
1097
+ ("well ill be there", None),
1098
+ ])
1099
+ def test_expand_contractions(self, provider, title, expected):
1100
+ assert provider._expand_contractions(title) == expected
1101
+
1102
+ def test_curly_apostrophe_sanitized(self, provider):
1103
+ assert provider._sanitize_filelist_query("Let’s Get It Started") == "Let s Get It Started"
1104
+
1105
+ def test_also_searches_expanded_title_and_merges(self, provider, monkeypatch):
1106
+ from flacfetch.core.models import TrackQuery
1107
+ comp, album, dup = MagicMock(source_id="1"), MagicMock(source_id="2"), MagicMock(source_id="1")
1108
+ calls = []
1109
+
1110
+ def fake(query, filelist):
1111
+ calls.append(filelist)
1112
+ return [comp] if filelist == "lets get retarded" else [album, dup]
1113
+
1114
+ monkeypatch.setattr(provider, "_search_browse_filelist", fake)
1115
+ result = provider._search_browse(TrackQuery(artist="black eyed peas", title="lets get retarded"))
1116
+ assert calls == ["lets get retarded", "let s get retarded"]
1117
+ assert result == [comp, album]
1118
+
1119
+ def test_expanded_search_failure_keeps_first_results(self, provider, monkeypatch):
1120
+ import requests
1121
+
1122
+ from flacfetch.core.models import TrackQuery
1123
+ comp = MagicMock(source_id="1")
1124
+
1125
+ def fake(query, filelist):
1126
+ if filelist == "let s get retarded":
1127
+ raise requests.Timeout("slow tracker")
1128
+ return [comp]
1129
+
1130
+ monkeypatch.setattr(provider, "_search_browse_filelist", fake)
1131
+ assert provider._search_browse(TrackQuery(artist="x", title="lets get retarded")) == [comp]
1132
+
1133
+ def test_no_retry_without_contractions(self, provider, monkeypatch):
1134
+ from flacfetch.core.models import TrackQuery
1135
+ calls = []
1136
+ monkeypatch.setattr(provider, "_search_browse_filelist", lambda q, f: calls.append(f) or [])
1137
+ assert provider._search_browse(TrackQuery(artist="x", title="Hello")) == []
1138
+ assert calls == ["Hello"]
@@ -27,3 +27,15 @@ def test_no_match():
27
27
  score = calculate_match_score("Tonight", "Tomorrow.flac")
28
28
  assert score < 0.5
29
29
 
30
+
31
+
32
+ def test_apostrophe_less_title_matches_contraction():
33
+ # Typed "lets", the file says "Let's" (straight or curly)
34
+ assert calculate_match_score("lets get retarded", "05 - Let's Get Retarded.flac") == 1.0
35
+ assert calculate_match_score("lets get retarded", "05 - Let’s Get Retarded.flac") == 1.0
36
+ assert calculate_match_score("Let's Get Retarded", "05 - Lets Get Retarded.flac") == 1.0
37
+
38
+
39
+ def test_underscored_scene_filename_matches():
40
+ score = calculate_match_score("lets get retarded", "306-black_eyed_peas-lets_get_retarded.flac")
41
+ assert score > 0.6
@@ -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