Ruby iTunes Client

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.

Installation
The client is packaged as a gem, so installation is pretty straightforward. The only hitch is that digest-m4p needs to be installed first. I am currently working on a pure Ruby implementation of Digest::M4P, so hopefully this process will become a lot easier.

First, download Digest::M4P from here. Unzip the tar.gz file, run ruby on extconf.rb, then run 'make', and as root run 'make install' like this:

$ ruby extconf.rb
$ make
$ sudo make install

Once that is installed, just install the daapclient gem:

$ sudo gem install daapclient

Usage:
Connecting to iTunes is very simple. Just create a new Net::DAAP::Client, and call connect. If the connect method is given a block, after the block is executing the client will execute disconnect for you.

daap = Net::DAAP::Client.new('localhost')
daap.connect do |dsn|
...
end # disconnect is called here for us!

DAAP allows any server to contain multiple databases of music, however in practice iTunes servers I have encountered only have one database. Since a server could have multiple databases, we iterate over them:

daap.databases.each do |db|
...
end

Once we have a handle on the database, we can access the databases attributes, such as the songs, artists, and albums it contains:

db.songs.each { |song| puts song.name }
db.artists.each { |artist| puts artist.name }
db.albums.each { |album| puts album.name }

Putting it All Together:
Now we can see how each piece, connecting to the server, getting the databases, and listing information, fits together with a simple program to list all songs on an iTunes server:

daap = Net::DAAP::Client.new('localhost')
daap.connect do |dsn|
  daap.databases.each do |db|
    db.songs.each { |song| puts song.name }
  end
end

More Information
For more information, be sure to check out the examples in the RDoc here.

Post a Comment

Your email is never shared. Required fields are marked *

*
*
Check Spelling
Activate Spell Check while Typing