Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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
- Run blockify 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 commecial 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
- To make this work you have to start wine/spotify with padsp. This is an example of how to do this:
- env WINEPREFIX="/home/xxxx/.wine" padsp wine /home/xxxx/.wine/drive_c/Program\ Files/Spotify/spotify.exe
- 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, pdb
- PATTERNFILE = os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), "blockPatterns.txt")
- def GetNamePartOfWindowString(line):
- title = line.split(" ", 7)[4:7]
- firstFourWordsOfTitle = " ".join(title)
- return firstFourWordsOfTitle
- 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 == "\n"):
- continue
- patternList.append(pattern)
- patternFile.close()
- return patternList
- def signalHandler(signum, frame):
- print 'Caught signal'
- AddWindowTitleToPatternList()
- signal.signal(signal.SIGUSR1, signalHandler)
- def AddWindowTitleToPatternList():
- global patternList
- pipe = GetWindowsPipe()
- for line in PipeReadlines(pipe):
- if (line.find("Spotify -") >= 0):
- windowTitle = GetNamePartOfWindowString(line)
- patternList.append(windowTitle)
- AddPatternToFile(windowTitle)
- break
- def PipeReadlines(pipe):
- try:
- return pipe.readlines()
- except:
- return ""
- #finally:
- # return ""
- def AddPatternToFile(newPattern):
- patternFile = file(PATTERNFILE, "a+")
- patternList = []
- for existingPattern in patternFile:
- patternList.append(existingPattern)
- if (patternList.count(newPattern) == 0):
- patternFile.write(newPattern + "\n")
- patternFile.close()
- def LowerVolume():
- subprocess.Popen("pacmd set-sink-mute 1 1 > /dev/null", shell=True)
- global volumeIsLow
- volumeIsLow = True
- def RaiseVolume():
- subprocess.Popen("pacmd set-sink-mute 1 0 > /dev/null", shell=True)
- global volumeIsLow
- volumeIsLow = False
- def GetWindows():
- pipe = GetWindowsPipe()
- separator = ""
- return separator.join(PipeReadlines(pipe))
- def Run():
- print 'Starting blockify.'
- if (not os.path.exists(PATTERNFILE)):
- open(PATTERNFILE, 'w').close()
- global volumeIsLow
- volumeIsLow = False
- RaiseVolume()
- global patternList
- patternList = GetBlockPatternList()
- while(True):
- found = False
- windows = GetWindows()
- for pattern in patternList:
- if (windows.find(pattern.strip('\n')) >= 0):
- found = True
- break
- if found:
- if (not volumeIsLow):
- LowerVolume()
- print 'Lowering volume'
- elif volumeIsLow:
- RaiseVolume()
- print 'Raising volume'
- time.sleep(1)
- if __name__ == "__main__":
- Run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement