Fixed issues with tracker classes

This commit is contained in:
Tom 2024-12-05 08:26:31 +00:00
parent 6e43d681da
commit 49633b7ee6
3 changed files with 61 additions and 39 deletions

View File

@ -1,13 +1,17 @@
class AggregateTracker { class AggregateTracker {
#trackers = []
constructor(trackers) { constructor(trackers) {
this._trackers = trackers; this.#trackers = trackers;
} }
poll() { async poll() {
const media = [] let media = []
for (let tracker of this._trackers) for (let tracker of this.#trackers)
media.push.apply(tracker.poll()); media = media.concat(await tracker.poll());
return media; return media;
} }
} }
module.exports = AggregateTracker;

View File

@ -2,39 +2,41 @@ const axios = require("axios");
const Song = require("../../models/song"); const Song = require("../../models/song");
class PlexTracker { class PlexTracker {
#config = null;
#cache = [];
constructor(config) { constructor(config) {
this._config = config; this.#config = config;
this._cache = []
} }
async poll(useCache = false) { async poll(useCache = false) {
if (!this._config.token || !this._config.url) if (!this.#config.token || !this.#config.url)
return []; return [];
if (useCache) if (useCache)
return cache; return this.#cache;
const response = await axios.get(this._config.url + "/status/sessions", { const response = await axios.get(this.#config.url + "/status/sessions", {
headers: { headers: {
"Accept": "application/json", "Accept": "application/json",
"X-Plex-Token": this._config.token "X-Plex-Token": this.#config.token
} }
}); });
if (!response.data.MediaContainer?.Metadata) { if (!response.data.MediaContainer?.Metadata) {
cache = []; this.#cache = [];
return cache; return this.#cache;
} }
const filtered = response.data.MediaContainer?.Metadata.filter(m => this.#filter(m)); const filtered = response.data.MediaContainer?.Metadata.filter(m => this.#filter(m));
cache = filtered.map(this.#transform); this.#cache = filtered.map(m => this.#transform(m));
return cache; return this.#cache;
} }
#filter(data) { #filter(data) {
if (!this._config.filters || this._config.filters.length == 0) if (!this.#config.filters || this.#config.filters.length == 0)
return true; return true;
for (let filter of this._config.filters) { for (let filter of this.#config.filters) {
if (filter.library && !filter.library.some(l => l == data.librarySectionTitle)) if (filter.library && !filter.library.some(l => l == data.librarySectionTitle))
continue; continue;
if (filter.ip && !filter.ip.some(l => l == data.address)) if (filter.ip && !filter.ip.some(l => l == data.address))

View File

@ -1,47 +1,63 @@
const axios = require("axios"); const axios = require("axios");
const logger = require("./logging"); const logger = require("../logging");
const fs = require("fs/promises"); const fs = require("fs/promises");
const fss = require("fs");
const Song = require("../../models/song"); const Song = require("../../models/song");
class SpotifyTracker { class SpotifyTracker {
constructor(config, token) { #config = null;
this._token = token; #token = null;
this._cache = null; #cache = null;
this._config = config; #auth = null;
this._auth = new Buffer.from(config.client_id + ':' + config.client_secret).toString('base64');
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');
} }
async poll(useCache = false) { async poll(useCache = false) {
if (token == null) if (this.#token == null)
return null; return [];
if (useCache) if (useCache)
return cache; return this.#cache;
if (token.expires_at < Date.now() + 300) if (this.#token.expires_at < Date.now() + 300)
await this.#refreshTokenIfNeeded(); await this.#refreshTokenIfNeeded();
try { try {
const response = await axios.get("https://api.spotify.com/v1/me/player/currently-playing", const response = await axios.get("https://api.spotify.com/v1/me/player/currently-playing",
{ {
headers: { headers: {
"Authorization": "Bearer " + token["access_token"] "Authorization": "Bearer " + this.#token.access_token
} }
} }
); );
if (!response.data) { if (!response.data) {
cache = null; this.#cache = [];
return null; return this.#cache;
} }
cache = [this.#transform(response.data)]; this.#cache = [this.#transform(response.data)];
return cache; return this.#cache;
} catch (ex) { } catch (ex) {
logger.error(ex, "Failed to get currently playing data from Spotify."); logger.error(ex, "Failed to get currently playing data from Spotify.");
return []; return [];
} }
} }
async loadCredentials() {
if (!fss.existsSync("credentials.spotify.json")) {
logger.info("No Spotify credentials found.");
return;
}
const content = await fs.readFile("credentials.spotify.json", "utf-8");
this.#token = JSON.parse(content);
}
#transform(data) { #transform(data) {
const item = data.item; const item = data.item;
const artists = item.artists.map(a => a.name); const artists = item.artists.map(a => a.name);
@ -51,18 +67,18 @@ class SpotifyTracker {
} }
async #refreshTokenIfNeeded() { async #refreshTokenIfNeeded() {
if (!this._token || this._token.expires_at && this._token.expires_at - Date.now() > 900) if (!this.#token || this.#token.expires_at && this.#token.expires_at - Date.now() > 900)
return false; return false;
const response = await axios.post("https://accounts.spotify.com/api/token", const response = await axios.post("https://accounts.spotify.com/api/token",
{ {
client_id: this._config.client_id, client_id: this.#config.client_id,
refresh_token: this._token.refresh_token, refresh_token: this.#token.refresh_token,
grant_type: "refresh_token" grant_type: "refresh_token"
}, },
{ {
headers: { headers: {
"Authorization": "Basic " + this._auth, "Authorization": "Basic " + this.#auth,
"Content-Type": "application/x-www-form-urlencoded" "Content-Type": "application/x-www-form-urlencoded"
} }
} }
@ -71,9 +87,9 @@ class SpotifyTracker {
const data = response.data; const data = response.data;
data["expires_at"] = Date.now() + data["expires_in"] * 1000; data["expires_at"] = Date.now() + data["expires_in"] * 1000;
if (!data["refresh_token"]) if (!data["refresh_token"])
data["refresh_token"] = this._token.refresh_token; data["refresh_token"] = this.#token.refresh_token;
this._token = data; this.#token = data;
await fs.writeFile("credentials.spotify.json", JSON.stringify(data)); await fs.writeFile("credentials.spotify.json", JSON.stringify(data));
logger.debug("Updated access token for Spotify."); logger.debug("Updated access token for Spotify.");
return true; return true;