summaryrefslogtreecommitdiff
path: root/nowplaying/playing.js
blob: e7a50b912e2546a2f3abff6cc46fcec00f189177 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/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 artist = parsed.options.playing_track.artist;
    if( artist && artist.length > 0 ) {
      ret += artist;
      ret += ' - ';
    }

    if( title.startsWith( '/' ) ) {
      const file = title.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 ) );
}

setupws();