Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Song Info

by alexbio (Monk)
on May 16, 2010 at 17:00 UTC ( #840237=CUFP: print w/ replies, xml ) Need Help??

Hi monks. I wrote a script that fetches information of a given song via Last.fm, and then prints it in a specified format.

Itís usage is very simple: it needs only the title and the artist of a song, and, optionally an username of a Last.fm account for adiotional information such as userís playcount.

It also takes a string which represents the output format. An example format string could be:

Title: {track}\nArtist: {artist}\nAlbum: {album}

A complete list of allowed variables could be found inside the documentation (perldoc).

Also check out the git repository.

#!/usr/bin/perl # Fetch song's info from last.fm. # # Copyright 2010 Alessandro Ghedini <al3xbio@gmail.com> # -------------------------------------------------------------- # "THE BEER-WARE LICENSE" (Revision 42): # Alessandro Ghedini wrote this file. As long as you retain this # notice you can do whatever you want with this stuff. If we # meet some day, and you think this stuff is worth it, you can # buy me a beer in return. # -------------------------------------------------------------- use HTTP::Request::Common; use LWP::UserAgent; use XML::Simple; use Encode::Escape; use strict; die("For info type 'perldoc $0'\n") unless $#ARGV > 0; my (%format_tags, $format, $user, $api_key, $track, $artist); $api_key = "b25b959554ed76058ac220b7b2e0a026"; for (my $i = 0; $i < $#ARGV + 1; $i++) { $format = $ARGV[$i+1] if ($ARGV[$i] eq "-f"); $user = $ARGV[$i+1] if ($ARGV[$i] eq "-u"); $api_key = $ARGV[$i+1] if ($ARGV[$i] eq "-k"); $track = $ARGV[$i+1] if ($ARGV[$i] eq "-t"); $artist = $ARGV[$i+1] if ($ARGV[$i] eq "-a"); print "For info type 'perldoc $0'\n" if ($ARGV[$i] eq "-h"); } my $api_root = "http://ws.audioscrobbler.com/2.0"; my $method = "track.getInfo"; my $ua = LWP::UserAgent -> new; my $q_method = "method=$method"; my $q_apikey = "api_key=$api_key"; my $q_artist = "artist=$artist"; my $q_track = "track=$track"; my $q_user = "username=$user"; my $api_url = "$api_root/?$q_method&$q_apikey&$q_artist&$q_track&$q_us +er"; my $response = $ua -> request(GET $api_url) -> as_string; my $body = (split /\n\n/, $response)[1]; my $xml_handler = XMLin($body); if ($xml_handler -> {error} -> {code}) { print "ERROR: ".$xml_handler -> {error} -> {content}."\n"; exit -1; } $format_tags{'track'} = $xml_handler -> {track} -> {name}; $format_tags{'artist'} = $xml_handler -> {track} -> {artist} -> +{name}; $format_tags{'album'} = $xml_handler -> {track} -> {album} -> {t +itle}; my $secs = $xml_handler -> {track} -> {duration} / 1000; $format_tags{'duration'} = (gmtime $secs)[1].":".(gmtime $secs)[0]; $format_tags{'toptags'} = ""; while (my $tag = each %{$xml_handler -> {track} -> {toptags} -> {tag}} +) { $format_tags{'toptags'} .= "$tag "; } $format_tags{'playcount'} = $xml_handler -> {track} -> {playcount}; $format_tags{'user_plays'} = $xml_handler -> {track} -> {userplaycount +}; $format_tags{'user_loved'} = $xml_handler -> {track} -> {userloved}; while (my $tag = each %format_tags) { $format =~ s/{$tag}/$format_tags{$tag}/; } print decode 'ascii-escape', $format; __END__ =head1 NAME SongInfo.pl - Fetch song's info from last.fm =head1 USAGE SongInfo [OPTIONS] =head1 OPTIONS =over =item -u Specifies the username, it is used for user's playcount. =item -t Track's title. =item -a Track's artist. =item -k API Key (optional). =item -f Format used to print song's info (see below). =back =head1 FORMAT The format variable is used to compose the output of SongInfo. The variables allowed are: =over =item B<{track}> Song's title =item B<{artist}> Song's artist =item B<{album}> Song's album =item B<{duration}> Song's lenght =item B<{toptags}> Song's most used tags (from Last.fm community). =item B<{playcount}> Song's total number of plays (from Last.fm community). =item B<{user_plays}> Song's number of plays by selected user (requires Last.fm account). =item B<{user_loved}> Return '1' if user loved the song, otherwise '0' (requires Last.fm acc +ount). =back You can also use standard escape characters such as: \n (newline), \t +(tab), \" (double quotes), \\ (backslash), etc... =head1 API KEY You can provide your own Last.fm API Key by setting the '-k' option, otherwise the default key is used. You can apply for an API Account on Last.fm: http://www.last.fm/api/account =cut
Alex's Log - http://alexlog.co.cc

Comment on Song Info
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://840237]
Approved by ww
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-12-25 07:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls