Skip to content

Instantly share code, notes, and snippets.

@iamevn
Last active June 9, 2018 22:51
Show Gist options
  • Select an option

  • Save iamevn/ea66c353751c4d0c72866b37683530ef to your computer and use it in GitHub Desktop.

Select an option

Save iamevn/ea66c353751c4d0c72866b37683530ef to your computer and use it in GitHub Desktop.
displays discord status based on currently playing thing in plex. I couldn't get timestamps working for whatever reason
#!/usr/bin/node
const PlexAPI = require('plex-api');
const credentials = require('plex-api-credentials');
const userAndPass = credentials({
username: 'username',
password: 'hunter2'
});
const client = new PlexAPI({
hostname: 'localhost',
authenticator: userAndPass
});
const discord_client = require('discord-rich-presence')('435627965175300096')
const display_paused = false;
const doit = function () {
client.query('/status/sessions').then(function (result) {
// console.log(result);
if (result.MediaContainer.Metadata != undefined) {
// could check that video.User.title == 'iamevn' and try a different one if not
// console.log(result.MediaContainer.Metadata[0]);
const video = result.MediaContainer.Metadata[0];
if (display_paused || video.Player.state != 'paused') {
if (video.type == 'episode') {
const playpause = video.Player.state == 'playing' ? '▶️ ' : (video.Player.state == 'paused' ? '⏸ ' : '');
const details = playpause + video.grandparentTitle;
const state = video.parentTitle + ' Episode ' + video.index + ': ' + video.title;
const startTimestamp = Date.now() - video.viewOffset;
// const startTimestamp = video.viewOffset;
discord_client.updatePresence({
details: details,
state: state,
// startTimestamp: startTimestamp,
});
console.log('Watching on Plex\n%s\n%s\n%s', details, state, startTimestamp)
} else if (video.type == 'movie') {
const playpause = video.Player.state == 'playing' ? '▶️ ' : (video.Player.state == 'paused' ? '⏸ ' : '');
const details = playpause + video.title;
const startTimestamp = Date.now() - video.viewOffset;
// const startTimestamp = video.viewOffset;
discord_client.updatePresence({
details: details,
// startTimestamp: startTimestamp,
});
} else if (video.type == 'track') {
// video.type == 'track' //song
// video.title == song_name
// video.parentTitle == album_name
// video.grandparentTitle == artist_name?
// video.Player.state == 'playing'|'paused'|'buffering'
// video.startTimestamp == playback_state
} else {
console.log(result.MediaContainer.Metadata[0]);
}
}
} else {
console.log('Nothing playing');
}
}, function (err) {
console.error('Could not connect to server', err);
});
};
// run thing every 15 seconds
setInterval(doit, 15000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment