The play journals keep running totals per track, which is enough for "most played ever" and nothing a Wrapped asks: top songs of this year, minutes listened, which month, what time of day, what got skipped. Those all come from one timestamped line per listen, so LinTunes now keeps that, while Preferences -> General -> "Keep a play history for my yearly Wrapped" is on. history/<machine>/<YYYY-MM>.jsonl, one writer per folder like the journals: local start time with its offset, ms actually heard (Player adds position steps under 1.5 s, so seeks and pauses don't count), how it ended, where it played and from what, and a name/artist/album snapshot that outlives a delete. Under 5 s is noise and never written. andTunes 0.3.0 writes the same lines on the Rabbit when sync says the desktop wants them, and sync copies each month home when the device's copy is longer. "Share this year's play history with friends" puts a trimmed copy (no ids, playlists or devices) in each friend's cassette folder, daily in November and December and monthly otherwise.
81 lines
3.1 KiB
Python
81 lines
3.1 KiB
Python
"""Build ``library.json`` — the whole point of andTunes.
|
|
|
|
Every other Android player rebuilds its library by scanning the device:
|
|
Auxio reads Android's MediaStore on each launch, which is what leaves the R1
|
|
sitting on "your songs will show up here". But LinTunes *already knows* the
|
|
answer — it has the artist, album, year and track number for every file it
|
|
just copied. So it writes them down, and the app opens by parsing one file
|
|
instead of indexing a filesystem.
|
|
|
|
Pure functions only: no Qt, no device, no I/O. The shape is deliberately flat
|
|
— one track list, playlists as id references. Artists and albums are grouped
|
|
on the device in a single pass at load, which is cheaper than shipping three
|
|
redundant lists and parsing them.
|
|
"""
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
from .layout import FORMAT_VERSION
|
|
|
|
|
|
def _utc_now() -> str:
|
|
return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
|
|
|
|
|
def track_entry(track, media_rel: str, art_rel: str | None = None) -> dict:
|
|
"""One track's row. Empty and zero fields are omitted.
|
|
|
|
Sparse rows the way `Track.to_dict` is sparse: a library of a few thousand
|
|
tracks is the file the app parses before it can draw anything, so every
|
|
key that says nothing is latency on the R1's cold start.
|
|
"""
|
|
entry = {"id": track.track_id, "title": track.name, "path": media_rel}
|
|
optional = {
|
|
"artist": track.artist,
|
|
"album_artist": track.album_artist,
|
|
"album": track.album,
|
|
"year": track.year,
|
|
"track": track.track_number,
|
|
"disc": track.disc_number,
|
|
"secs": round(track.total_time / 1000) if track.total_time else 0,
|
|
}
|
|
for key, value in optional.items():
|
|
if value:
|
|
entry[key] = value
|
|
if art_rel:
|
|
entry["art"] = art_rel
|
|
return entry
|
|
|
|
|
|
def build_manifest(entries, playlists, *, generator: str,
|
|
generated: str | None = None, history: bool = False) -> dict:
|
|
"""The complete index.
|
|
|
|
`entries` is a list of ``(track, media_rel, art_rel_or_None)`` in no
|
|
particular order; `playlists` is ``(name, persistent_id, [track_id, …])``
|
|
with the ids in playing order. A playlist's ids are filtered to tracks
|
|
that actually made it onto the device, so the app never resolves a
|
|
dangling reference — the m3u is filtered the same way by the worker.
|
|
|
|
``history`` tells the app to keep a listen history (the desktop's
|
|
"Keep a play history for my yearly Wrapped"); absent means off, which is
|
|
what an app reading an older manifest assumes too.
|
|
"""
|
|
rows = [track_entry(track, rel, art) for track, rel, art in entries]
|
|
known = {row["id"] for row in rows}
|
|
rows.sort(key=lambda r: r["id"])
|
|
manifest = {
|
|
"format": FORMAT_VERSION,
|
|
"generated": generated or _utc_now(),
|
|
"generator": generator,
|
|
"tracks": rows,
|
|
"playlists": [
|
|
{"id": pid, "name": name,
|
|
"tracks": [tid for tid in track_ids if tid in known]}
|
|
for name, pid, track_ids in playlists
|
|
],
|
|
}
|
|
if history:
|
|
manifest["history"] = True
|
|
return manifest
|