File: mpris_now_playing.en_US.sh

package info (click to toggle)
eiskaltdcpp 2.4.2-1.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,676 kB
  • sloc: cpp: 97,597; ansic: 5,004; perl: 1,897; xml: 1,440; sh: 1,313; php: 661; javascript: 257; makefile: 39
file content (146 lines) | stat: -rwxr-xr-x 4,146 bytes parent folder | download | duplicates (2)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash
#
# This script is designed to show what is playing now in a mpris compatible media player
#
# Authors: Vovochka404 (vovochka13 (at) gmail.com), Nikoli <nikoli[at]lavabit.com>
# License: Public Domain
# Depends: bash, qdbus
#
# Example output:
# /me is listening to Three Days Grace - Wake Up (Three Days Grace) via Amarok
#
# Player support:
# For qmmp: Settings -> Plugins -> General -> MPRIS plugin
# For vlc: Preferences (full view) -> Interface -> Control interface -> D-bus control interface
# For audacious: Native
# For amarok: Native
# For dragon (KDE Dragon Player): Native
# For mpd (Music Player Daemon): http://mpd.wikia.com/wiki/Client:MpDris
# For songbird: http://addons.songbirdnest.com/addon/1626
# For clementine: Native since 0.3
# For exaile: Plugin. See http://www.exaile.org/wiki/KDE_and_MPRIS_plugin
# For deadbeef: Plugin: https://github.com/Serranya/deadbeef-mpris2-plugin
#
# See also:
# https://github.com/xmms2/wiki/wiki/MPRIS
# http://incise.org/mpris-remote.html


CONFIG_FILE="${HOME}/.config/eiskaltdc++/mpris_now_playing2.conf"

METADATA_CALL="/Player org.freedesktop.MediaPlayer.GetMetadata"
METADATA_CALL_V2="/org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Metadata"

STATUS_CALL="/Player org.freedesktop.MediaPlayer.GetStatus"
STATUS_CALL_V2="/org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlaybackStatus"

PLAYER=''
declare -A tag
declare -A paused

QDBUS=$(which qdbus 2>/dev/null);
if [ -z "$QDBUS" ]; then
	QDBUS=$(which qdbus-qt5 2>/dev/null);
fi
if [ -z "$QDBUS" ]; then
	echo "/me is missing qdbus or qdbus-qt5... :(";
	exit 0;
fi


DBUS_COLLECTION=$(${QDBUS} | grep "org.mpris.");

for app in $DBUS_COLLECTION; do
	IS_MPRIS2=$(echo $app | grep "MediaPlayer2" | grep -v "grep" | wc -l);
	PLAYER=$(echo $app | sed -r "s/.+\.//g" );
	if [ $IS_MPRIS2 -eq 0 ]; then
		MCALL=$METADATA_CALL;
		SCALL=$STATUS_CALL;
		_AP="--literal";
	else
		MCALL=$METADATA_CALL_V2;
		SCALL=$STATUS_CALL_V2;
		_AP="";
	fi
	METAINFO="$(${QDBUS} "${app}" ${MCALL} 2>/dev/null)";
	if [ -n "$METAINFO" ]; then
		STATUS="$($QDBUS ${_AP} "${app}" ${SCALL} 2>/dev/null)";
		if [ $IS_MPRIS2 -eq 0 ]; then
			STATUS=$(echo $STATUS | sed -r -e "s/\\[Argument: \\(iiii\\) ([0-9]+), .+\\]/\\1/");
			if [ "$STATUS" == "0" ]; then
				STATUS="Playing";
			else
				STATUS="Paused";
			fi
		fi
		for i in album artist genre title; do
			tag[${i}]="$(echo "${METAINFO}" | sed -r -e "/^(xesam:)?${i}: / !d" -e "s/^(xesam:)?${i}: //")"
		done
		# Works only for local files:
		tag["location"]="$(echo "${METAINFO}" | sed -r -e '/^(location|xesam:url): file:\/\// !d' -e 's/(location|xesam:url): file:\/\///' | sed -n -e's/%\([0-9A-F][0-9A-F]\)/\\x\1/g' -e's/+/ /g' -e's/.*/echo -e "&"/g' -ep | "${SHELL}")"
		tag["player"]=$PLAYER;
		if [ "$STATUS" == "Playing" ]; then
			break;
		else
			for i in album artist genre title location player; do
			paused[$i]=${tag[$i]};
			done
		fi
	else
		PLAYER="";
		STATUS="";
		tag["player"]="";
	fi
done

# If last player was not playing - getting last paused player.
if [ "$STATUS" != "Playing" ] && [ -n "${paused[player]}" ]; then
	for i in album artist genre title location player; do
		tag[$i]=${paused[$i]};
	done
fi;


# Some beautiful names for players
case ${tag["player"]} in
    amarok)
        tag["player"]="Amarok"
        ;;
    audacious)
        tag["player"]="Audacious2"
        ;;
    dragon)
        tag["player"]="Dragon Player"
        ;;
    vlc)
        tag["player"]="VLC"
        ;;
    clementine)
        tag["player"]="Clementine"
        ;;
esac

# Trying to load home config

# If got no config file, let's write basic config.
[ ! -e "${CONFIG_FILE}" ] && cat >> "${CONFIG_FILE}" << _EOF_
#
# This is an example config for mpris_now_playing script
#
if [ -n "\${tag[player]}" ];
then
    NOW_LISTENING_TO="/me is listening to \${tag[artist]} - \${tag[title]} (\${tag[album]}) via \${tag[player]} <magnet>\${tag[location]}</magnet>"
else
    NOW_LISTENING_TO="/me is listening to mouse clicks"
fi
_EOF_

. "${CONFIG_FILE}"

# Let's test what we'he got
if [ "${NOW_LISTENING_TO}" ]
then
    echo "${NOW_LISTENING_TO}"
else
    echo "/me is..."
fi