Song: Move dbus commands to a function

This commit is contained in:
Dylan Araps 2016-12-15 23:40:54 +11:00
parent ca8c45b668
commit 8aef69b77f
1 changed files with 16 additions and 28 deletions

View File

@ -1139,6 +1139,18 @@ get_song() {
# This is absurdly long. # This is absurdly long.
player="$(ps x | awk '!(/awk|Helper|Cache/) && /mpd|cmus|mocp|spotify|Google Play|iTunes.app|rhythmbox|banshee|amarok|deadbeef|audacious|gnome-music|lollypop|clementine|pragha/ {printf $5 " " $6; exit}')" player="$(ps x | awk '!(/awk|Helper|Cache/) && /mpd|cmus|mocp|spotify|Google Play|iTunes.app|rhythmbox|banshee|amarok|deadbeef|audacious|gnome-music|lollypop|clementine|pragha/ {printf $5 " " $6; exit}')"
get_song_dbus() {
# Multiple players use an almost identical dbus command to get the information.
# This function saves us including the same command throughtout the function.
song="$(\
dbus-send --print-reply --dest=org.mpris.MediaPlayer2."${1}" /org/mpris/MediaPlayer2 \
org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata' |\
awk -F 'string "' '/string|array/ {printf "%s",$2; next}{print ""}' |\
awk -F '"' '/artist|title/ {printf $2 " - "}'
)"
song="${song% - }"
}
case "${player/*\/}" in case "${player/*\/}" in
"mpd"*) "mpd"*)
song="$(mpc current)" song="$(mpc current)"
@ -1159,18 +1171,7 @@ get_song() {
"spotify"*) "spotify"*)
case "$os" in case "$os" in
"Linux") "Linux") get_song_dbus "spotify" ;;
# Thanks dbus
song="$(\
dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 \
org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata' |\
awk -F 'string "' '/string|array/ {printf "%s",$2; next}{print ""}' |\
awk -F '"' '/artist|title/ {printf $2 " - "}'
)"
song="${song% - }"
song="${song/'('*}"
song="${song//'['*}"
;;
"Mac OS X") "Mac OS X")
song="$(osascript -e 'tell application "Spotify" to artist of current track as string & " - " & name of current track as string')" song="$(osascript -e 'tell application "Spotify" to artist of current track as string & " - " & name of current track as string')"
@ -1219,22 +1220,9 @@ get_song() {
song="$(audtool current-song)" song="$(audtool current-song)"
;; ;;
"gnome-music"* | "lollypop"* | "clementine"*) "gnome-music"*) get_song_dbus "GnomeMusic" ;;
case "$player" in "lollypop"*) get_song_dbus "Lollypop" ;;
"gnome-music"*) player="GnomeMusic" ;; "clementine"*) get_song_dbus "clementine" ;;
"lollypop"*) player="Lollypop" ;;
"clementine"*) player="clementine" ;;
esac
# Hello dbus my old friend.
song="$(\
dbus-send --print-reply --dest=org.mpris.MediaPlayer2."${player}" /org/mpris/MediaPlayer2 \
org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata' |\
awk -F 'string "' '/string|array/ {printf "%s",$2; next}{print ""}' |\
awk -F '"' '/artist|title/ {printf $2 " - "}'
)"
song="${song% - }"
;;
"pragha"*) "pragha"*)
artist="$(pragha -c | awk -F':' '/artist/ {print $2}')" artist="$(pragha -c | awk -F':' '/artist/ {print $2}')"