Last active
January 4, 2016 19:49
-
-
Save ivanalejandro0/8669459 to your computer and use it in GitHub Desktop.
Script that monitors Spotify and mute the system volume when an ad is played.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Problem: if there is an ad you will be muted, no matter of what. You need to | |
# stop this script to avoid that. | |
set -euo pipefail | |
IFS=$'\n\t' | |
source volume.sh | |
get_systray_title(){ | |
# get the systray icon's text. | |
# echo `xprop -name "Spotify - Linux Preview" 2>&1 | grep "_NET_WM_ICON_NAME(UTF8_STRING) = " | cut -d\" -f2` | |
echo `xprop -name "Spotify Free - Linux Preview" 2>&1 | grep "_NET_WM_ICON_NAME(UTF8_STRING) = " | cut -d\" -f2` | |
} | |
is_ad(){ | |
# TODO: maybe use 'Spotify -' as a prefix? | |
# blacklisted prefixes: | |
prefixes=( | |
'Spotify - 20th Century Fox' | |
'Spotify - Alcatel' | |
'Spotify - AXE' | |
'Spotify - Banco Supervielle' | |
'Spotify - Beat Batten' | |
'Spotify - Compumundo' | |
'Spotify - Corona' | |
'Spotify - Disney' | |
'Spotify - Heineken' | |
'Spotify - Movistar' | |
'Spotify - Pedidos Ya' | |
'Spotify - Personal' | |
'Spotify - Quilmes' | |
'Spotify - SUPERVIELLE' | |
'Spotify - Sennheiser' | |
'Spotify - Sin Control' | |
'Spotify - Sony Music' | |
'Spotify - Spotify' | |
'Spotify - TNT' | |
'Spotify - Tuenti' | |
'Spotify - Universal Music' | |
'Spotify - Verano 2015' | |
'Spotify - Warner' | |
'Spotify - Warner Music' | |
) | |
title=`get_systray_title` | |
# check if it is a new ad and start with known prefixes | |
for p in "${prefixes[@]}"; do | |
if [[ $title = ${p}* ]]; then | |
echo true | |
return | |
fi | |
done | |
echo false | |
} | |
# Define escape code and colors | |
esc=`echo -en "\033"` | |
CC_GREEN="${esc}[0;32m" | |
CC_RED="${esc}[0;31m" | |
CC_NORMAL="${esc}[m" | |
# we cycle indefinitely since Spotify seems to have removed all actions | |
# notifying that a song has changed | |
SINK="" | |
while true; do | |
sleep 0.1 | |
if [[ `is_ad` == "true" ]]; then | |
if [[ `volume_is_muted` != "yes" ]]; then | |
[[ -n $SINK ]] && echo -n "Advertisement -> ${CC_RED}mute.${CC_NORMAL} waiting ad to finish... " | |
volume_mute | |
fi | |
continue # skip to the next iteration | |
fi | |
if [[ `volume_is_muted` == 'yes' ]]; then | |
echo "${CC_GREEN}unmute.${CC_NORMAL}" | |
volume_unmute | |
fi | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
_define_sink_to_use(){ | |
# NOTE: sometimes all sinks are 'suspended'... | |
# this may return more than one sink to deal with | |
# SINK=`pactl list short sinks | grep -E "RUNNING|IDLE|SUSPENDED" | cut -f1` | |
# SINK_NAME=`pactl list short sinks | grep -E "RUNNING|IDLE|SUSPENDED" | cut -f2` | |
SINK=`pactl list short sinks | grep -E "RUNNING|IDLE" | cut -f1` | |
SINK_NAME=`pactl list short sinks | grep -E "RUNNING|IDLE" | cut -f2` | |
if [[ -z $SINK ]]; then exit 1; fi; | |
} | |
volume_is_muted(){ | |
_define_sink_to_use | |
# echo `pacmd dump | awk ' | |
# $1 == "set-sink-mute" {m[$2] = $3} | |
# $1 == "set-default-sink" {s = $2} | |
# END {print m[s]}'` | |
pacmd dump | grep -i set-sink-mute | grep $SINK_NAME | cut -f3 -d' ' | |
} | |
volume_mute(){ | |
_define_sink_to_use | |
# pactl set-sink-mute $SINK 1 > /dev/null 2>&1 | |
pactl set-sink-mute $SINK 1 | |
} | |
volume_unmute(){ | |
_define_sink_to_use | |
pactl set-sink-mute $SINK 0 > /dev/null 2>&1 | |
} | |
volume_get(){ | |
_define_sink_to_use | |
echo `pactl list sinks | | |
grep -A5000 "Sink #$SINK" | | |
grep -m1 "Volume" | | |
grep -oE "[0-9]{2,3}%" | | |
head -n1` | |
} | |
volume_set(){ | |
if [[ -z $1 ]]; then | |
echo "Missing parameter, use '100%'." | |
exit | |
fi | |
_define_sink_to_use | |
# this looks like: pactl set-sink-volume 0 100% | |
pactl set-sink-volume $SINK $1 | |
} | |
help(){ | |
echo ">> Volume set/get helper for pulseaudio." | |
echo "Sets/gets the system volume." | |
echo | |
echo "Usage: $0 (set xx% | get )" | |
echo | |
echo " set : Sets the system volume to the given value. Note: it can be more than 100%." | |
echo " get : Gets the current system volume and echoes it to the screen." | |
echo " mute : Mute." | |
echo " unmute : Unmute, restore volume." | |
echo " ismuted : Checks whether the volume is muted or not." | |
echo " help : Show this help" | |
echo | |
} | |
# If the script is being sourced do not check for command/params | |
[[ "${BASH_SOURCE[0]}" != "${0}" ]] && return | |
case "$1" in | |
get) | |
volume_get | |
;; | |
set) | |
volume_set $2 | |
;; | |
ismuted) | |
volume_is_muted | |
;; | |
mute) | |
volume_mute | |
;; | |
unmute) | |
volume_unmute | |
;; | |
*) | |
help | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment