diff options
| author | navewindre <boneyaard@gmail.com> | 2024-11-17 21:08:13 +0100 |
|---|---|---|
| committer | navewindre <boneyaard@gmail.com> | 2024-11-17 21:08:13 +0100 |
| commit | b9d696b77933e8e4a042e3414b000512bee135c1 (patch) | |
| tree | 09eae02f0eb9fbde987a6ab14a251441be27b1b8 /nowplaying | |
| parent | 3e5011cc5eb8331206bd15977c3c5b7cb37fb28f (diff) | |
nowplaying script 4 musikcube
Diffstat (limited to 'nowplaying')
| -rw-r--r-- | nowplaying/package.json | 5 | ||||
| -rwxr-xr-x | nowplaying/playing.js | 72 |
2 files changed, 77 insertions, 0 deletions
diff --git a/nowplaying/package.json b/nowplaying/package.json new file mode 100644 index 0000000..37c3082 --- /dev/null +++ b/nowplaying/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "ws": "^8.18.0" + } +} diff --git a/nowplaying/playing.js b/nowplaying/playing.js new file mode 100755 index 0000000..3f334eb --- /dev/null +++ b/nowplaying/playing.js @@ -0,0 +1,72 @@ +#!/usr/bin/node + +const WebSocket = require( "ws" ); + +process.on( "uncaughtException", () => { + console.log(); +} ); + +process.on( "unhandledRejection", () => { + console.log(); +} ); + +let ws = null; +let idc = 1; +const setupws = () => { + try { + ws = new WebSocket( "ws://localhost:7905" ); + } catch( e ) { + + } + ws.onopen = () => { + const obj = { + name: "authenticate", + type: "request", + id: `msg-${idc++}`, + options: { + password: "1234" + } + } + + ws.send( JSON.stringify( obj ) ); + } + ws.onmessage = ( e ) => { + const parsed = JSON.parse( e.data ); + if( parsed.name == "authenticate" ) + return sendreq(); + + const title = parsed.options.playing_track.title; + let ret = "" + if( parsed.options.state != 'playing' ) + ret = '(⏸) '; + + const homedir = require( "os" ).homedir(); + if( title.startsWith( homedir ) ) { + const path = title.substring( homedir.length + 1 ); + const file = path.split( "/" ).pop(); + ret += file; + } + else ret += title; + if( ret.length > 30 ) + ret = ret.substring( 0, 30 ) + "..."; + console.log( ret ); + ws.close(); + }; +} + +const sendreq = () => { + const obj = { + name: "get_playback_overview", + type: "request", + id: `msg-${idc++}`, + options: { + password: "1234" + } + } + + ws.send( JSON.stringify( obj ) ); +} + +try { + setupws(); +} catch ( e ) {} |
