summaryrefslogtreecommitdiff
path: root/nowplaying/playing.js
diff options
context:
space:
mode:
authornavewindre <boneyaard@gmail.com>2024-11-17 21:08:13 +0100
committernavewindre <boneyaard@gmail.com>2024-11-17 21:08:13 +0100
commitb9d696b77933e8e4a042e3414b000512bee135c1 (patch)
tree09eae02f0eb9fbde987a6ab14a251441be27b1b8 /nowplaying/playing.js
parent3e5011cc5eb8331206bd15977c3c5b7cb37fb28f (diff)
nowplaying script 4 musikcube
Diffstat (limited to 'nowplaying/playing.js')
-rwxr-xr-xnowplaying/playing.js72
1 files changed, 72 insertions, 0 deletions
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 ) {}