Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Discord rich presence and added extra properties to songInfo provider #124

Merged
merged 2 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@
"linux": {
"icon": "assets/generated/icons/png",
"category": "AudioVideo",
"target": ["AppImage", "snap", "freebsd", "deb", "rpm"]
"target": [
"AppImage",
"snap",
"freebsd",
"deb",
"rpm"
]
}
},
"scripts": {
Expand Down Expand Up @@ -53,6 +59,7 @@
"@ffmpeg/core": "^0.8.4",
"@ffmpeg/ffmpeg": "^0.9.5",
"YoutubeNonStop": "git://github.com/lawfx/YoutubeNonStop.git#v0.8.0",
"discord-rpc": "^3.1.4",
"downloads-folder": "^3.0.1",
"electron-debug": "^3.1.0",
"electron-is": "^3.0.0",
Expand Down
41 changes: 41 additions & 0 deletions plugins/discord-rpc/back.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const Discord = require('discord-rpc');
const rpc = new Discord.Client({
transport: 'ipc'
});

const clientId = '790655993809338398';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick question about the ID, did you register an application in Discord under your own account?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uhm yes, but it can be changed to yours if you want :)


module.exports = win => {
// If the page is ready, register the callback
win.on('ready-to-show', () => {
// Startup the rpc client
rpc.login({
clientId
}).catch(console.error);

// Register the callback
global.songInfo.onNewData(songInfo => {
// Song information changed, so lets update the rich presence

const activityInfo = {
details: songInfo.title,
state: songInfo.artist,
largeImageKey: 'logo',
largeImageText: songInfo.views + ' - ' + songInfo.likes
};

if (songInfo.isPaused) {
// Add an idle icon to show that the song is paused
activityInfo.smallImageKey = 'idle';
activityInfo.smallImageText = 'idle/paused';
} else {
// Add the start and end time of the song
const songStartTime = Date.now() - (songInfo.elapsedSeconds * 1000);
activityInfo.startTimestamp = songStartTime;
activityInfo.endTimestamp = songStartTime + (songInfo.songDuration * 1000);
}

rpc.setActivity(activityInfo);
});
});
};
23 changes: 22 additions & 1 deletion providers/song-info/back.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const imageSelector = '#layout > ytmusic-player-bar > div.middle-controls.style-
// This selects the song subinfo, this includes artist, views, likes
const subInfoSelector = '#layout > ytmusic-player-bar > div.middle-controls.style-scope.ytmusic-player-bar > div.content-info-wrapper.style-scope.ytmusic-player-bar > span';

// This selects the progress bar, used for songlength and current progress
const progressSelector = '#progress-bar';

// This is used for to control the songs
const presskey = (window, key) => {
window.webContents.sendInputEvent({
Expand Down Expand Up @@ -55,6 +58,18 @@ const getSubInfo = async win => {
return subInfo;
};

// Grab the progress using the selector
const getProgress = async win => {
// Get max value of the progressbar element
const songDuration = await win.webContents.executeJavaScript(
'document.querySelector("' + progressSelector + '").max');
// Get current value of the progressbar element
const elapsedSeconds = await win.webContents.executeJavaScript(
'document.querySelector("' + progressSelector + '").value');

return {songDuration, elapsedSeconds};
};

// Grab the native image using the src
const getImage = async src => {
const result = await fetch(src);
Expand All @@ -79,7 +94,9 @@ module.exports = win => {
likes: '',
imageSrc: '',
image: null,
isPaused: true
isPaused: true,
songDuration: 0,
elapsedSeconds: 0
};
// The song control functions
global.songControls = {
Expand All @@ -102,6 +119,10 @@ module.exports = win => {
global.songInfo.title = await getTitle(win);
global.songInfo.isPaused = await getPausedStatus(win);

const {songDuration, elapsedSeconds} = await getProgress(win);
global.songInfo.songDuration = songDuration;
global.songInfo.elapsedSeconds = elapsedSeconds;

// If title changed then we do need to update other info
if (oldTitle !== global.songInfo.title) {
const subInfo = await getSubInfo(win);
Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2885,6 +2885,14 @@ dir-glob@^2.2.2:
dependencies:
path-type "^3.0.0"

discord-rpc@^3.1.4:
version "3.1.4"
resolved "https://registry.yarnpkg.com/discord-rpc/-/discord-rpc-3.1.4.tgz#6d449a682e6a0dec4f0444d5f36f9ebfabaccf91"
integrity sha512-QaBu+gHica2SzgRAmTpuJ4J8DX9+fDwAqhvaie3hcbkU9WPqewEPh21pWdd/7vTI/JNuapU7PFm2ZKg3BTkbGg==
dependencies:
node-fetch "^2.6.1"
ws "^7.3.1"

dmg-builder@22.8.1:
version "22.8.1"
resolved "https://registry.yarnpkg.com/dmg-builder/-/dmg-builder-22.8.1.tgz#9b3bcbbc43e5fed232525d61a5567ea4b66085c3"
Expand Down Expand Up @@ -8745,6 +8753,11 @@ ws@^7.2.3:
resolved "https://registry.yarnpkg.com/ws/-/ws-7.3.1.tgz#d0547bf67f7ce4f12a72dfe31262c68d7dc551c8"
integrity sha512-D3RuNkynyHmEJIpD2qrgVkc9DQ23OrN/moAwZX4L8DfvszsJxpjQuUq3LMx6HoYji9fbIOBY18XWBsAux1ZZUA==

ws@^7.3.1:
version "7.4.2"
resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.2.tgz#782100048e54eb36fe9843363ab1c68672b261dd"
integrity sha512-T4tewALS3+qsrpGI/8dqNMLIVdq/g/85U98HPMa6F0m6xTbvhXU6RCQLqPH3+SlomNV/LdY6RXEbBpMH6EOJnA==

xdg-basedir@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/xdg-basedir/-/xdg-basedir-4.0.0.tgz#4bc8d9984403696225ef83a1573cbbcb4e79db13"
Expand Down