mirror of
https://github.com/wkulhanek/bonob.git
synced 2025-12-21 17:33:29 +01:00
Update scrobble logic
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -11,3 +11,4 @@ node_modules
|
||||
!.yarn/sdks
|
||||
!.yarn/versions
|
||||
.pnp.*
|
||||
log.txt
|
||||
@@ -605,25 +605,43 @@ function server(
|
||||
// Sonos Reporting Endpoint for playback analytics
|
||||
app.post("/report/:version/timePlayed", async (req, res) => {
|
||||
const version = req.params["version"];
|
||||
logger.debug(`Received Sonos reporting event (v${version}):`, JSON.stringify(req.body));
|
||||
logger.debug(`Received Sonos reporting event (v${version}): ${JSON.stringify(req.body)}`);
|
||||
|
||||
try {
|
||||
// Sonos may send an array of reports
|
||||
const reports = Array.isArray(req.body) ? req.body : [req.body];
|
||||
|
||||
for (const report of reports) {
|
||||
const {
|
||||
reportId,
|
||||
mediaUrl,
|
||||
durationPlayedMillis,
|
||||
positionMillis,
|
||||
type,
|
||||
} = req.body;
|
||||
} = report;
|
||||
|
||||
// Extract track ID from mediaUrl (format: /stream/track/{id})
|
||||
const trackIdMatch = mediaUrl?.match(/\/stream\/track\/([^?]+)/);
|
||||
if (!trackIdMatch) {
|
||||
logger.warn(`Could not extract track ID from mediaUrl: ${mediaUrl}`);
|
||||
return res.status(200).json({ status: "ok" });
|
||||
// Extract track ID from mediaUrl (format: /stream/track/{id} or x-sonos-http:track%3a{id}.mp3)
|
||||
let trackId: string | undefined;
|
||||
|
||||
if (mediaUrl) {
|
||||
// Try standard URL format first
|
||||
const standardMatch = mediaUrl.match(/\/stream\/track\/([^?]+)/);
|
||||
if (standardMatch) {
|
||||
trackId = standardMatch[1];
|
||||
} else {
|
||||
// Try x-sonos-http format (track%3a{id}.mp3)
|
||||
const sonosMatch = mediaUrl.match(/track%3[aA]([^.?&]+)/);
|
||||
if (sonosMatch) {
|
||||
trackId = sonosMatch[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!trackId) {
|
||||
logger.warn(`Could not extract track ID from mediaUrl: ${mediaUrl}, full report: ${JSON.stringify(report)}`);
|
||||
continue; // Skip this report, process next one
|
||||
}
|
||||
|
||||
const trackId = trackIdMatch[1];
|
||||
const durationPlayedSeconds = Math.floor((durationPlayedMillis || 0) / 1000);
|
||||
|
||||
logger.info(
|
||||
@@ -652,14 +670,14 @@ function server(
|
||||
if (serviceToken) {
|
||||
await musicService.login(serviceToken).then((musicLibrary) => {
|
||||
// Get track duration to determine scrobbling threshold
|
||||
return musicLibrary.track(trackId).then((track) => {
|
||||
return musicLibrary.track(trackId!).then((track) => {
|
||||
const shouldScrobble =
|
||||
(track.duration < 30 && durationPlayedSeconds >= 10) ||
|
||||
(track.duration >= 30 && durationPlayedSeconds >= 30);
|
||||
|
||||
if (shouldScrobble) {
|
||||
logger.info(`Scrobbling track ${trackId} after ${durationPlayedSeconds}s playback`);
|
||||
return musicLibrary.scrobble(trackId);
|
||||
return musicLibrary.scrobble(trackId!);
|
||||
} else {
|
||||
logger.debug(
|
||||
`Not scrobbling track ${trackId}: duration=${track.duration}s, played=${durationPlayedSeconds}s`
|
||||
@@ -674,6 +692,7 @@ function server(
|
||||
logger.debug("No authentication available for reporting endpoint scrobble");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res.status(200).json({ status: "ok" });
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user