Tenderlove Making

Browsing iTunes Networks with Ruby

I’ve been working on my ruby DAAP Client lately, and I ran across a thread on the ruby-talk list about a multicast DNS client. Combining the two of those gave me some pretty neat information about the iTunes servers running on our network.

I could easliy get a list of iTunes servers using only mdns-sd like this: ~~~ ruby require ‘rubygems’ require ‘net/dns/mdns-sd’

DNSSD = Net::DNS::MDNSSD

Find all iTunes servers

browse_service = DNSSD.browse(‘_daap._tcp’) do |r| rservice = DNSSD.resolve(r.name, r.type, r.domain) do |res| puts res.target end end

$stdin.gets browse_service.stop ~~~

Once I was able to get a list of iTunes servers, listing all available artists was trivial. I just added the DAAP client in, and it worked:

require 'rubygems'
require 'net/daap'
require 'net/dns/mdns-sd'

DNSSD = Net::DNS::MDNSSD

# Find all iTunes servers
browse_service = DNSSD.browse('_daap._tcp') do |r|
  rservice = DNSSD.resolve(r.name, r.type, r.domain) do |res|
    daap = Net::DAAP::Client.new(res.target)
    daap.connect do |dsn|
      daap.databases.each { |db|
        db.artists.each { |a| puts "#{res.target} #{a.name}" }
      }
    end
  end
end

$stdin.gets
browse_service.stop

Combine that with the get method on Net::DAAP::Song, and you have a winning combo! A script that will automatically detect iTunes servers, and download the music. This will be great for backing up the music I keep on my Mac. I just plug it in to the network, and this script will automatically sync it up.

« go back