59 lines
1.7 KiB
JavaScript
59 lines
1.7 KiB
JavaScript
const axios = require("axios");
|
|
const Song = require("../../models/song");
|
|
|
|
class PlexTracker {
|
|
constructor(config) {
|
|
this._config = config;
|
|
this._cache = []
|
|
}
|
|
|
|
async poll(useCache = false) {
|
|
if (!this._config.token || !this._config.url)
|
|
return [];
|
|
if (useCache)
|
|
return cache;
|
|
|
|
const response = await axios.get(this._config.url + "/status/sessions", {
|
|
headers: {
|
|
"Accept": "application/json",
|
|
"X-Plex-Token": this._config.token
|
|
}
|
|
});
|
|
|
|
if (!response.data.MediaContainer?.Metadata) {
|
|
cache = [];
|
|
return cache;
|
|
}
|
|
|
|
const filtered = response.data.MediaContainer?.Metadata.filter(m => this.#filter(m));
|
|
cache = filtered.map(this.#transform);
|
|
return cache;
|
|
}
|
|
|
|
#filter(data) {
|
|
if (!this._config.filters || this._config.filters.length == 0)
|
|
return true;
|
|
|
|
for (let filter of this._config.filters) {
|
|
if (filter.library && !filter.library.some(l => l == data.librarySectionTitle))
|
|
continue;
|
|
if (filter.ip && !filter.ip.some(l => l == data.address))
|
|
continue;
|
|
if (filter.deviceId && !filter.deviceId.some(l => l == data.machineIdentifier))
|
|
continue;
|
|
if (filter.platform && !filter.platform.some(l => l == data.Player.platform))
|
|
continue;
|
|
if (filter.product && !filter.product.some(l => l == data.Player.product))
|
|
continue;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
#transform(data) {
|
|
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");
|
|
}
|
|
}
|
|
|
|
module.exports = PlexTracker; |