Added Spotify tracking. Fixed filters when none given.
This commit is contained in:
@ -2,7 +2,6 @@ const pino = require("pino");
|
||||
const logger = pino(pino.destination({ dest: 'logs/.log', sync: false }));
|
||||
|
||||
const environment = process.env.NODE_ENV || 'development';
|
||||
console.log(process.env.NODE_ENV);
|
||||
if (environment == "production") {
|
||||
logger.level = 30
|
||||
} else {
|
||||
|
@ -1,6 +1,7 @@
|
||||
const plex = require("./plex");
|
||||
const logger = require("./logging")
|
||||
const config = require("../config/configuration");
|
||||
const spotify = require("./spotify");
|
||||
|
||||
const lastPlaying = {};
|
||||
const lastScrobbleTimes = {};
|
||||
@ -9,6 +10,12 @@ async function poll() {
|
||||
const now = Date.now();
|
||||
const playing = [];
|
||||
|
||||
await spotify.loadCredentials();
|
||||
await spotify.refreshTokenIfNeeded();
|
||||
const spotifyTrack = await spotify.getCurrentlyPlaying();
|
||||
if (spotifyTrack != null)
|
||||
playing.push(spotifyTrack);
|
||||
|
||||
try {
|
||||
const data = await plex.getCurrentlyPlaying();
|
||||
playing.push.apply(playing, data);
|
||||
@ -46,8 +53,8 @@ async function poll() {
|
||||
}
|
||||
|
||||
function applyFilter(track, filters) {
|
||||
if (!filters)
|
||||
return;
|
||||
if (!filters || filters.length == 0)
|
||||
return true;
|
||||
|
||||
for (let filter of filters) {
|
||||
if (filter.library && !filter.library.some(l => l == track.library))
|
||||
@ -67,7 +74,7 @@ function applyFilter(track, filters) {
|
||||
|
||||
function checkIfCanScrobble(current, previous, now) {
|
||||
if (!previous)
|
||||
return;
|
||||
return false;
|
||||
|
||||
let filters = [];
|
||||
if (previous.source == 'plex')
|
||||
|
98
services/spotify.js
Normal file
98
services/spotify.js
Normal file
@ -0,0 +1,98 @@
|
||||
const axios = require("axios");
|
||||
const config = require("../config/configuration")
|
||||
const logger = require("./logging");
|
||||
const fs = require("fs/promises");
|
||||
const fss = require("fs")
|
||||
|
||||
let token = null;
|
||||
let cache = {}
|
||||
|
||||
async function refreshTokenIfNeeded() {
|
||||
if (!token || token["expires_at"] && token["expires_at"] - Date.now() > 900) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await axios.post("https://accounts.spotify.com/api/token",
|
||||
{
|
||||
client_id: config.spotify.client_id,
|
||||
refresh_token: token["refresh_token"],
|
||||
grant_type: "refresh_token"
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
"Authorization": "Basic " + new Buffer.from(config.spotify.client_id + ':' + config.spotify.client_secret).toString('base64'),
|
||||
"Content-Type": "application/x-www-form-urlencoded"
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
const data = response.data;
|
||||
data["expires_at"] = Date.now() + data["expires_in"] * 1000;
|
||||
if (!data["refresh_token"]) {
|
||||
data["refresh_token"] = token["refresh_token"];
|
||||
}
|
||||
await fs.writeFile("credentials.spotify.json", JSON.stringify(data));
|
||||
token = data;
|
||||
logger.info("Updated access token for Spotify.");
|
||||
return true;
|
||||
} catch (ex) {
|
||||
logger.error(ex, "Failed to get Spotify oauth.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function loadCredentials() {
|
||||
if (!fss.existsSync("credentials.spotify.json")) {
|
||||
logger.info("No Spotify credentials found.");
|
||||
return;
|
||||
}
|
||||
|
||||
const content = await fs.readFile("credentials.spotify.json", "utf-8");
|
||||
token = JSON.parse(content);
|
||||
}
|
||||
|
||||
async function getCurrentlyPlaying(cached = false) {
|
||||
if (cached) {
|
||||
return cache['spotify']
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await axios.get("https://api.spotify.com/v1/me/player/currently-playing",
|
||||
{
|
||||
headers: {
|
||||
"Authorization": "Bearer " + token["access_token"]
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.data) {
|
||||
cache['spotify'] = null;
|
||||
return null;
|
||||
}
|
||||
|
||||
const media = response.data.item;
|
||||
cache['spotify'] = {
|
||||
"track": media.name,
|
||||
"album": media.album.name,
|
||||
"artist": media.artists.map(a => a.name).join(', '),
|
||||
"year": media.parentYear,
|
||||
"duration": media.duration_ms,
|
||||
"playtime": response.data.progress_ms,
|
||||
"mediaKey": media.id,
|
||||
"sessionKey": "spotify",
|
||||
"state": response.data.is_playing ? "playing" : "paused",
|
||||
"source": "spotify"
|
||||
};
|
||||
return cache['spotify'];
|
||||
} catch (ex) {
|
||||
logger.error(ex, "Failed to get currently playing data from Spotify.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
refreshTokenIfNeeded,
|
||||
loadCredentials,
|
||||
getCurrentlyPlaying
|
||||
}
|
Reference in New Issue
Block a user