diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c5690e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/node_modules/ +/.idea/ \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..e822145 --- /dev/null +++ b/package-lock.json @@ -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 + } + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..96076ba --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..e3aa886 --- /dev/null +++ b/src/index.js @@ -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);