Advertisement
Guest User

Untitled

a guest
Oct 27th, 2009
2,409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.60 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. Save this text to a file named "blockify.py", make it executable with the command:
  11.  
  12. chmod +x blockify.py
  13.  
  14. ...and run it using the command:
  15.  
  16. ./blockify.py
  17.  
  18. 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.
  19.  
  20. 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):
  21.  
  22. kill -USR1 $(pgrep -f blockify.py)
  23.  
  24. In Ubuntu you can bind keyboard shortcuts at system > preferences > keyboard shortcuts
  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
  30.  
  31. PATTERNFILE = os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), "blockPatterns.txt")
  32.  
  33. def GetNamePartOfWindowString(line):
  34.     return line.split(" ", 4)[4]
  35.  
  36. def GetWindowsPipe():
  37.     pipe = subprocess.Popen("wmctrl -l", shell=True, stdout=subprocess.PIPE).stdout
  38.     return pipe
  39.  
  40. def GetBlockPatternList():
  41.     patternFile = file(PATTERNFILE, "r")
  42.     patternList = []
  43.     for pattern in patternFile:
  44.         if (pattern == "\n"):
  45.             continue
  46.         patternList.append(pattern)
  47.     patternFile.close()
  48.    
  49.     return patternList
  50.  
  51. def signalHandler(signum, frame):
  52.     AddWindowTitleToPatternList()
  53.  
  54. signal.signal(signal.SIGUSR1, signalHandler)
  55.  
  56. def AddWindowTitleToPatternList():
  57.     global patternList
  58.     pipe = GetWindowsPipe()
  59.    
  60.     for line in pipe.readlines():
  61.         if (line.find("Spotify -") >= 0):
  62.             windowTitle = GetNamePartOfWindowString(line)
  63.             patternList.append(windowTitle)
  64.             AddPatternToFile(windowTitle + "\n")
  65.             return
  66.        
  67. def AddPatternToFile(newPattern):
  68.     patternFile = file(PATTERNFILE, "a+")
  69.     patternList = []
  70.    
  71.     for existingPattern in patternFile:
  72.         patternList.append(existingPattern)
  73.        
  74.     if (patternList.count(newPattern) == 0):
  75.         patternFile.write(newPattern)
  76.        
  77.     patternFile.close()
  78.            
  79. def LowerVolume():
  80.     subprocess.Popen("pacmd \"set-sink-mute 0 1\" > /dev/null", shell=True)
  81.     global volumeIsLow
  82.     volumeIsLow = True
  83.    
  84. def RaiseVolume():
  85.     subprocess.Popen("pacmd \"set-sink-mute 0 0\" > /dev/null", shell=True)
  86.     global volumeIsLow
  87.     volumeIsLow = False
  88.  
  89. def GetWindows():
  90.     pipe = GetWindowsPipe()
  91.     separator = ""
  92.     windowTitles = separator.join(pipe.readlines())
  93.     return windowTitles
  94.  
  95. if (not os.path.exists(PATTERNFILE)):
  96.     open(PATTERNFILE, 'w').close()
  97.        
  98. volumeIsLow = False
  99. RaiseVolume()
  100. patternList = GetBlockPatternList()
  101.  
  102. while(True):
  103.    
  104.     found = False
  105.     windows = GetWindows()
  106.    
  107.     for pattern in patternList:
  108.         if (windows.find(pattern.strip('\n')) >= 0):
  109.             found = True
  110.             break
  111.        
  112.     if found:
  113.         if (not volumeIsLow):
  114.             LowerVolume()
  115.     elif volumeIsLow:
  116.         RaiseVolume()
  117.            
  118.     time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement