I've been writing Rails test code for what feels like the last century. Unit tests, functional tests, integration tests...ick! I finally managed to get it up to about 1.3 lines of test code to every line of production code. Now that I've hit that point, I figured a change in coding behavior was necessary.

For quite some time, I've been using my own AppleScript code to do a HTTP POST to a certain web service on my site. Yeah, I guess this worked, but it wasn't quite as slick as I would have liked. There were a few plugins available, but nothing to useful. Now Playing is a useful iTunes plugin written by Brandon Fuller and was previously only available for the Windows platform. It provides a number of features, one of which was what I mentioned above, however, until now, it hasn't been available for OSX. I guess timing is everything though, I hadn't checked his website in ages and since I was on this new mission to get some of the music section of the site underway, I figured I'd see what he'd been up to over the past months. To my pleasant surprise, Brandon wrote a version of Now Playing for OSX. You can download it here.

So with a nice little integrated iTunes plugin, I figured I'd see what I could get accomplished in-between Prison Break, Grey's Anatomy, and 24 tonight. I created the basic structure for all the tables in the database that will hold music information. I would bother you with the petty details, but darn there are quite a few tables...call me a stickler for normalization. After I nailed must of the tables down, I got the plugin working fairly quickly and Bada-bing, meta-information about the music I was playing in iTunes was being populated in the database Tony Soprano style.

The next, and perhaps most difficult, part of my adventure tonight was deciding how to work with Amazon Web Services. Should I use a more heavyweight SOAP protocol, or simply things with the welterweight REST protcol? I tried both and decided to go with REST for now, I can always change my mind later I guess. Since I had such a difficult time finding examples and random information on how to use REST as a client rather than a server, I thought I'd include a small snippit of code below in case you might find it useful somewhere down the road.

def amazon_it(album_name)
    # Search parameters
    subscription_id = <subscription id>
    service = "AWSECommerceService"
    operation = "ItemSearch"
    responseGroup = 'Images'
    searchTerm = album_name.to_s
    searchIndex = "Music"
    url = URI.parse("http://webservices.amazon.com/onca/xml?" + {"Service" => service, "Operation" => operation, "SubscriptionId" => subscription_id, "ResponseGroup" => responseGroup, "Keywords" => searchTerm, "SearchIndex" => searchIndex}.to_a.collect {|item| item.first + "=" + CGI::escape(item.last) }.join("&"))
    xml = Document.new( Net::HTTP.get(url) )
    if !XPath.first( xml, "//Item/LargeImage/URL").nil?
        @image_URL = XPath.first(xml, "//Item/LargeImage/URL").text
        save_album_art(@image_URL)
    elsif !XPath.first( xml, "//Item/MediumImage/URL").nil?
        @image_URL = XPath.first(xml, "//Item/MediumImage/URL").text
        save_album_art(@image_URL)
    elsif !XPath.first( xml, "//Item/SmallImage/URL").nil?
        @image_URL = XPath.first(xml, "//Item/SmallImage/URL").text
        save_album_art(@image_URL)
    else
        # do something
    end
    render(:nothing => true)
end

All in all, it wasn't too bad. Didn't have much luck on the #rubyonrails IRC channel this time, but it was a quiet night in there. Nevertheless, I managed to get most of this code functioning and at this point in time, when I play a song in iTunes, the album art is saved to file. So far, so good.

Current Rating: 5.0 rating from 1 vote

  • Current rating is 5
  •  
  •  
  •  
  •  
  •  

No Responses to "Music Development"

Comments are Closed

Name: (Required)
Website:
Comment:
Remember my info