1#!/bin/bash
2#
3# This script is designed to show what is playing now in a mpris compatible media player
4#
5# Authors: Vovochka404 (vovochka13 (at) gmail.com), Nikoli <nikoli[at]lavabit.com>
6# License: Public Domain
7# Depends: bash, qdbus
8#
9# Example output:
10# /me is listening to Three Days Grace - Wake Up (Three Days Grace) via Amarok
11#
12# Player support:
13# For qmmp: Settings -> Plugins -> General -> MPRIS plugin
14# For vlc: Preferences (full view) -> Interface -> Control interface -> D-bus control interface
15# For audacious: Native
16# For amarok: Native
17# For dragon (KDE Dragon Player): Native
18# For mpd (Music Player Daemon): http://mpd.wikia.com/wiki/Client:MpDris
19# For songbird: http://addons.songbirdnest.com/addon/1626
20# For clementine: Native since 0.3
21# For exaile: Plugin. See http://www.exaile.org/wiki/KDE_and_MPRIS_plugin
22#
23# See also:
24# http://xmms2.org/wiki/MPRIS
25# http://incise.org/mpris-remote.html
26
27
28PLAYERS="amarok audacious qmmp mpd xmms2 dragon vlc songbird clementine exaile"
29
30CONFIG_FILE="${HOME}/.config/eiskaltdc++/mpris_now_playing.conf"
31
32METADATA_CALL="/Player org.freedesktop.MediaPlayer.GetMetadata"
33PLAYER=''
34declare -A tag
35
36# Trying to detect mpris compatible player and get info.
37for P in ${PLAYERS}; do
38    DBUS="$(qdbus | grep "org.mpris.${P}" | awk '{print $1}')"
39    if [[ "${DBUS}" && $(qdbus "${DBUS}" ${METADATA_CALL} 2>/dev/null) ]]
40    then
41        METAINFO="$(qdbus "${DBUS}" ${METADATA_CALL} 2>/dev/null)"
42        PLAYER="${P}"
43        for i in album artist genre title; do
44            tag[${i}]="$(echo "${METAINFO}" | sed -e "/^${i}: / !d" -e "s/^${i}: //")"
45        done
46        # Works only for local files:
47        LOCATION="$(echo "${METAINFO}" | sed -e '/^location: file:\/\// !d' -e 's/location: file:\/\///' | sed -n -e's/%\([0-9A-F][0-9A-F]\)/\\x\1/g' -e's/+/ /g' -e's/.*/echo -e "&"/g' -ep | "${SHELL}")"
48    fi
49done
50
51# Some beautiful names for players
52case ${PLAYER} in
53    amarok)
54        PLAYER="Amarok"
55        ;;
56    audacious)
57        PLAYER="Audacious2"
58        ;;
59    dragon)
60        PLAYER="Dragon Player"
61        ;;
62    vlc)
63        PLAYER="VLC"
64        ;;
65    clementine)
66        PLAYER="Clementine"
67        ;;
68esac
69
70# Trying to load home config
71
72# If got no config file, let's write basic config.
73[ ! -e "${CONFIG_FILE}" ] && cat >> "${CONFIG_FILE}" << _EOF_
74#
75# This is an example config for mpris_now_playing script
76#
77if [ "\${PLAYER}" ]
78then
79    NOW_LISTENING_TO="/me is listening to \${tag[artist]} - \${tag[title]} (\${tag[album]}) via \${PLAYER} <magnet>\${LOCATION}</magnet>"
80else
81    NOW_LISTENING_TO="/me is listening to mouse clicks"
82fi
83_EOF_
84
85. "${CONFIG_FILE}"
86
87# Let's test what we'he got
88if [ "${NOW_LISTENING_TO}" ]
89then
90    echo "${NOW_LISTENING_TO}"
91else
92    echo "/me is..."
93fi
94