This module retrieves "Similar Artists" information from audioscrobbler.net and caches it for a specified amount of time, to speed up things and prevent hammering the source server.
I'd like to know if the code could be optimized (of course, we're using Perl, damnit!) and whether the chosen namespace would be ok for this module.
package Audio::Scrobbler::SimilarArtists;
use strict;
use Cache::File;
use Carp;
use File::Path qw/mkpath/;
use LWP::Simple;
use URI::Escape;
use XML::Simple;
our $VERSION = '0.01';
sub new {
my ($class, %parameters) = @_;
my $self = bless ({}, ref ($class) || $class);
my %options = (
min_match => 75,
cache_time => '1 week',
cache_dir => '/tmp/audioscrobbler.cache',
%parameters,
);
$self->{'_options'} = \%options;
# Check if the cache_dir exists
if(!-d $self->{'_options'}->{'cache_dir'}) {
eval { mkpath($self->{'_options'}->{'cache_dir'}) };
if($@) {
Carp::croak("Couldn't create $self->{'_options'}->{'cache_dir'}:$@");
}
}
return $self;
}
sub lookup {
my ($self, $name) = @_;
unless($name) {
Carp::croak('No name supplied!');
return;
}
my $cache = new Cache::File (
cache_root => $self->{'_options'}->{'cache_dir'},
);
my $filename = $name;
$filename =~ s/\W//g;
my @info;
@info = @{$cache->thaw($filename)}
if(ref $cache->thaw($filename) eq 'ARRAY');
unless(@info) {
my $data = get(sprintf("%s/%s/%s",
'http://ws.audioscrobbler.com/1.0/artist',
uri_escape($name),
'similar.xml')
);
if($data) {
my $xs = new XML::Simple();
my $x = $xs->XMLin($data);
if(ref $x->{'artist'} eq 'ARRAY') {
foreach my $item (@{$x->{'artist'}}) {
next unless ref $item eq 'HASH';
push @info, $item
if($item->{'match'} >= $self->{'_options'}->{'min_match'});
}
$cache->freeze($filename, \@info,
$self->{'_options'}->{'cache_time'});
}
} else {
Carp::carp("Couldn't fetch XML for $name");
return ();
}
}
return @info;
}
I've taken out the POD, for this post is long enough already ;-)
You're doing too much work, when you can just use [cpan://Memoize] and take advantage of the [cpan://Memoize::Storable] capability.
package Audio::Scrobbler::SimilarArtists;
use strict;
use Memoize;
my %cache;
sub new {
my ($class, %parameters) = @_;
my $self = bless ({}, ref ($class) || $class);
my %options = (
min_match => 75,
cache_time => '1 week',
cache_file => '/tmp/audioscrobbler.cache',
%parameters,
);
$self->{'_options'} = \%options;
$self->{cache} = \%cache;
tie %{ $self->{cache} } =>
'GDBM_File', $self->{_options}->{cache_file}, O_RDWR|O_CREAT, 0666;
memoize 'lookup', SCALAR_CACHE => [HASH => \%cache];
return $self;
}
sub lookup {
# same as above, but without caching code: memoize handles that.
}
perlmonks.org content © perlmonks.org and b10m, radiantmatrix
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03