Scrobble plays from the UI, and vary suggestions between rebuilds
Scrobbling: the UI reports "now playing" when a song starts, and a listen once half of it (or 4 minutes) has actually been played. Library songs go through Subsonic's scrobble endpoint, which keeps play counts and passes them on to the user's Listenbrainz/Last.fm; YouTube songs go straight to Listenbrainz with listenbrainz.token. discover.scrobble = false turns it off. Also stops the previous YouTube video when moving to the next YouTube song, as it played on while the next was looked up. Fresher suggestions: each mix's suggestions are a random sample of its best 3 x discovery_count candidates, weighted by fit squared, rather than always the top ones. Suggestions left alone for a week expire, and stay out of the mixes for 60 days. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
39cd2b69f1
commit
b75f66c205
18 files changed
+437
-15
No files matched your search
@@ -49,6 +49,18 @@ export type Suggestion = {
|
||||
youtube_id: string | null;
|
||||
};
|
||||
|
||||
export type Listen = {
|
||||
source: "library" | "youtube";
|
||||
/** Subsonic song id, or suggestion id */
|
||||
id: string;
|
||||
submission: boolean;
|
||||
/** Unix seconds */
|
||||
started_at: number;
|
||||
/** Seconds */
|
||||
duration?: number;
|
||||
video_id?: string;
|
||||
};
|
||||
|
||||
export type MixDetail = MixSummary & {
|
||||
songs: Song[];
|
||||
/** Songs in `songs` the user played recently */
|
||||
@@ -99,6 +111,8 @@ export const api = {
|
||||
/** Videos to play a suggestion with, best first. `refresh` searches again. */
|
||||
youtubeVideos: (suggestionId: string, refresh = false) =>
|
||||
request<{ video_ids: string[]; cached: boolean }>(`/youtube/${suggestionId}?refresh=${refresh}`),
|
||||
/** Record a song played here (`submission: false` when it starts, true once listened to) */
|
||||
scrobble: (listen: Listen) => request<void>("/scrobble", json("POST", listen)),
|
||||
/** The video that played, when it wasn't the first one */
|
||||
setYoutubeVideo: (suggestionId: string, videoId: string) =>
|
||||
request<void>(`/youtube/${suggestionId}`, json("PUT", { video_id: videoId })),
|
||||
|
||||
@@ -5,6 +5,8 @@ export type QueueItem = {
|
||||
source: "library" | "youtube";
|
||||
/** Audio to stream, for library songs. YouTube ones play from `suggestionId`. */
|
||||
src: string;
|
||||
/** The library song's Subsonic id */
|
||||
songId?: string;
|
||||
title: string;
|
||||
artist: string;
|
||||
album?: string | null;
|
||||
@@ -16,6 +18,7 @@ export const fromSong = (song: Song): QueueItem => ({
|
||||
key: `song:${song.id}`,
|
||||
source: "library",
|
||||
src: streamUrl(song.id),
|
||||
songId: song.id,
|
||||
title: song.title,
|
||||
artist: song.artist,
|
||||
album: song.album,
|
||||
|
||||
@@ -218,6 +218,58 @@ export function usePlayer(onError: (item: QueueItem) => void) {
|
||||
return () => window.clearInterval(t);
|
||||
}, [onYouTube, status]);
|
||||
|
||||
// ---------- scrobbling ----------
|
||||
// "Now playing" when an item starts, and a listen once half of it, or 4 minutes, has been
|
||||
// played (Listenbrainz's rule). Seeking doesn't count as listening.
|
||||
|
||||
const listen = useRef<{
|
||||
key: string;
|
||||
startedAt: number;
|
||||
/** Seconds actually played */
|
||||
played: number;
|
||||
last: number;
|
||||
submitted: boolean;
|
||||
} | null>(null);
|
||||
|
||||
const scrobble = useCallback(
|
||||
(item: QueueItem, submission: boolean, startedAt: number, duration: number, video: string | null) => {
|
||||
const id = item.source === "youtube" ? item.suggestionId : item.songId;
|
||||
if (!id) return;
|
||||
void api
|
||||
.scrobble({
|
||||
source: item.source,
|
||||
id,
|
||||
submission,
|
||||
started_at: startedAt,
|
||||
duration: duration || undefined,
|
||||
video_id: item.source === "youtube" ? video ?? undefined : undefined,
|
||||
})
|
||||
.catch(() => undefined);
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (status !== "playing" || !current || listen.current?.key === current.key) return;
|
||||
const startedAt = Math.floor(Date.now() / 1000);
|
||||
listen.current = { key: current.key, startedAt, played: 0, last: time, submitted: false };
|
||||
scrobble(current, false, startedAt, duration, videoId);
|
||||
}, [status, current, time, duration, videoId, scrobble]);
|
||||
|
||||
useEffect(() => {
|
||||
const l = listen.current;
|
||||
if (!l || !current || l.key !== current.key) return;
|
||||
const delta = time - l.last;
|
||||
l.last = time;
|
||||
// (a jump is a seek, or the time going back to 0 for a new item)
|
||||
if (status !== "playing" || delta <= 0 || delta > 2) return;
|
||||
l.played += delta;
|
||||
if (!l.submitted && duration > 0 && l.played >= Math.min(duration / 2, 240)) {
|
||||
l.submitted = true;
|
||||
scrobble(current, true, l.startedAt, duration, videoId);
|
||||
}
|
||||
}, [time]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||
|
||||
// ---------- loading items ----------
|
||||
|
||||
useEffect(() => {
|
||||
@@ -233,6 +285,8 @@ export function usePlayer(onError: (item: QueueItem) => void) {
|
||||
el.pause();
|
||||
el.removeAttribute("src");
|
||||
el.load();
|
||||
// the previous YouTube song would play on while this one is looked up
|
||||
youtube.current?.stop();
|
||||
attempt.current = null;
|
||||
void playYouTube(current.suggestionId, load, false);
|
||||
} else {
|
||||
|
||||
Reference in new issue
Block a user