v0.35.0: a play history for the yearly Wrapped

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.
This commit is contained in:
2026-10-02 16:47:40 -07:00
parent 10ced778af
commit d6140f5ac4
26 changed files with 1148 additions and 15 deletions
+15
View File
@@ -14,6 +14,7 @@ from lintunes.models import Library, Playlist, PlaylistType
from lintunes.paths import to_relative
from lintunes.perf import timed
from lintunes.storage import json_storage
from lintunes.storage.listen_history import ListenHistory
from lintunes.storage.play_journal import PlayJournal
from lintunes.undo import Command, UndoStack
@@ -75,6 +76,9 @@ class LibraryManager(QObject):
self.play_journal = PlayJournal()
self.play_journal.load(self.data_dir, self.library)
self._dirty_journal = False
# One line per listen, for the yearly Wrapped — only fed while the
# user has it switched on (MainWindow checks the preference).
self.listen_history = ListenHistory(self.play_journal.machine)
self._save_timer = QTimer(self)
self._save_timer.setSingleShot(True)
@@ -850,6 +854,12 @@ class LibraryManager(QObject):
self.track_updated.emit(track_id)
self._touch_smart({"play_count", "play_date_utc"})
def record_listen(self, entry: dict | None):
"""Queue one listen for the history; written with the next save."""
if entry:
self.listen_history.add(entry)
self._schedule_save()
def record_skip(self, track_id: int):
track = self.library.tracks.get(track_id)
if not track:
@@ -975,6 +985,11 @@ class LibraryManager(QObject):
self._dirty_journal = False
self._record_sig(self.data_dir / "plays"
/ f"{self.play_journal.machine}.json")
if self.listen_history.dirty:
try:
self.listen_history.flush(self.data_dir)
except OSError:
log.exception("couldn't write the listen history")
if self._dirty_tracks:
with timed("flush library.json (%d tracks)",
len(self.library.tracks)):