initial commit

This commit is contained in:
ShuriZma 2023-05-14 21:48:06 +02:00
parent e2f1e68752
commit 3fcef5e619
Signed by: ShuriZma
GPG Key ID: 8D289758EE9B8074
4 changed files with 215 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/node_modules/
/.idea/

123
package-lock.json generated Normal file
View File

@ -0,0 +1,123 @@
{
"name": "tidal-discord-rpc",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "tidal-discord-rpc",
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"discord-rpc": "^4.0.1",
"tidalapi-ts": "github:ShuriZma/TidalAPI"
}
},
"node_modules/bindings": {
"version": "1.5.0",
"license": "MIT",
"optional": true,
"dependencies": {
"file-uri-to-path": "1.0.0"
}
},
"node_modules/discord-rpc": {
"version": "4.0.1",
"license": "MIT",
"dependencies": {
"node-fetch": "^2.6.1",
"ws": "^7.3.1"
},
"optionalDependencies": {
"register-scheme": "github:devsnek/node-register-scheme"
}
},
"node_modules/file-uri-to-path": {
"version": "1.0.0",
"license": "MIT",
"optional": true
},
"node_modules/lodash": {
"version": "4.17.21",
"license": "MIT"
},
"node_modules/node-addon-api": {
"version": "1.7.2",
"license": "MIT",
"optional": true
},
"node_modules/node-fetch": {
"version": "2.6.7",
"license": "MIT",
"dependencies": {
"whatwg-url": "^5.0.0"
},
"engines": {
"node": "4.x || >=6.0.0"
},
"peerDependencies": {
"encoding": "^0.1.0"
},
"peerDependenciesMeta": {
"encoding": {
"optional": true
}
}
},
"node_modules/register-scheme": {
"version": "0.0.2",
"resolved": "git+ssh://git@github.com/devsnek/node-register-scheme.git#e7cc9a63a1f512565da44cb57316d9fb10750e17",
"hasInstallScript": true,
"license": "MIT",
"optional": true,
"dependencies": {
"bindings": "^1.3.0",
"node-addon-api": "^1.3.0"
}
},
"node_modules/tidalapi-ts": {
"version": "2.1.2",
"resolved": "git+ssh://git@github.com/ShuriZma/TidalAPI.git#205fe5c9ac8e7a5c9ecd34798b3bc360c23d9505",
"license": "MIT",
"dependencies": {
"lodash": "^4.17.20",
"node-fetch": "^2.6.1"
}
},
"node_modules/tr46": {
"version": "0.0.3",
"license": "MIT"
},
"node_modules/webidl-conversions": {
"version": "3.0.1",
"license": "BSD-2-Clause"
},
"node_modules/whatwg-url": {
"version": "5.0.0",
"license": "MIT",
"dependencies": {
"tr46": "~0.0.3",
"webidl-conversions": "^3.0.0"
}
},
"node_modules/ws": {
"version": "7.5.9",
"license": "MIT",
"engines": {
"node": ">=8.3.0"
},
"peerDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": "^5.0.2"
},
"peerDependenciesMeta": {
"bufferutil": {
"optional": true
},
"utf-8-validate": {
"optional": true
}
}
}
}
}

17
package.json Normal file
View File

@ -0,0 +1,17 @@
{
"name": "tidal-discord-rpc",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"type": "module",
"scripts": {
"main": "node src/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"discord-rpc": "^4.0.1",
"tidalapi-ts": "github:ShuriZma/TidalAPI"
}
}

73
src/index.js Normal file
View File

@ -0,0 +1,73 @@
'use strict';
/* eslint-disable no-console */
import {TidalAPI} from "tidalapi-ts";
import DiscordRPC from "discord-rpc";
import fs from "fs";
let token
fs.readFile('path-to-log-file', (err, data) => {
let matches = data.toString().match(/ey[A-z0-9\-]+\.[A-z0-9\-]+\.[A-z0-9\-]+/ig);
token = matches !== null ? matches[0] : '';
});
await new Promise(r => setTimeout(r, 100));
const api = new TidalAPI({
token: token,
countryCode: 'DE',
quality: 'LOSSLESS',
});
// Set this to your Client ID.
const clientId = 'client-id';
const rpc = new DiscordRPC.Client({ transport: 'ipc' });
async function setActivity() {
if (!rpc) {
return;
}
let startTimestamp = new Date();
let trackId = '';
fs.readFile('path-to-log-file', (err, data) => {
let matches = data.toString().match(/"trackId":(\d+)/ig);
trackId = matches !== null ? ((matches[1] ?? false) ? matches[1].match(/\d+/g)[0] : matches[0].match(/\d+/g)[0]) : '';
});
await new Promise(r => setTimeout(r, 100));
if (trackId !== '') {
const info = await api.getTrackInfo(trackId);
await rpc.setActivity({
details: info.title,
state: `${info.artist.name} - ${info.album.title}`,
startTimestamp,
largeImageKey: info.album.cover !== null ? api.getArtUrlSync(info.album.cover) : 'logo',
largeImageText: info.album.title,
smallImageKey: info.artists[0].picture !== null ? api.getArtUrlSync(info.artists[0].picture, 750, 750) : ((info.audioQuality === 'HI_RES' || info.audioQuality === 'LOSSLESS') ? info.audioQuality.toLocaleLowerCase() : 'play'),
smallImageText: info.artist.name,
instance: false,
buttons: [
{
label: 'Listen to song on Tidal',
'url': info.url,
}
]
});
}
}
rpc.on('ready', async () => {
await setActivity();
fs.watch('path-to-log-file', function (event) {
if (event === 'change') {
setActivity();
}
});
});
rpc.login({ clientId }).catch(console.error);