?

Log in

No account? Create an account

Wed, Apr. 20th, 2005, 01:00 pm
URI::Title::iTMS fixages

URI::Title::iTMS is the plugin for URI::Title that copes with Apple's iTunes Music Store URLs. The problem was that it relied on a function from Net::iTMS that was only present in versions less that 0.1, namely

    my $info = fetch_iTMS_info( $uri )
     or die "iTMS / Not an iTMS url";
    $title   =
        eval { "iTMS / " . join(" / ", map { $_->{name} } @{ $info->Path } ) }
        || "iTMS / Error getting title from $uri: $@";

so upgrading to the latest version would break URI::Title::iTMS. After dicking around for a bit I realised you could get the same info out of the new one by using a slightly more convoluted route,

    my $r    =  Net::iTMS::Request->new( );
    my $info = $r->url($uri);
    my $path = $info->root->first_child('Path');
    $title   = eval { "iTMS / " . join(" / ", map { $_->att('displayName') } 
            $path->children('PathElement') ) }
        || "iTMS / Error getting title from $uri: $@";

So it was easy to do a

    eval { require Net::iTMS::Request };
    if ($@) {
        # first version
        # ...
    } else {
        # second version
        # ...
    }

I also noticed iTMS would do internal redirects with itms:// as the scheme which would, naturally, break LWP so I also added

    $r->{_ua} = MyLWP->new;
    $r->{_ua}->agent('iTunes/4.2 (Macintosh; U; PPC Mac OS X 10.2)');

with MyLWP being ...

    package MyLWP;
    use base qw(LWP::UserAgent);
    sub prepare_request {
        my ($self, $request) = @_;
        my $uri = $request->uri;
        $uri->scheme('http');
        $request->uri($uri);
        return $request;
    }

And it all seemed to work. HUZZAH!