Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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_ma +tch'}); } $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 ;-)

Update: but I do think the SYNOPSIS is the least I can show:

=head1 SYNOPSIS use strict; use Audio::Scrobbler::SimilarArtists; my $sa = Audio::Scrobbler::SimilarArtists->new( minmatch => 85, cache_time => '1 week', cache_dir => '/var/cache/audioscrobbler' ); my @artists = $sa->lookup('Hate Forest'); if(@artists) { print "<a href=\"$_->{'url'}\">$_->{'name'}</a> ". "(match: $_->{'match'})\n" foreach(@artists); }

Update2: maybe WebService::LastFM::SimilarArtists is a better name, since Audio:: might imply it actually does something with audio?

--
b10m

All code is usually tested, but rarely trusted.

In reply to Module RFC: Audio::Scrobbler::SimilarArtists by b10m

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2023-03-30 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (74 votes). Check out past polls.

    Notices?