Skip to content

Instantly share code, notes, and snippets.

@dkordik
Created December 6, 2012 08:49
Show Gist options
  • Save dkordik/4222925 to your computer and use it in GitHub Desktop.
Save dkordik/4222925 to your computer and use it in GitHub Desktop.
Sniff network for iTunes #nowplaying mDNS announces via ngrep. Requires Ruby 1.9 for the awesome named-group regexes. Expand to do interesting things based on what you're playing.
match = 'nowplaying'
ngrep_command = "ngrep -W byline '#{match}' port 5353"
puts "Watching network for iTunes mDNS #nowplaying announces..."
IO.popen(ngrep_command).each do |line|
if line.match(/\##{match}/)
pattern = /nowplaying-title=(?<title>[\w ]+).+nowplaying-artist=(?<artist>[\w ]+).+nowplaying-album=(?<album>[\w ]+)/
nowplaying = pattern.match(line)
puts
puts "Artist: #{nowplaying[:artist]}"
puts "Album: #{nowplaying[:album]}"
puts "Title: #{nowplaying[:title]}"
end
end
@roykolak
Copy link

roykolak commented Dec 6, 2012

true story

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment