From b9d696b77933e8e4a042e3414b000512bee135c1 Mon Sep 17 00:00:00 2001 From: navewindre Date: Sun, 17 Nov 2024 21:08:13 +0100 Subject: nowplaying script 4 musikcube --- nowplaying/playing.js | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100755 nowplaying/playing.js (limited to 'nowplaying/playing.js') 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 ) {} -- cgit v1.2.3