Skip to content

Instantly share code, notes, and snippets.

@anupdhml
Created November 21, 2013 04:08
Show Gist options
  • Save anupdhml/7575942 to your computer and use it in GitHub Desktop.
Save anupdhml/7575942 to your computer and use it in GitHub Desktop.
A script to start spotify with ad-mute, notification and auto storage-clean features
#!/bin/bash
# A script to start spotify with ad-mute, notification and auto storage-clean features
# By <anupdhml@gmail.com>
# Ad mute based on
# http://pcworldsoftware.kilu.net/files/link_spotify-admute.php
# https://gist.github.com/pcworld/3198763/#comment-857143
# Notification function depends on https://code.google.com/p/spotify-notify/
# Place the script in your ~/bin. Or modify script accordingly.
# spotify binary should be present in your $PATH. If not, uncomment this line and
# link the binary to the script dir. Also change the line at the end
#script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # http://stackoverflow.com/a/246128
was_paused=0 # if last state was paused
ad=0 # if an ad is probably playing right now
storage_dir="${HOME}/.cache/spotify/Storage/" # dir to clean periodically
clean_interval=3600
##############################################################################
function mute_ads () {
while true; do
num=$(pactl list | grep -E '(^Sink Input)|(media.name = \"Spotify\"$)' | awk '/Spotify/ {print a} {a = $0}' | cut -c 13-)
#icon_name=$(xprop -name 'Spotify - Linux Preview' _NET_WM_ICON_NAME | cut -d \" -f 2)
icon_name=$(xprop -name 'Spotify - Linux Preview' WM_ICON_NAME | awk -F"– " '{print $2}' | cut -d \" -f 1)
song_name=$(dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify / org.freedesktop.MediaPlayer2.GetMetadata | grep xesam:title -A 1 | grep variant | cut -d \" -f 2)
if [[ "$song_name" && "$icon_name" =~ "$song_name" ]]; then
if [ "$was_paused" -eq 0 -a "$ad" -ne 0 ]; then
# wait 1 second after a commercial
sleep 1
fi
ad=0
pactl set-sink-input-mute $num no
else
ad=1
pactl set-sink-input-mute $num yes
fi
#if [ "$icon_name" = "Spotify" ]; then
if [ -z "$icon_name" ]; then
was_paused=1
else
was_paused=0
fi
sleep 0.5
done
}
function auto_clean () {
while true; do
sleep $clean_interval
rm -r ${storage_dir}
done
}
##############################################################################
# Extra features
mute_ads &
auto_clean &
~/bin/spotify-notify.py -s -m & # ignore multimedia keys too
#${script_dir}/spotify "$@" &
spotify "$@" &
wait $! &&
test -z "`jobs -p`" || kill `jobs -p` # http://stackoverflow.com/a/2618497
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment