aboutsummaryrefslogtreecommitdiff
path: root/extras/HOME/.local/bin/spotify-control
blob: e001eb64c25d33097595faa3541a02d55cb25037 (plain) (blame)
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
70
71
72
73
74
75
76
#!/bin/bash

if [ $# -lt 1 ]
then
        echo "No command?"
        exit
fi

if [ "$(pidof spotify)" = "" ] 
then
  echo "Spotify is not running"
  exit
fi

function mpris2_dbus_player_do {
  dbus-send \
    --print-reply \
    --dest=org.mpris.MediaPlayer2.spotify \
    /org/mpris/MediaPlayer2 \
    "org.mpris.MediaPlayer2.Player.$1"
}

function mpris2_dbus_get_player_property {
  dbus-send \
    --print-reply \
    --dest=org.mpris.MediaPlayer2.spotify \
    /org/mpris/MediaPlayer2 \
    org.freedesktop.DBus.Properties.Get \
    string:'org.mpris.MediaPlayer2.Player' "string:$1"
}

case $1 in
    "play")
        mpris2_dbus_player_do PlayPause
        ;;
    "next")
        mpris2_dbus_player_do Next
        ;;
    "prev")
        mpris2_dbus_player_do Previous
        ;;
    "getTitle")
        mpris2_dbus_get_player_property 'Metadata' | \
          egrep -A 1 "title" | \
          egrep -v "title" | \
          cut -b 44- | \
          cut -d '"' -f 1 | \
          egrep -v ^$
        ;;
    "getArtist")
        mpris2_dbus_get_player_property 'Metadata' | \
          -A 2 "artist" | \
          egrep -v "artist" | \
          egrep -v "array" | \
          cut -b 27- | \
          cut -d '"' -f 1 | \
          egrep -v ^$
        ;;
    "getAlbum")
        mpris2_dbus_get_player_property 'Metadata' | \
          egrep -A 2 "album" | \
          egrep -v "album" | \
          egrep -v "array" | \
          cut -b 44- | \
          cut -d '"' -f 1 | \
          egrep -v ^$
        ;;
    "getStatus")
        mpris_dbus_get_player_property 'PlaybackStatus' | \
          grep 'string "[^"]*"' | \
          sed 's/.*"\(.*\)"[^"]*$/\1/'
        ;;
    *)
        echo "Unknown command: " $1
        ;;
esac