Merge pull request #124 from semvis123/master

Added Discord rich presence and added extra properties to songInfo provider
This commit is contained in:
th-ch
2021-01-13 22:08:10 +01:00
committed by GitHub
4 changed files with 84 additions and 2 deletions

View File

@ -24,7 +24,13 @@
"linux": {
"icon": "assets/generated/icons/png",
"category": "AudioVideo",
"target": ["AppImage", "snap", "freebsd", "deb", "rpm"]
"target": [
"AppImage",
"snap",
"freebsd",
"deb",
"rpm"
]
}
},
"scripts": {
@ -54,6 +60,7 @@
"@ffmpeg/core": "^0.8.5",
"@ffmpeg/ffmpeg": "^0.9.6",
"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",

View File

@ -0,0 +1,41 @@
const Discord = require('discord-rpc');
const rpc = new Discord.Client({
transport: 'ipc'
});
const clientId = '790655993809338398';
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);
});
});
};

View File

@ -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({
@ -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);
@ -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 = {
@ -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);

View File

@ -2911,6 +2911,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"
@ -8773,6 +8781,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"