Coverage for downtify/playlist_diagnostics.py: 90%
164 statements
« prev ^ index » next coverage.py v7.17.4, created at 2026-11-21 22:18 +0000
« prev ^ index » next coverage.py v7.17.4, created at 2026-11-21 22:18 +0000
1"""Compare a Spotify playlist with Downtify and Navidrome records."""
3from __future__ import annotations
5from dataclasses import dataclass
6from pathlib import Path
7from typing import Any, Optional
9from loguru import logger
11from .library_paths import locate_library_file
12from .monitor import KIND_PLAYLIST, SOURCE_SPOTIFY
13from .navidrome import _effective_navidrome_settings
14from .track_index import normalize_spotify_track_id
17@dataclass(slots=True)
18class PlaylistDiagnosticsContext:
19 catalog: Any
20 spotify_cache: Any
21 track_index: Any
22 navidrome_index: Any
23 monitor_db: Any
24 hidden: Any
25 settings: dict[str, Any]
26 download_dir: Path
27 slskd_dir: Optional[Path]
28 is_admin: bool
29 user_id: int
30 navidrome_client: Any
33@dataclass(slots=True)
34class _PlaylistSnapshot:
35 tracks: dict[str, dict[str, str]]
36 catalog_tracks: list[dict[str, Any]]
37 orphan_files: list[dict[str, str]]
38 catalog_files: dict[str, str]
39 index_files: dict[str, str]
40 physical_files: dict[str, str]
41 allowed_files: set[str]
42 monitor_ids: Optional[set[str]]
43 nav_matched: Optional[set[str]]
44 nav_count: Optional[int]
45 nav_issues: list[dict[str, str]]
48def build_playlist_diagnostics(
49 name: str,
50 files: list[str],
51 context: PlaylistDiagnosticsContext,
52) -> dict[str, Any]:
53 snapshot = _playlist_snapshot(name, files, context)
54 if snapshot is None:
55 return {'available': False, 'reason': 'spotify_tracks_unavailable'}
56 return {'available': True, 'items': _diagnostic_items(snapshot)}
59def _playlist_snapshot(
60 name: str,
61 files: list[str],
62 context: PlaylistDiagnosticsContext,
63) -> Optional[_PlaylistSnapshot]:
64 catalog = context.catalog
65 spotify_id = catalog.spotify_id_for_playlist(name) if catalog else None
66 cached = (
67 context.spotify_cache.get(spotify_id)
68 if context.spotify_cache is not None and spotify_id
69 else None
70 )
71 if not cached:
72 return None
74 tracks = _source_tracks(cached[1])
75 catalog_tracks = catalog.list_tracks(name) if catalog else []
76 _filter_hidden_tracks(name, tracks, catalog_tracks, context)
77 if not tracks:
78 return None
80 allowed_files = {
81 _normalize_path(path) for path in files if _normalize_path(path)
82 }
83 catalog_files = _catalog_files(name, spotify_id, catalog, allowed_files)
84 index_files, source_files, physical_files = _library_files(
85 tracks, catalog_files, allowed_files, context
86 )
87 nav_count, nav_issues = _navidrome_rows(
88 name, tracks, source_files, context
89 )
90 orphan_files = [
91 {
92 'track_id': str(row.get('track_spotify_id') or ''),
93 'label': str(row.get('filename') or ''),
94 }
95 for row in catalog_tracks
96 if str(row.get('track_spotify_id') or '') not in tracks
97 and _normalize_path(row.get('filename')) in allowed_files
98 and locate_library_file(
99 _normalize_path(row.get('filename')),
100 context.download_dir,
101 context.slskd_dir,
102 )
103 is not None
104 ]
105 return _PlaylistSnapshot(
106 tracks=tracks,
107 catalog_tracks=catalog_tracks,
108 orphan_files=orphan_files,
109 catalog_files=catalog_files,
110 index_files=index_files,
111 physical_files=physical_files,
112 allowed_files=allowed_files,
113 monitor_ids=_monitor_track_ids(spotify_id, context),
114 nav_matched=_navidrome_matched_ids(tracks, source_files, context),
115 nav_count=nav_count,
116 nav_issues=nav_issues,
117 )
120def _library_files(
121 tracks: dict[str, dict[str, str]],
122 catalog_files: dict[str, str],
123 allowed_files: set[str],
124 context: PlaylistDiagnosticsContext,
125) -> tuple[dict[str, str], dict[str, Optional[str]], dict[str, str]]:
126 index_files = (
127 context.track_index.filenames_by_spotify_id()
128 if context.track_index is not None
129 else {}
130 )
131 source_files = {
132 track_id: catalog_files.get(track_id) or index_files.get(track_id)
133 for track_id in tracks
134 }
135 physical_files = {
136 track_id: filename
137 for track_id, filename in source_files.items()
138 if filename in allowed_files
139 and locate_library_file(
140 filename, context.download_dir, context.slskd_dir
141 )
142 is not None
143 }
144 return index_files, source_files, physical_files
147def _diagnostic_items(snapshot: _PlaylistSnapshot) -> list[dict[str, Any]]:
148 tracks = snapshot.tracks
149 catalog_files = snapshot.catalog_files
150 index_files = snapshot.index_files
151 physical_files = snapshot.physical_files
152 monitor_ids = snapshot.monitor_ids
153 nav_matched = snapshot.nav_matched
154 missing_downloads = set(tracks) - set(catalog_files)
155 missing_files = set(tracks) - set(physical_files)
156 missing_index = set(tracks) - set(index_files)
157 orphans = snapshot.orphan_files
158 return [
159 {'key': 'spotify_tracks', 'count': len(tracks), 'issues': []},
160 {
161 'key': 'downloaded',
162 'count': len(tracks) - len(missing_downloads),
163 'issues': _affected(tracks, missing_downloads),
164 },
165 {
166 'key': 'physical_files',
167 'count': len(physical_files),
168 'issues': _affected(tracks, missing_files),
169 },
170 {
171 'key': 'track_index',
172 'count': len(set(tracks) - missing_index),
173 'issues': _affected(tracks, missing_index),
174 },
175 {
176 'key': 'monitor_history',
177 'count': len(set(tracks) & monitor_ids)
178 if monitor_ids is not None
179 else None,
180 'issues': _affected(tracks, set(tracks) - monitor_ids)
181 if monitor_ids is not None
182 else [],
183 },
184 {
185 'key': 'navidrome_matched',
186 'count': len(nav_matched) if nav_matched is not None else None,
187 'issues': _affected(tracks, set(tracks) - nav_matched)
188 if nav_matched is not None
189 else [],
190 },
191 {
192 'key': 'navidrome_rows',
193 'count': snapshot.nav_count,
194 'issues': snapshot.nav_issues,
195 },
196 {
197 'key': 'missing_files',
198 'count': len(missing_files),
199 'issues': _affected(tracks, missing_files),
200 },
201 {'key': 'orphan_files', 'count': len(orphans), 'issues': orphans},
202 ]
205def _normalize_path(value: Any) -> str:
206 return str(value or '').strip().replace('\\', '/')
209def _source_tracks(rows: list[dict[str, Any]]) -> dict[str, dict[str, str]]:
210 tracks: dict[str, dict[str, str]] = {}
211 for song in rows:
212 track_id = normalize_spotify_track_id(song)
213 if not track_id or track_id in tracks:
214 continue
215 artists = song.get('artists') or (
216 [song['artist']] if song.get('artist') else []
217 )
218 label = ' - '.join(
219 part
220 for part in (
221 ', '.join(str(artist) for artist in artists),
222 str(song.get('name') or ''),
223 )
224 if part
225 )
226 tracks[track_id] = {'track_id': track_id, 'label': label or track_id}
227 return tracks
230def _filter_hidden_tracks(
231 name: str,
232 tracks: dict[str, dict[str, str]],
233 catalog_tracks: list[dict[str, Any]],
234 context: PlaylistDiagnosticsContext,
235) -> None:
236 if context.is_admin or context.hidden is None:
237 return
238 hidden = context.hidden.songs(name)
239 hidden_ids = set(hidden.track_ids)
240 for row in catalog_tracks:
241 if hidden.hides_file(
242 row.get('filename'), str(row.get('content_key') or '')
243 ):
244 hidden_ids.add(str(row.get('track_spotify_id') or ''))
245 for track_id in hidden_ids:
246 tracks.pop(track_id, None)
249def _catalog_files(
250 name: str,
251 spotify_id: Optional[str],
252 catalog: Any,
253 allowed_files: set[str],
254) -> dict[str, str]:
255 if catalog is None:
256 return {}
257 names = {name}
258 if spotify_id:
259 names.update(
260 row['name']
261 for row in catalog.list_playlists_with_spotify_id()
262 if row['spotify_id'] == spotify_id
263 )
264 result: dict[str, str] = {}
265 for playlist_name in names:
266 for row in catalog.list_tracks(playlist_name):
267 track_id = str(row.get('track_spotify_id') or '').strip()
268 filename = _normalize_path(row.get('filename'))
269 if track_id and filename in allowed_files:
270 result.setdefault(track_id, filename)
271 return result
274def _affected(
275 tracks: dict[str, dict[str, str]], track_ids: set[str]
276) -> list[dict[str, str]]:
277 return [tracks[track_id] for track_id in tracks if track_id in track_ids]
280def _monitor_track_ids(
281 spotify_id: Optional[str], context: PlaylistDiagnosticsContext
282) -> Optional[set[str]]:
283 db = context.monitor_db
284 if db is None:
285 return None
286 watch = db.get_by_spotify_id(
287 str(spotify_id), None if context.is_admin else context.user_id
288 )
289 if (
290 watch is None
291 or watch.kind != KIND_PLAYLIST
292 or watch.source != SOURCE_SPOTIFY
293 ):
294 return None
295 return set(db.get_track_filenames(watch.id))
298def _navidrome_matched_ids(
299 tracks: dict[str, dict[str, str]],
300 source_files: dict[str, Optional[str]],
301 context: PlaylistDiagnosticsContext,
302) -> Optional[set[str]]:
303 cfg = _effective_navidrome_settings(context.settings)
304 index = context.navidrome_index
305 if (
306 not context.settings.get('sync_navidrome', True)
307 or not cfg.get('enabled')
308 or index is None
309 ):
310 return None
311 return {
312 track_id
313 for track_id in tracks
314 if index.lookup_spotify(track_id)
315 or (
316 source_files.get(track_id)
317 and index.lookup_filename(source_files[track_id])
318 )
319 }
322def _navidrome_rows(
323 name: str,
324 tracks: dict[str, dict[str, str]],
325 source_files: dict[str, Optional[str]],
326 context: PlaylistDiagnosticsContext,
327) -> tuple[Optional[int], list[dict[str, str]]]:
328 cfg = _effective_navidrome_settings(context.settings)
329 if not context.settings.get('sync_navidrome', True) or not cfg.get(
330 'enabled'
331 ):
332 return None, []
333 try:
334 client = context.navidrome_client(cfg)
335 if not client.configured():
336 return None, []
337 playlist_ids = client.find_playlist_ids_by_name(name)
338 songs = (
339 client.get_playlist_songs(playlist_ids[0]) if playlist_ids else []
340 )
341 except Exception as exc:
342 logger.info(
343 'playlist diagnostics: Navidrome lookup failed name={!r} err={}',
344 name,
345 exc,
346 )
347 return None, []
349 index = context.navidrome_index
350 if index is None:
351 return len(songs), []
352 actual_ids = {str(song.get('id') or '') for song in songs}
353 expected_ids: set[str] = set()
354 missing_ids: set[str] = set()
355 for track_id in tracks:
356 song_id = index.lookup_spotify(track_id)
357 filename = source_files.get(track_id)
358 if not song_id and filename:
359 song_id = index.lookup_filename(filename)
360 if song_id:
361 expected_ids.add(song_id)
362 if not song_id or song_id not in actual_ids:
363 missing_ids.add(track_id)
364 issues = _affected(tracks, missing_ids)
365 for position, song in enumerate(songs):
366 song_id = str(song.get('id') or '')
367 if not song_id or song_id in expected_ids:
368 continue
369 title = str(song.get('title') or song.get('name') or '')
370 artist = str(song.get('artist') or '')
371 issues.append({
372 'track_id': song_id,
373 'label': f'{artist} - {title}'
374 if artist and title
375 else title or artist or f'Navidrome row {position + 1}',
376 })
377 return len(songs), issues