#!/usr/bin/python

"""

Blockify -- A simple program to mute the sound when a commercial is playing on Spotify.

The program depends on wmctrl and pulseaudio-utils. In Ubuntu and derivatives you can install them with the command:

sudo apt-get install wmctrl pulseaudio-utils

Save this text to a file named "blockify.py", make it executable with the command:

chmod +x blockify.py

...and run it using the command:

./blockify.py

The program works by repeatedly reading the titles of all windows and muting the sound if it detects certain phrases. These phrases are stored in the file "blockPatterns.txt" which should be in the same directory as blockify.py. This works since Spotify writes the name of the song playing to the window title, including commercials.

The following command sends a signal to blockify that makes it add the currently playing commercial to the block list (it would be a good idea to bind this to a key-combination for your system):

kill -USR1 $(pgrep -f blockify.py)

In Ubuntu you can bind keyboard shortcuts at system > preferences > keyboard shortcuts

This program was written very quickly and there are many potential improvements that could be made. You are welcome to make any changes and share the code as you see fit.

"""

import subprocess , time , signal , os , sys

PATTERNFILE = os . path . join ( os . path . abspath ( os . path . dirname ( sys . argv [ 0 ] ) ) , "blockPatterns.txt" )

def GetNamePartOfWindowString ( line ) :

return line. split ( " " , 4 ) [ 4 ]

def GetWindowsPipe ( ) :

pipe = subprocess . Popen ( "wmctrl -l" , shell = True , stdout = subprocess . PIPE ) . stdout

return pipe

def GetBlockPatternList ( ) :

patternFile = file ( PATTERNFILE , "r" )

patternList = [ ]

for pattern in patternFile:

if ( pattern == "

" ) :

continue

patternList. append ( pattern )

patternFile. close ( )

return patternList

def signalHandler ( signum , frame ) :

AddWindowTitleToPatternList ( )

signal . signal ( signal . SIGUSR1 , signalHandler )

def AddWindowTitleToPatternList ( ) :

global patternList

pipe = GetWindowsPipe ( )

for line in pipe. readlines ( ) :

if ( line. find ( "Spotify -" ) >= 0 ) :

windowTitle = GetNamePartOfWindowString ( line )

patternList. append ( windowTitle )

AddPatternToFile ( windowTitle + "

" )

return

def AddPatternToFile ( newPattern ) :

patternFile = file ( PATTERNFILE , "a+" )

patternList = [ ]

for existingPattern in patternFile:

patternList. append ( existingPattern )

if ( patternList. count ( newPattern ) == 0 ) :

patternFile. write ( newPattern )

patternFile. close ( )

def LowerVolume ( ) :

subprocess . Popen ( "pacmd \" set-sink-mute 0 1 \" > /dev/null" , shell = True )

global volumeIsLow

volumeIsLow = True

def RaiseVolume ( ) :

subprocess . Popen ( "pacmd \" set-sink-mute 0 0 \" > /dev/null" , shell = True )

global volumeIsLow

volumeIsLow = False

def GetWindows ( ) :

pipe = GetWindowsPipe ( )

separator = ""

windowTitles = separator. join ( pipe. readlines ( ) )

return windowTitles

if ( not os . path . exists ( PATTERNFILE ) ) :

open ( PATTERNFILE , 'w' ) . close ( )

volumeIsLow = False

RaiseVolume ( )

patternList = GetBlockPatternList ( )

while ( True ) :

found = False

windows = GetWindows ( )

for pattern in patternList:

if ( windows. find ( pattern. strip ( '

' ) ) >= 0 ) :

found = True

break

if found:

if ( not volumeIsLow ) :

LowerVolume ( )

elif volumeIsLow:

RaiseVolume ( )