-
Notifications
You must be signed in to change notification settings - Fork 3
/
mprisctl
executable file
·110 lines (102 loc) · 3.48 KB
/
mprisctl
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
#
# Copyright (C) 2016 barbUk
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
#
set -o errexit -o pipefail -o nounset
readonly state_file=/tmp/mpris_last_player
readonly tmp_art_file=/tmp/mpris_tmp_art_file
readonly tmp_art_filename=/tmp/mpris_tmp_art_filename
action="${1:-info}"
mpris='no player detected'
artfile=
info() {
playerctl \
--all-players \
--format '{{playerName}};{{status}};{{xesam:artist}};{{xesam:album}};{{xesam:title}};{{mpris:artUrl}}' \
metadata 2>/dev/null
}
handle_art() {
local art="$1"
# Online art
if [[ "$art" == "https://"* ]]; then
if ! ([ -f "$tmp_art_filename" ] && [ -f "$tmp_art_file" ] && grep -q "$art" "$tmp_art_filename"); then
curl --silent --output "$tmp_art_file" "$art"
echo "$art" > "$tmp_art_filename"
fi
echo "$tmp_art_file"
elif [[ "$art" == "file://"* ]]; then
echo "${art#*file://}"
fi
}
if ! [[ $(playerctl status 2>&1) == 'No players found' ]]; then
case "$action" in
'info' )
while IFS=';' read -r player status artist album title art; do
if [ -z "$album" ]; then
album='n/a'
fi
if [ -z "$artist" ]; then
artist='n/a'
fi
if [ -z "$art" ]; then
art="file://$HOME/.dotfiles/img/default_player_icon.svg"
fi
if [ "$status" = Playing ]; then
mpris="$status\n$artist\n$album\n$title\n"
artfile="$art"
echo "$player" > "$state_file"
elif [ -e "$state_file" ] && [ "$(cat "$state_file")" = "$player" ]; then
mpris="$status\n$artist\n$album\n$title\n"
artfile="$art"
elif [ "$mpris" = 'no player detected' ]; then
mpris="$status\n$artist\n$album\n$title\n"
fi
done < <(info)
artfile="$(handle_art "$artfile")"
echo -e "$mpris$artfile"
;;
* )
while IFS=';' read -r player status artist album title art; do
if [ "$status" = Playing ]; then
playerctl --player="$player" "$action"
mpris="$player"
echo "$player" > "$state_file"
fi
done < <(info)
if [ "$mpris" = 'no player detected' ] && [ -e "$state_file" ]; then
if ! playerctl --player="$(cat "$state_file")" "$action" 2>/dev/null; then
playerctl "$action"
rm "$state_file"
fi
elif ! [ -e "$state_file" ]; then
while IFS=';' read -r player status artist album title art; do
# If we have a playing status, give it the command
if [ "$status" = Playing ]; then
playerctl --player="$player" "$action"
mpris="$player"
echo "$player" > "$state_file"
fi
done < <(info)
# If after that we still don't have a player detected,
# pass the command to the default player
if ! [ -e "$state_file" ]; then
playerctl "$action"
fi
fi
;;
esac
else
echo 'no player detected'
fi