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/sdks
|
||||||
!.yarn/versions
|
!.yarn/versions
|
||||||
.pnp.*
|
.pnp.*
|
||||||
|
log.txt
|
||||||
@@ -605,25 +605,43 @@ function server(
|
|||||||
// Sonos Reporting Endpoint for playback analytics
|
// Sonos Reporting Endpoint for playback analytics
|
||||||
app.post("/report/:version/timePlayed", async (req, res) => {
|
app.post("/report/:version/timePlayed", async (req, res) => {
|
||||||
const version = req.params["version"];
|
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 {
|
try {
|
||||||
|
// Sonos may send an array of reports
|
||||||
|
const reports = Array.isArray(req.body) ? req.body : [req.body];
|
||||||
|
|
||||||
|
for (const report of reports) {
|
||||||
const {
|
const {
|
||||||
reportId,
|
reportId,
|
||||||
mediaUrl,
|
mediaUrl,
|
||||||
durationPlayedMillis,
|
durationPlayedMillis,
|
||||||
positionMillis,
|
positionMillis,
|
||||||
type,
|
type,
|
||||||
} = req.body;
|
} = report;
|
||||||
|
|
||||||
// Extract track ID from mediaUrl (format: /stream/track/{id})
|
// Extract track ID from mediaUrl (format: /stream/track/{id} or x-sonos-http:track%3a{id}.mp3)
|
||||||
const trackIdMatch = mediaUrl?.match(/\/stream\/track\/([^?]+)/);
|
let trackId: string | undefined;
|
||||||
if (!trackIdMatch) {
|
|
||||||
logger.warn(`Could not extract track ID from mediaUrl: ${mediaUrl}`);
|
if (mediaUrl) {
|
||||||
return res.status(200).json({ status: "ok" });
|
// 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);
|
const durationPlayedSeconds = Math.floor((durationPlayedMillis || 0) / 1000);
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
@@ -652,14 +670,14 @@ function server(
|
|||||||
if (serviceToken) {
|
if (serviceToken) {
|
||||||
await musicService.login(serviceToken).then((musicLibrary) => {
|
await musicService.login(serviceToken).then((musicLibrary) => {
|
||||||
// Get track duration to determine scrobbling threshold
|
// Get track duration to determine scrobbling threshold
|
||||||
return musicLibrary.track(trackId).then((track) => {
|
return musicLibrary.track(trackId!).then((track) => {
|
||||||
const shouldScrobble =
|
const shouldScrobble =
|
||||||
(track.duration < 30 && durationPlayedSeconds >= 10) ||
|
(track.duration < 30 && durationPlayedSeconds >= 10) ||
|
||||||
(track.duration >= 30 && durationPlayedSeconds >= 30);
|
(track.duration >= 30 && durationPlayedSeconds >= 30);
|
||||||
|
|
||||||
if (shouldScrobble) {
|
if (shouldScrobble) {
|
||||||
logger.info(`Scrobbling track ${trackId} after ${durationPlayedSeconds}s playback`);
|
logger.info(`Scrobbling track ${trackId} after ${durationPlayedSeconds}s playback`);
|
||||||
return musicLibrary.scrobble(trackId);
|
return musicLibrary.scrobble(trackId!);
|
||||||
} else {
|
} else {
|
||||||
logger.debug(
|
logger.debug(
|
||||||
`Not scrobbling track ${trackId}: duration=${track.duration}s, played=${durationPlayedSeconds}s`
|
`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");
|
logger.debug("No authentication available for reporting endpoint scrobble");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return res.status(200).json({ status: "ok" });
|
return res.status(200).json({ status: "ok" });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user