Files
lintunes/lintunes/cassette/share.py
T
trav 662217a7b7 v0.28.0: Cassette share selector — choose what friends can cassette
- Sync Settings gets a share selector on each friend page (and on General
  when "Share the same selection with all friends" is on; each friend's
  own selection is kept, greyed, and comes back when it's turned off).
- Library: a genre/artist/album column browser with a checkbox beside
  every artist, album and track, search, Select All / Select None and
  "Share whole library". Playlists: a checklist with "Share all
  playlists". Nothing is shared by default.
- Checking an artist or album is a snapshot of its tracks; the whole
  library and shared playlists are live. A shared playlist's tracks are
  checked and locked, with a tooltip naming the playlist(s).
- cassette/share.py writes the trimmed copy (library.json, playlists.json)
  into each friend's folder: tags yes, listening history and local paths
  no. Written on OK and after library edits (debounced), and only when the
  bytes would change.
2026-09-28 21:18:14 -07:00

114 lines
4.3 KiB
Python

"""What a friend sees: the trimmed library copy in my outgoing folder.
Pure and Qt-free. A ``Selection`` (state.py) says what I offer:
* ``all_library`` — everything, live: a track added next week is offered too.
* ``track_ids`` — tracks checked in the browser. Checking an artist or album
stores the ids it covered *then*, so a new album shows up unchecked.
* ``all_playlists`` / ``playlist_ids`` — shared playlists, live: a song added
to a shared playlist is offered with it. Their tracks are **locked** in the
selector (``locks``), because unchecking them there would break the
playlist on the friend's side.
Offering only makes a track *available*; nothing is sent until the friend
asks for it.
The copy keeps what describes a song (the tags) and drops what describes *my
listening* — play and skip counts, ratings, loved, last played, dates — and
anything that only means something on this machine (location, comments,
start/stop times, EQ).
"""
from __future__ import annotations
from pathlib import Path
from lintunes.models import PlaylistType
from lintunes.cassette import publish
LIBRARY_FILE = "library.json"
PLAYLISTS_FILE = "playlists.json"
# Track fields a friend receives. Everything else stays home.
SHARED_FIELDS = (
"name", "artist", "album_artist", "album", "genre", "composer",
"grouping", "kind", "size", "total_time", "disc_number", "disc_count",
"track_number", "track_count", "year", "bpm", "bit_rate", "sample_rate",
"compilation", "sort_name", "sort_artist", "sort_album_artist",
"sort_album", "sort_composer", "work", "movement_name",
"movement_number", "movement_count",
)
def shareable_playlists(library) -> list:
"""Playlists that can be shared at all: regular and smart. Folders are
structure, and system playlists are the library itself."""
return [p for p in library.playlists.values()
if p.playlist_type in (PlaylistType.REGULAR, PlaylistType.SMART)
and not p.is_system]
def shared_playlists(library, selection) -> list:
playlists = shareable_playlists(library)
if not selection.all_playlists:
playlists = [p for p in playlists
if p.persistent_id in selection.playlist_ids]
return sorted(playlists, key=lambda p: (p.name.casefold(), p.persistent_id))
def locks(library, selection) -> dict[int, list[str]]:
"""{track id: names of the shared playlists that require it}."""
required: dict[int, list[str]] = {}
for playlist in shared_playlists(library, selection):
for tid in playlist.track_ids:
if tid in library.tracks:
names = required.setdefault(tid, [])
if playlist.name not in names:
names.append(playlist.name)
return required
def offered_ids(library, selection) -> set[int]:
if selection.all_library:
return set(library.tracks)
offered = {tid for tid in selection.track_ids if tid in library.tracks}
offered.update(locks(library, selection))
return offered
def track_entry(track) -> dict:
entry = {"id": track.track_id}
for name in SHARED_FIELDS:
value = getattr(track, name)
if value not in ("", 0, None, False):
entry[name] = value
return entry
def build_library(library, selection) -> dict:
offered = offered_ids(library, selection)
tracks = [track_entry(library.tracks[tid]) for tid in sorted(offered)]
return {"format": publish.FORMAT_VERSION, "tracks": tracks}
def build_playlists(library, selection) -> dict:
offered = offered_ids(library, selection)
playlists = []
for playlist in shared_playlists(library, selection):
playlists.append({
"id": playlist.persistent_id,
"name": playlist.name,
"smart": playlist.is_smart,
"track_ids": [t for t in playlist.track_ids if t in offered],
})
return {"format": publish.FORMAT_VERSION, "playlists": playlists}
def publish_share(out: Path, library, selection) -> bool:
"""Write the trimmed copy into ``out``. True when either file changed."""
publish.prepare_out_dir(out)
wrote_library = publish.write_json_if_changed(
out / LIBRARY_FILE, build_library(library, selection))
wrote_playlists = publish.write_json_if_changed(
out / PLAYLISTS_FILE, build_playlists(library, selection))
return wrote_library or wrote_playlists