Advertisement
Guest User

Untitled

a guest
Feb 20th, 2011
8,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.18 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. """
  4. Blockify -- A simple program to mute the sound when a commercial is playing on Spotify.
  5.  
  6. The program depends on wmctrl and pulseaudio-utils. In Ubuntu and derivatives you can install them with the command:
  7.  
  8. sudo apt-get install wmctrl pulseaudio-utils
  9.  
  10. Run blockify using the command:
  11.  
  12. ./blockify.py
  13.  
  14. 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.
  15.  
  16. 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):
  17.  
  18. kill -USR1 $(pgrep -f blockify.py)
  19.  
  20. In Ubuntu you can bind keyboard shortcuts at system > preferences > keyboard shortcuts
  21.  
  22. To make this work you have to start wine/spotify with padsp. This is an example of how to do this:
  23.  
  24. env WINEPREFIX="/home/xxxx/.wine" padsp wine /home/xxxx/.wine/drive_c/Program\ Files/Spotify/spotify.exe
  25.  
  26. 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.
  27. """
  28.  
  29. import subprocess, time, signal, os, sys, pdb
  30.  
  31. PATTERNFILE = os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), "blockPatterns.txt")
  32.  
  33. def GetNamePartOfWindowString(line):
  34.     title = line.split(" ", 7)[4:7]
  35.     firstFourWordsOfTitle = " ".join(title)
  36.     return firstFourWordsOfTitle    
  37.  
  38. def GetWindowsPipe():
  39.     pipe = subprocess.Popen("wmctrl -l", shell=True, stdout=subprocess.PIPE).stdout
  40.     return pipe
  41.  
  42. def GetBlockPatternList():
  43.     patternFile = file(PATTERNFILE, "r")
  44.     patternList = []
  45.     for pattern in patternFile:
  46.         if (pattern == "\n"):
  47.             continue
  48.         patternList.append(pattern)
  49.     patternFile.close()
  50.    
  51.     return patternList
  52.  
  53. def signalHandler(signum, frame):
  54.     print 'Caught signal'
  55.     AddWindowTitleToPatternList()
  56.  
  57. signal.signal(signal.SIGUSR1, signalHandler)
  58.  
  59. def AddWindowTitleToPatternList():
  60.     global patternList
  61.     pipe = GetWindowsPipe()
  62.        
  63.     for line in PipeReadlines(pipe):
  64.         if (line.find("Spotify -") >= 0):
  65.             windowTitle = GetNamePartOfWindowString(line)
  66.             patternList.append(windowTitle)
  67.             AddPatternToFile(windowTitle)
  68.             break
  69.  
  70. def PipeReadlines(pipe):
  71.     try:
  72.         return pipe.readlines()
  73.     except:
  74.         return ""
  75. #finally:
  76. # return ""
  77.  
  78. def AddPatternToFile(newPattern):
  79.     patternFile = file(PATTERNFILE, "a+")
  80.     patternList = []
  81.    
  82.     for existingPattern in patternFile:
  83.         patternList.append(existingPattern)
  84.        
  85.     if (patternList.count(newPattern) == 0):
  86.         patternFile.write(newPattern + "\n")
  87.    
  88.     patternFile.close()
  89.  
  90. def LowerVolume():
  91.     subprocess.Popen("pacmd set-sink-mute 1 1 > /dev/null", shell=True)
  92.     global volumeIsLow
  93.     volumeIsLow = True
  94.  
  95. def RaiseVolume():
  96.     subprocess.Popen("pacmd set-sink-mute 1 0 > /dev/null", shell=True)
  97.     global volumeIsLow
  98.     volumeIsLow = False
  99.  
  100. def GetWindows():
  101.     pipe = GetWindowsPipe()
  102.     separator = ""
  103.     return separator.join(PipeReadlines(pipe))
  104.  
  105. def Run():
  106.     print 'Starting blockify.'
  107.     if (not os.path.exists(PATTERNFILE)):
  108.         open(PATTERNFILE, 'w').close()
  109.    
  110.     global volumeIsLow    
  111.     volumeIsLow = False
  112.     RaiseVolume()
  113.     global patternList
  114.     patternList = GetBlockPatternList()
  115.    
  116.     while(True):
  117.        
  118.         found = False
  119.         windows = GetWindows()
  120.                
  121.         for pattern in patternList:
  122.             if (windows.find(pattern.strip('\n')) >= 0):
  123.                 found = True
  124.                 break
  125.            
  126.         if found:
  127.             if (not volumeIsLow):
  128.                 LowerVolume()
  129.                 print 'Lowering volume'
  130.         elif volumeIsLow:
  131.             RaiseVolume()
  132.             print 'Raising volume'
  133.                
  134.         time.sleep(1)
  135.  
  136. if __name__ == "__main__":
  137.     Run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement