Expand Cut Tags

No cut tags
topaz: (2632)
[personal profile] topaz
And that is that it's so damn useful.  [livejournal.com profile] numignost already noted elsewhere how easily you can build up a complete app using off-the-shelf Ruby components, but I just had occasion to rediscover it for myself.

I have a little over 200 CDs that I've ripped to MP3.  I wanted to get cover art for these CDs so that my iPod will display it when I'm listening to the song, because I'm dorky that way.  [livejournal.com profile] fraterrisus wrote a nifty Perl script that walks his MP3 directory tree, searches Amazon for each album, and downloads the album image for you.  Clever!  "It requires the Music::Tag and Music::Tag::Amazon modules," he warned me.

Oh boy.

Installing Perl modules means navigating a sea of ever-shifting, interlocking dependencies known as "CPAN", the "Comprehensive Perl Archive Network."  When you install a module from CPAN, it checks to see if it depends on any other modules you don't already have, and depends on and offers to install them.  On several occasions, CPAN has flatly refused to continue without upgrading my Perl interpreter, something which it then proceeds to do improperly and often requires that I reinstall from scratch.  When I install a package that requires me to install something from CPAN, I usually think long and hard about whether it would be easier to get along without it.

Debian and Ubuntu have done a magnificent job of fixing this by turning just about every CPAN module into a Debian package.  The Debian package system has a much more robust set of tools for managing dependencies than CPAN, so installing software that way is almost completely painless.

Unfortunately for me, Music::Tag and Music::Tag::Amazon are not among those.  After a few hours of installing Digest::SHA1 and Options::Config and Cache::Cache (yes, really) and Log::Log4Perl and I really can't remember what-all else, still to no avail, I punted.

Somewhere along the line I noticed that someone had written a Ruby/AWS package for querying Amazon Web Services in Ruby.  Out of curiosity I looked and found a libmp3info-ruby as well.

Hmm.

After registering for an Amazon Developer ID, I spent an hour or so fiddling and came up with this:

#! /usr/bin/env ruby

require 'amazon/aws'
require 'amazon/aws/search'
require 'net/http'
require 'uri'
require 'mp3info'

include Amazon::AWS
include Amazon::AWS::Search

AMZN_DEV_ID = "blahblahblahblah";

req = Request.new(AMZN_DEV_ID);
req.locale = 'us'

# Get a song file that tells us which album & artist we're looking at
mp3dir = ARGV.shift || '.'
songfile = Dir[mp3dir + '/*.mp3'].first
Mp3Info.open(songfile) { |mp3|
  title = mp3.tag.title
  artist = mp3.tag.artist

  # Create an item search object.
  is = ItemSearch.new( 'Music', { 'Artist' => artist, 'Title' => title } )
  rg = ResponseGroup.new( 'Medium' )

  resp = req.search( is, rg )

  items = resp.item_sets.items

  # Fetch the cover art.
  img_uri = items.first.large_image.url
  img = Net::HTTP.get(URI.parse(img_uri))

  img_filename = mp3dir + "/folder.jpg"
  File.open(img_filename, "w") { |f|
    f.write(img)
  }
}
There it is.  I didn't have to install eighteen packages held together by a slender gossamer framework of dependencies.  Ruby's core library includes all the tools you need to parse XML and deliver webservices requests without praying to the CPAN god.  Score one for the Rubyists.

There are other things that I appreciate about the language itself and not just what the community has done with it, but I'll get back to that some other time.

Date: 2008-04-06 03:52 am (UTC)
From: [identity profile] rwx.livejournal.com
yeah, there's a lot to be said to not have to do a breadth-first traversal of CPAN to get your thumb out of your ass for things that aren't NVT-based.

Date: 2008-04-06 04:17 am (UTC)
ext_86356: (frowny)
From: [identity profile] qwrrty.livejournal.com
You have acronymed me out. What in the name of Frith is NVT?

Date: 2008-04-06 04:43 am (UTC)
From: [identity profile] rwx.livejournal.com
http://www.microsoft.com/technet/prodtechnol/windows2000serv/reskit/intwork/incd_uni_drzi.mspx?mfr=true

The Network Virtual Terminal. Basically the pretend terminal that http, smtp, and etc... use.

(Basically, applications that you can telnet into the port and type things in rational fashions are nvt applications.)

May 2018

S M T W T F S
  12345
6789101112
13141516171819
20212223242526
27282930 31  

Most Popular Tags

Style Credit

Page generated Mar. 5th, 2026 06:29 am
Powered by Dreamwidth Studios