Added scrobbling functionality. Changes all around to support scrobbling, such as named trackers and configuration changes.

This commit is contained in:
Tom
2024-12-05 17:32:37 +00:00
parent b30dc3396a
commit 5100d18ac6
10 changed files with 184 additions and 46 deletions

View File

@@ -1,10 +1,23 @@
class AggregateTracker {
#name = null;
#trackers = []
provider = null;
constructor(trackers) {
constructor(name, trackers) {
this.#name = name;
this.#trackers = trackers;
}
get name() {
return this.#name;
}
get scrobblerNames() {
return this.#trackers.map(t => t.scrobblerNames)
.flat()
.filter((v, i, a) => a.indexOf(v) == i);
}
async poll() {
let media = []
for (let tracker of this.#trackers)

View File

@@ -4,11 +4,20 @@ const Song = require("../../models/song");
class PlexTracker {
#config = null;
#cache = [];
provider = "plex";
constructor(config) {
this.#config = config;
}
get name() {
return this.#config.name;
}
get scrobblerNames() {
return this.#config.scrobblers;
}
async poll(useCache = false) {
if (!this.#config.token || !this.#config.url)
return [];
@@ -28,7 +37,7 @@ class PlexTracker {
}
const filtered = response.data.MediaContainer?.Metadata.filter(m => this.#filter(m));
this.#cache = filtered.map(m => this.#transform(m));
this.#cache = filtered.map(m => this.#transform(m, this.#config.name));
return this.#cache;
}
@@ -52,9 +61,10 @@ class PlexTracker {
return false;
}
#transform(data) {
#transform(data, source) {
const id = data.guid.substring(data.guid.lastIndexOf('/') + 1);
return new Song(id, data.title, data.parentTitle, data.grandparentTitle, data.parentYear, data.duration, data.viewOffset, data.sessionKey, data.Player.state, "plex");
const artists = data.grandparentTitle.split(',').map(a => a.trim());
return new Song(id, data.title, data.parentTitle, artists, data.parentYear, data.duration, data.viewOffset, data.sessionKey, data.Player.state, source, "plex");
}
}

View File

@@ -7,16 +7,24 @@ const Song = require("../../models/song");
class SpotifyTracker {
#config = null;
#token = null;
#cache = null;
#cache = [];
#auth = null;
provider = "spotify";
constructor(config, token = null) {
this.#config = config;
this.#token = token;
this.#cache = null;
this.#auth = new Buffer.from(config.client_id + ':' + config.client_secret).toString('base64');
}
get name() {
return this.#config.name;
}
get scrobblerNames() {
return this.#config.scrobblers;
}
async poll(useCache = false) {
if (this.#token == null)
return [];
@@ -40,7 +48,7 @@ class SpotifyTracker {
return this.#cache;
}
this.#cache = [this.#transform(response.data)];
this.#cache = [this.#transform(response.data, this.#config.name)];
return this.#cache;
} catch (ex) {
logger.error(ex, "Failed to get currently playing data from Spotify.");
@@ -58,12 +66,12 @@ class SpotifyTracker {
this.#token = JSON.parse(content);
}
#transform(data) {
#transform(data, source) {
const item = data.item;
const artists = item.artists.map(a => a.name);
const year = null;
const state = data.is_playing ? "playing" : "paused";
return new Song(item.id, item.name, item.album.name, artists, year, item.duration_ms, data.progress_ms, "spotify", state, "spotify");
return new Song(item.id, item.name, item.album.name, artists, year, item.duration_ms, data.progress_ms, "spotify", state, source, "spotify");
}
async #refreshTokenIfNeeded() {