Coverage for downtify/playlist_listing.py: 97%
34 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"""The downloaded playlists, as ``GET /playlists`` lists them (and, by
2track id, ``GET /api/v1/playlists``)."""
4from __future__ import annotations
6from collections.abc import Sequence
7from pathlib import Path
8from typing import Any, Optional
10from . import m3u
11from .library_paths import library_stored_path
12from .library_paths_cache import get_cached_playlists, peek_cached_path_pairs
13from .likes import is_liked_playlist
14from .playlist_mosaic import has_explicit_cover
17def list_library_playlists(
18 download_dir: Path,
19 slskd_dir: Optional[Path] = None,
20 extra_dirs: Optional[Sequence[Path]] = None,
21 *,
22 stale_ok: bool = False,
23) -> list[dict[str, Any]]:
24 """Every playlist from ``.m3u`` / UTF-8 ``.m3u8`` files under downloads,
25 including playlist/album downloads and Playlist Monitor sweeps (see
26 ``downtify/m3u.py``), plus playlists the user created in the Library.
28 Each M3U file on disk is one playlist, regardless of whether
29 *Organize by artist/album* put its tracks in a per-playlist folder or
30 scattered them into artist/album folders — the M3U is the one place
31 that still records "these tracks belong together, in this order"
32 either way. Single tracks and albums downloaded without an M3U
33 aren't playlists and don't show up here. The liked songs playlist
34 comes first, then the rest by name.
35 """
37 extras = tuple(extra_dirs or ())
38 return get_cached_playlists(
39 Path(download_dir),
40 slskd_dir,
41 extras,
42 lambda: _scan_library_playlists(Path(download_dir), slskd_dir, extras),
43 stale_ok=stale_ok,
44 )
47def _known_library_files(
48 download_dir: Path,
49 slskd_dir: Optional[Path],
50 extra_dirs: tuple[Path, ...],
51) -> Optional[set[str]]:
52 from .library_catalog import LibraryContext # noqa: PLC0415
54 ctx = LibraryContext(
55 download_dir=download_dir,
56 slskd_dir=slskd_dir,
57 extra_dirs=extra_dirs,
58 )
59 pairs = peek_cached_path_pairs(ctx)
60 if pairs is None:
61 return None
62 return {stored for stored, _resolved in pairs}
65def _scan_library_playlists(
66 download_dir: Path,
67 slskd_dir: Optional[Path],
68 extra_dirs: tuple[Path, ...],
69) -> list[dict[str, Any]]:
70 base = Path(download_dir).resolve()
71 if not base.exists():
72 return []
73 known = _known_library_files(download_dir, slskd_dir, extra_dirs)
74 playlists: list[dict[str, Any]] = []
75 for m3u_path in m3u.iter_m3u_files(base):
76 manual = m3u.is_manual_m3u(m3u_path)
77 tracks = [
78 stored
79 for stored in m3u.read_m3u_tracks(
80 m3u_path,
81 base,
82 slskd_dir,
83 extra_dirs,
84 keep_missing=True,
85 )
86 if known is None or stored in known
87 ]
88 if not tracks and not manual:
89 continue
90 # The sidecar artwork save_playlist_cover writes beside the M3U,
91 # when the playlist has one of its own.
92 cover = m3u_path.with_suffix('.jpg')
93 playlists.append({
94 'name': m3u_path.stem,
95 'files': tracks,
96 'count': len(tracks),
97 'added': int(m3u_path.stat().st_mtime),
98 'cover': (
99 library_stored_path(cover, base, slskd_dir, extra_dirs)
100 if cover.is_file()
101 else ''
102 ),
103 # The playlist of hearted songs, not a downloaded one.
104 'liked': is_liked_playlist(m3u_path.stem),
105 'manual': manual,
106 'explicit_cover': manual and has_explicit_cover(m3u_path),
107 })
108 playlists.sort(key=lambda p: (not p['liked'], p['name'].casefold()))
109 return playlists