söndag 17 februari 2013

Spotify in notification

Hello,
 I have made a little script that sends a notification of the Artist, Album and Song that is playing on Spotify.
It is checked every fifth second but not updated if it has not changed.
Change the sleep command to change the update frequency.

So I thought I would share it with the world :)
       



#!/bin/bash

# --------------------------------------
#
#    Author: Jonas Lindberg
#    Contact: badomen02 gmail
#    Created: February 17, 2013
#
#    Purpose: Pop up notification of what Spotify is playing.
#    Usage: Start Spotify then start this script. This script will self exit when spotify exits. 
#    Dependencies: libnotify-bin, sudo apt-get install libnotify-bin to install it.
#    Troubleshooting: Test this command notify-send "Artist" "Album" in a terminal.
#
#    This script is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY.
#
# --------------------------------------

echo "Started"
title_playing=""
# It will run as long spotify is runing.
while [ "$(pidof spotify)" ];do
  title_compare="$(dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata' 2>/dev/null |egrep -A 1 "title"|egrep -v "title"|cut -b 44-|cut -d '"' -f 1|egrep -v ^$)"

  # Just make a notify when a new song is played 
  if [ "$title_playing" != "$title_compare" ];then
    album="$(dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata' 2>/dev/null |egrep -A 2 "album"|egrep -v "album"|egrep -v "array"|cut -b 44-|cut -d '"' -f 1|egrep -v ^$)"

    artist="$(dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata' 2>/dev/null |egrep -A 2 "artist"|egrep -v "artist"|egrep -v "array"|cut -b 27-|cut -d '"' -f 1|egrep -v ^$)"

    title_playing="$(dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata' 2>/dev/null |egrep -A 1 "title"|egrep -v "title"|cut -b 44-|cut -d '"' -f 1|egrep -v ^$)"
    
      # Checks that the artist is not empty and that the title doesn't start with Spotify.
      if [ -n "$artist" ]; then 
        if [[ $title_playing != Spotify* ]]; then
          notify-send "$artist" "Album: $album
Title: $title_playing"
        fi
     fi  
  fi
# How often in seconds it will update
 sleep 5
done
echo "Ended"


       
 

Inga kommentarer: