summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorReiner Herrmann <reiner@reiner-h.de>2016-12-08 20:08:42 +0100
committerReiner Herrmann <reiner@reiner-h.de>2016-12-08 20:08:42 +0100
commitac5a18a0345b397ed915cac9060316fea35b2cdc (patch)
tree12e41fd9809e41f6f855ba02fcf759c3651ced8c
parent02858423d30725f92d2a7c5ca548f7096862becd (diff)
added spotify-scrobble script
-rwxr-xr-xspotify-scrobble.sh64
1 files changed, 64 insertions, 0 deletions
diff --git a/spotify-scrobble.sh b/spotify-scrobble.sh
new file mode 100755
index 0000000..004b81b
--- /dev/null
+++ b/spotify-scrobble.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+#
+# Get currently played song from Spotify and scrobble with
+# moc-scrobbler (https://github.com/pachanka/moc-scrobbler)
+#
+
+DBUS_ARGS="--print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:org.mpris.MediaPlayer2.Player"
+
+scrobble() {
+ artist=$1
+ title=$2
+ album=$3
+ duration=$4
+
+ moc-scrobbler --artist="$1" --track="$2" --album="$3" --duration="$4"
+}
+
+while true
+do
+ status=`dbus-send $DBUS_ARGS string:PlaybackStatus 2>/dev/null`
+ if [ $? -gt 0 ]; then
+ echo "Spotify is not running."
+ exit
+ fi
+
+ if [ "`echo "$status" | tail -n1 | cut -d '"' -f 2`" != "Playing" ]; then
+ # currently nothing is playing; keep checking
+ old_artist=
+ old_title=
+ old_album=
+ old_duration=
+ sleep 5
+ continue
+ fi
+
+ metadata=`dbus-send $DBUS_ARGS string:Metadata`
+
+ artist=`echo "$metadata" | grep -A2 "\<artist\>" | tail -n1 | cut -d '"' -f 2`
+ title=`echo "$metadata" | grep -A1 "\<title\>" | tail -n1 | cut -d '"' -f 2`
+ album=`echo "$metadata" | grep -A1 "\<album\>" | tail -n1 | cut -d '"' -f 2`
+ duration=`echo "$metadata" | grep -A1 "\<length\>" | tail -n1 | awk '{ print $3 }'`
+ [ -n "$duration" ] && duration=`expr $duration / 1000000`
+
+ if [ -z "$artist" -o -z "$title" -o -z "$album" -o -z "$duration" ]; then
+ echo "Invalid data, not scrobbling. [$artist] [$title] [$album] [$duration]"
+ sleep 5
+ continue
+ fi
+
+ if [ "$old_artist" = "$artist" -a "$old_title" = "$title" ]; then
+ # don't scrobble same song as in last iteration
+ sleep 5
+ continue
+ fi
+
+ scrobble "$artist" "$title" "$album" "$duration"
+
+ old_artist=$artist
+ old_title=$title
+ old_album=$album
+ old_duration=$duration
+
+ sleep 5
+done