Add Discord RPC & fix artist in mediainfo (#40)

This commit is contained in:
Marie 2021-04-19 20:43:25 +02:00 committed by GitHub
parent 31670d0c2b
commit ab25bf16b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 84 additions and 2 deletions

View File

@ -23,6 +23,7 @@
"homepage": "https://github.com/Mastermindzh/tidal-hifi", "homepage": "https://github.com/Mastermindzh/tidal-hifi",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"discord-rpc": "^3.2.0",
"electron-store": "^5.1.1", "electron-store": "^5.1.1",
"express": "^4.17.1", "express": "^4.17.1",
"hotkeys-js": "^3.7.6", "hotkeys-js": "^3.7.6",

View File

@ -20,6 +20,7 @@ const settings = {
mpris: "mpris", mpris: "mpris",
enableCustomHotkeys: "enableCustomHotkeys", enableCustomHotkeys: "enableCustomHotkeys",
trayIcon: "trayIcon", trayIcon: "trayIcon",
enableDiscord: "enableDiscord",
windowBounds: { windowBounds: {
root: "windowBounds", root: "windowBounds",
width: "windowBounds.width", width: "windowBounds.width",

View File

@ -9,14 +9,15 @@ const {
} = require("./scripts/settings"); } = require("./scripts/settings");
const { addTray, refreshTray } = require("./scripts/tray"); const { addTray, refreshTray } = require("./scripts/tray");
const { addMenu } = require("./scripts/menu"); const { addMenu } = require("./scripts/menu");
const path = require("path"); const path = require("path");
const tidalUrl = "https://listen.tidal.com"; const tidalUrl = "https://listen.tidal.com";
const expressModule = require("./scripts/express"); const expressModule = require("./scripts/express");
const mediaKeys = require("./constants/mediaKeys"); const mediaKeys = require("./constants/mediaKeys");
const mediaInfoModule = require("./scripts/mediaInfo"); const mediaInfoModule = require("./scripts/mediaInfo");
const discordModule = require("./scripts/discord");
const globalEvents = require("./constants/globalEvents"); const globalEvents = require("./constants/globalEvents");
let mainWindow; let mainWindow;
let icon = path.join(__dirname, "../assets/icon.png"); let icon = path.join(__dirname, "../assets/icon.png");
@ -85,6 +86,7 @@ app.on("ready", () => {
addGlobalShortcuts(); addGlobalShortcuts();
store.get(settings.trayIcon) && addTray({ icon }) && refreshTray(); store.get(settings.trayIcon) && addTray({ icon }) && refreshTray();
store.get(settings.api) && expressModule.run(mainWindow); store.get(settings.api) && expressModule.run(mainWindow);
store.get(settings.enableDiscord) && discordModule.initRPC();
}); });
app.on("activate", function () { app.on("activate", function () {
@ -113,6 +115,12 @@ ipcMain.on(globalEvents.updateStatus, (event, arg) => {
}); });
ipcMain.on(globalEvents.storeChanged, (event, arg) => { ipcMain.on(globalEvents.storeChanged, (event, arg) => {
mainWindow.setMenuBarVisibility(store.get(settings.menuBar)); mainWindow.setMenuBarVisibility(store.get(settings.menuBar));
if(store.get(settings.enableDiscord) && !discordModule.rpc) {
discordModule.initRPC();
} else if(!store.get(settings.enableDiscord) && discordModule.rpc) {
discordModule.unRPC();
}
}); });
ipcMain.on(globalEvents.error, (event, arg) => { ipcMain.on(globalEvents.error, (event, arg) => {

View File

@ -20,6 +20,7 @@ function refreshSettings() {
trayIcon.checked = store.get(settings.trayIcon); trayIcon.checked = store.get(settings.trayIcon);
mpris.checked = store.get(settings.mpris); mpris.checked = store.get(settings.mpris);
enableCustomHotkeys.checked = store.get(settings.enableCustomHotkeys); enableCustomHotkeys.checked = store.get(settings.enableCustomHotkeys);
enableDiscord.checked = store.get(settings.enableDiscord);
} }
/** /**
@ -81,6 +82,7 @@ window.addEventListener("DOMContentLoaded", () => {
trayIcon = get("trayIcon"); trayIcon = get("trayIcon");
mpris = get("mprisCheckbox"); mpris = get("mprisCheckbox");
enableCustomHotkeys = get("enableCustomHotkeys"); enableCustomHotkeys = get("enableCustomHotkeys");
enableDiscord = get("enableDiscord");
refreshSettings(); refreshSettings();
@ -92,4 +94,5 @@ window.addEventListener("DOMContentLoaded", () => {
addInputListener(trayIcon, settings.trayIcon); addInputListener(trayIcon, settings.trayIcon);
addInputListener(mpris, settings.mpris); addInputListener(mpris, settings.mpris);
addInputListener(enableCustomHotkeys, settings.enableCustomHotkeys); addInputListener(enableCustomHotkeys, settings.enableCustomHotkeys);
addInputListener(enableDiscord, settings.enableDiscord);
}); });

View File

@ -141,6 +141,16 @@
<span class="slider round"></span> <span class="slider round"></span>
</label> </label>
</div> </div>
<div class="option">
<h4>Discord RPC</h4>
<p>
Show what you're listening to on Discord
</p>
<label class="switch">
<input id="enableDiscord" type="checkbox">
<span class="slider round"></span>
</label>
</div>
</div> </div>
</section> </section>
<section id="about" class="tab-panel"> <section id="about" class="tab-panel">

View File

@ -12,13 +12,14 @@ const notificationPath = `${app.getPath("userData")}/notification.jpg`;
let currentSong = ""; let currentSong = "";
let player; let player;
const elements = { const elements = {
play: '*[data-test="play"]', play: '*[data-test="play"]',
pause: '*[data-test="pause"]', pause: '*[data-test="pause"]',
next: '*[data-test="next"]', next: '*[data-test="next"]',
previous: 'button[data-test="previous"]', previous: 'button[data-test="previous"]',
title: '*[data-test^="footer-track-title"]', title: '*[data-test^="footer-track-title"]',
artists: '*[class^="mediaArtists"]', artists: '*[class^="css-14o5h2y"]',
home: '*[data-test="menu--home"]', home: '*[data-test="menu--home"]',
back: '[class^="backwardButton"]', back: '[class^="backwardButton"]',
forward: '[class^="forwardButton"]', forward: '[class^="forwardButton"]',
@ -30,6 +31,7 @@ const elements = {
settings: '*[data-test^="open-settings"]', settings: '*[data-test^="open-settings"]',
media: '*[data-test="current-media-imagery"]', media: '*[data-test="current-media-imagery"]',
image: "img", image: "img",
url: 'a[href*="/track/"]',
/** /**
* Get an element from the dom * Get an element from the dom
@ -223,6 +225,7 @@ function updateStatus() {
*/ */
setInterval(function () { setInterval(function () {
const title = elements.getText("title"); const title = elements.getText("title");
const url = elements.get("url").href.replace(/[^0-9]/g, '');
const artists = elements.getText("artists"); const artists = elements.getText("artists");
const songDashArtistTitle = `${title} - ${artists}`; const songDashArtistTitle = `${title} - ${artists}`;
@ -238,6 +241,7 @@ setInterval(function () {
const options = { const options = {
title, title,
message: artists, message: artists,
url: `https://tidal.com/browse/track/${url}`
}; };
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
if (image.startsWith("http")) { if (image.startsWith("http")) {

52
src/scripts/discord.js Normal file
View File

@ -0,0 +1,52 @@
const discordrpc = require("discord-rpc");
const clientId = '833617820704440341';
const mediaInfoModule = require("./mediaInfo");
const discordModule = [];
let discord;
let rpc;
const idleStatus = {
details: `Browsing Tidal`,
largeImageKey: 'tidal-hifi-icon',
largeImageText: 'Tidal HiFi 2.0.0',
instance: false,
}
discordModule.initRPC = function () {
rpc = new discordrpc.Client({ transport: 'ipc' });
rpc.login({ clientId }).catch(console.error);
discordModule.rpc = rpc;
rpc.on('ready', () => {
rpc.setActivity(idleStatus);
})
discord = setInterval(() => {
if (mediaInfoModule.mediaInfo.status == 'paused' && rpc) {
rpc.setActivity(idleStatus);
} else if (rpc) {
rpc.setActivity({
details: `Listening to ${mediaInfoModule.mediaInfo.title}`,
state: mediaInfoModule.mediaInfo.artist,
largeImageKey: 'tidal-hifi-icon',
largeImageText: 'Tidal HiFi 2.0.0',
buttons: [
{ label: "Play on Tidal", url: mediaInfoModule.mediaInfo.url }
],
instance: false,
});
}
}, 15e3);
}
discordModule.unRPC = function () {
clearInterval(discord);
rpc.clearActivity();
rpc.destroy();
rpc = false;
discordModule.rpc = rpc;
}
module.exports = discordModule;

View File

@ -5,6 +5,7 @@ const mediaInfo = {
artist: "", artist: "",
icon: "", icon: "",
status: statuses.paused, status: statuses.paused,
url: ""
}; };
const mediaInfoModule = { const mediaInfoModule = {
mediaInfo, mediaInfo,
@ -17,6 +18,7 @@ mediaInfoModule.update = function(arg) {
mediaInfo.title = propOrDefault(arg.title); mediaInfo.title = propOrDefault(arg.title);
mediaInfo.artist = propOrDefault(arg.message); mediaInfo.artist = propOrDefault(arg.message);
mediaInfo.icon = propOrDefault(arg.icon); mediaInfo.icon = propOrDefault(arg.icon);
mediaInfo.url = propOrDefault(arg.url);
}; };
/** /**

View File

@ -17,6 +17,7 @@ const store = new Store({
trayIcon: true, trayIcon: true,
mpris: false, mpris: false,
enableCustomHotkeys: false, enableCustomHotkeys: false,
enableDiscord: false,
windowBounds: { width: 800, height: 600 }, windowBounds: { width: 800, height: 600 },
}, },
}); });