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

In reply to Song Info by alexbio

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.