-
-
Save georgkreimer/2930809 to your computer and use it in GitHub Desktop.
Mute Spotify ads on OS X
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
# coding: utf-8 | |
#!/usr/bin/env ruby | |
# | |
# Mutes Spotify ads by monitoring Growl's output in syslog, and changing the | |
# system-wide audio output to dummy output when an ad is detected. | |
# | |
# == Installation | |
# | |
# 1. Install Soundflower (http://code.google.com/p/soundflower/) | |
# 2. download gist | |
# 3. ruby spotify_mute_ads.rb | |
# 4. (if "soundflower not installed" error -> reboot! | |
# | |
# NOTE: Everything except installation of Soundflower is done automatically, | |
# but I've left all the steps here for the sake of clarity. | |
# All you have to do is install Soundflower and run this script. | |
# | |
# Requires Soundflower and SwitchAudioSource utility for switching audio to dummy output | |
# http://code.google.com/p/soundflower/ | |
# http://switchaudio-osx.googlecode.com/files/SwitchAudioSource-v1.0.zip | |
# | |
# Make Growl log to syslog: | |
# | |
# defaults write com.Growl.GrowlHelperApp GrowlLoggingEnabled -int 1 | |
# sudo killall GrowlHelperApp | |
# | |
# Restart GrowlHelperApp with one of the following: | |
# | |
# open ~/Library/PreferencePanes/Growl.prefPane/Contents/Resources/GrowlHelperApp.app | |
# open /Library/PreferencePanes/Growl.prefPane/Contents/Resources/GrowlHelperApp.app | |
# | |
# Run the script. | |
# | |
PATH_TO_AUDIO_SWITCHER = "#{ENV['HOME']}/SwitchAudioSource" | |
NORMAL_OUTPUT_DEVICE = "Built-in Output" | |
DUMMY_OUTPUT_DEVICE = "Soundflower (2ch)" | |
AD_MATCHERS = ["spotify:track:", "spotify:album:", /https?:\/\//] | |
def sh cmd | |
puts cmd; system cmd | |
end | |
### | |
# Setup | |
# | |
unless File.exist? PATH_TO_AUDIO_SWITCHER | |
puts "SwitchAudioSource not found at #{PATH_TO_AUDIO_SWITCHER}, fetching and unzipping…" | |
sh 'curl -o /tmp/sas.zip http://switchaudio-osx.googlecode.com/files/SwitchAudioSource-v1.0.zip' | |
sh 'cd /tmp; unzip -o sas.zip; mv SwitchAudioSource ~/SwitchAudioSource' | |
end | |
growl_logging = %x[defaults read com.Growl.GrowlHelperApp GrowlLoggingEnabled].to_i | |
if growl_logging.zero? | |
sh 'defaults write com.Growl.GrowlHelperApp GrowlLoggingEnabled -int 1' | |
sh 'sudo killall GrowlHelperApp' | |
sh 'open ~/Library/PreferencePanes/Growl.prefPane/Contents/Resources/GrowlHelperApp.app' | |
sh 'open /Library/PreferencePanes/Growl.prefPane/Contents/Resources/GrowlHelperApp.app' | |
end | |
unless `#{PATH_TO_AUDIO_SWITCHER} -a` =~ /Soundflower \(2ch\)/ | |
sh "#{PATH_TO_AUDIO_SWITCHER} -a" | |
puts | |
puts "Soundflower output device not found. Is Soundflower installed?" | |
exit | |
end | |
### | |
def set_output_device(device) | |
`#{PATH_TO_AUDIO_SWITCHER} -t output -s '#{device}'` | |
end | |
def is_ad?(string) | |
string =~ Regexp.union(*AD_MATCHERS) | |
end | |
def spotify_log | |
IO.popen("syslog -F '$(Message)' -w 1 -E none -d") do |f| | |
while line = f.gets | |
# Spotify GrowlHelper messages are split in two lines | |
if line =~ /^Spotify:/ | |
buffer = line.tr("\n", ' ') | |
else | |
if buffer | |
buffer << line.chomp | |
yield(buffer) | |
buffer = nil | |
end | |
end | |
end | |
end | |
end | |
spotify_log do |line| | |
puts line | |
if is_ad? line | |
set_output_device(DUMMY_OUTPUT_DEVICE) | |
else | |
set_output_device(NORMAL_OUTPUT_DEVICE) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment