Automatically Query iTunes Shares 1

Posted by Aaron Patterson on August 24, 2006

Here’s a quick program I came up with to query iTunes shares as soon as they connect. It uses dnssd to query for iTunes over multicast dns, and Net::DAAP::Client (ruby iTunes client) to query the iTunes share. The program will wait until someone starts up iTunes and automatically queries the iTunes server.

require 'rubygems'
require 'dnssd'
require 'net/daap'

Thread.abort_on_exception = true

class ITunesResolver
  @@servers = {}
  def self.resolve(br, action = :add)
    Thread.new(br, action) { |br, action|
      DNSSD.resolve(br.name, br.type, br.domain) { |rr|
        tr = rr.text_record

        # Skip servers that have a password
        if ! tr.has_key? "Password" or tr["Password"] == "false"
          if action == :add
            @@servers[rr.target] = Thread.new(rr.target) { |t|

              # Connect to the server and list all songs
              daap = Net::DAAP::Client.new(t)
              daap.connect do |dsn|
                daap.databases do |db|
                  # Print song names
                  db.songs.sort.each { |s| puts s.name }
                end
              end # End DAAP Query
            }
          else
            (@@servers.delete rr.target).kill if @@servers.has_key? rr.target
          end
        end
        rr.service.stop
      }
    }
  end
end

browse_service = DNSSD.browse('_daap._tcp') do |browse_reply|
  if browse_reply.flags.add?
    ITunesResolver.resolve(browse_reply)
  else
    ITunesResolver.resolve(browse_reply, :del)
  end
end

$stdin.gets
browse_service.stop

It’s not perfect, but the bugs are left as an exercise for the reader to fix!

New Ruby iTunes Client

Posted by Aaron Patterson on July 09, 2006

I just released a new version of the ruby iTunes client, Net::DAAP::Client version 0.2.1. I also released Digest::M4P as a Gem and made daapclient depend on it. I wasn’t able to release Digest::M4P as a gem until RubyGems version 0.9.0 was released because of a weird bug. But now that 0.9.0 is here, everything works! Too bad the digest code still doesn’t work on windows…….

I need to release my pure ruby MD5 so that I can make Digest::M4P pure ruby and not have to worry about this windows stuff.

Ruby iTunes Client

Posted by Aaron Patterson on June 01, 2006

I’m trying to think of cool stuff for a Ruby presentation, so I thought I’d write up a quick tutorial for the Ruby iTunes client here to see how it sounds.

Introduction:
DAAP stands for “Digital Audio Access Protocol”, and is the protocol used by Apple’s iTunes to share music over a network. Hence the name Net::DAAP::Client.
Continue reading…