Skip to content

Instantly share code, notes, and snippets.

@miraclx
Created October 12, 2020 00:53
Show Gist options
  • Select an option

  • Save miraclx/736eed8d2908919713630ec5e005be2b to your computer and use it in GitHub Desktop.

Select an option

Save miraclx/736eed8d2908919713630ec5e005be2b to your computer and use it in GitHub Desktop.
Print actively playing track information
#!/usr/bin/env zsh
function get_active_players() {
for m in `qdbus | egrep -i 'org.mpris.MediaPlayer2|plasma-browser-integration'`
if [[ `qdbus $m /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlaybackStatus` == 'Playing' ]]; then
echo $m
fi
}
function get_player_metadata() {
qdbus "$1" /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Metadata
}
function get_player_name() {
qdbus "$1" /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Identity
}
function decode_url() {
echo `echo "$1" | sed -E 's/%(\w{2})/\\\\x\1/g'`
}
function get_url_from_dbus_metadata() {
echo "$1" | grep 'xesam:url:' | sed -E 's/xesam:url: (.+)$/\1/g'
}
function url_is_file_protocol() {
echo "$1" | grep "file://" > /dev/null
[[ $? == 0 ]]
}
function strip_file_protocol() {
echo "$1" | sed -E "s/file:\/\///"
}
function get_song_artist_from_dbus_metadata() {
echo "$1" | grep 'xesam:artist:' | sed -E 's/xesam:artist: (.+)$/\1/g'
}
function get_song_title_from_dbus_metadata() {
echo "$1" | grep 'xesam:title:' | sed -E 's/xesam:title: (.+)$/\1/g'
}
function get_song_metadata() {
AtomicParsley "$1" -t
}
function get_uri_from_song_metadata() {
echo "$1" | grep 'com.apple.iTunes;SOURCE' | sed -E 's/.+contains: .+: (.+)$/\1/g'
}
function main() {
players=$(get_active_players)
if [[ -z "${players// }" ]]; then
echo "\x1b[33m[i]\x1b[0m No actively playing media detected!"
return
fi
for player in $players; do
player_name=$(get_player_name $player)
if [[ $active_only ]]; then
echo "$player_name"
else
[[ $quiet ]] || echo "[\x1b[34m$player_name\x1b[0m]"
metadata=$(get_player_metadata $player)
song_title=$(get_song_title_from_dbus_metadata $metadata)
song_artist=$(get_song_artist_from_dbus_metadata $metadata)
if [[ $who_only ]]; then
echo "$song_title by $song_artist"
else
[[ $quiet ]] || echo " \u25b6 \x1b[33m$song_title\x1b[0m by \x1b[36m$song_artist\x1b[0m"
raw_song_url=$(get_url_from_dbus_metadata $metadata)
if url_is_file_protocol $raw_song_url
then
stripped_url=$(strip_file_protocol "$raw_song_url")
song_url=$(decode_url "$stripped_url")
if [[ $path_only ]]; then
echo "$song_url"
else
[[ $quiet ]] || echo " \u2022 path = $song_url"
song_metadata=$(get_song_metadata "$song_url")
if [[ $meta_only ]]; then
echo "$song_metadata"
else
song_uri=$(get_uri_from_song_metadata $song_metadata)
[[ $quiet ]] && echo "$song_uri" || echo " \u2022 uri = $song_uri"
fi
fi
else [[ $quiet ]] || echo " \x1b[33m[i]\x1b[0m Non-file:// protocol, cannot extract song metadata"
fi
fi
fi
done
}
while getopts ":hmpwaq" flag; do
case $flag in
h)
echo "dtrak (c) 2020 Miraculous Owonubi"
echo "Show details of actively playing music"
echo ""
echo "Usage: ${0##*/} [-h] [-m] [-q]"
echo "\nOptions"
echo " -m print metadata only (implies \`-q\`)"
echo " -p print file path only (implies \`-q\`)"
echo " -w print title and artist only (implies \`-q\`)"
echo " -a print active players only (implies \`-q\`)"
echo " -q only show important data (defaults to printing URI only)"
echo " -h show help information"
return
;;
m)
meta_only=1
quiet=1
;;
p)
path_only=1
quiet=1
;;
w)
who_only=1
quiet=1
;;
a)
active_only=1
quiet=1
;;
q)
quiet=1
;;
?)
printf "%s invalid option -- '%s'\n" "${0##*/}" "$OPTARG"
;;
esac
done
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment