public
Description: Scrobble Vinyl by packet sniffing iPhone shazam requests
Homepage: http://skillsmatter.com/podcast/ruby-on-rails/packet-sniffing
Clone URL: git://github.com/james/captor.git
captor / sniff.rb
100644 22 lines (19 sloc) 0.731 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env ruby
require 'pcaplet'
require './lastfm'
 
# create a sniffer that grabs the first 1500 bytes of each packet
$network = Pcaplet.new('-s 1500')
 
# create a filter that uses our query string and the sniffer we just made
# ip = `dig +short clarus.shazamid.com`
# $filter = Pcap::Filter.new('tcp and dst port 80', $network.capture)
# $network.add_filter($filter)
 
for p in $network
  # if $filter =~ p
    if p && p.respond_to?(:tcp_data) && p.tcp_data && (title = p.tcp_data.match(/.+<ttitle>(.+?)<\/ttitle>.+/)) && (artist = p.tcp_data.match(/.+<tartist id=".+">(.+?)<\/tartist>.+/))
      if title && artist
        print "#{artist[1]}: #{title[1]}: "
        submit(artist[1], title[1])
      end
    end
  # end
end