Hack 60 Expanding Your Musical Tastes

figs/moderate.gif figs/hack60.gif

Looking for new music to complement your stale collection? With this script, you'll be able to pass some names of your favorite artists , and get Audioscrobbler recommendations .

You've downloaded every album by your favorite artist, even the B-sides. Maybe your playlist of 3,000 songs is starting to get stale. For whatever reason, you've decided it is time to find new music to fall in love with. Downloading songs off Limewire (http://www.limewire.com) with "GET THIS" in the filename, only to find out it's the ramblings of a broken bagpipe, is hit or miss at best. Wouldn't it be great to see what other people, who tend to like the same music you do, are listening to?

Audioscrobbler (http://www.audioscrobbler.com) has a great solution: it accepts playlist information from its users about what they listen to and how often. From there, Audioscrobbler associates artists with each other based on how often users listen to them. We are going to use a script to access the Audioscrobbler web site and retrieve a list of artists with a correlation factorhow closely related that artist is to the artists you submit.

First of all, we need to find the traditional way this is done at the Audioscrobbler web site. A quick check of the site reveals a Related Artists link that takes you to a form where you can type in three artists and get a listing of matches. Since this is exactly what we need, let's take a look at the code that runs the form. Looking at the HTML source, you can see the HTML code for the form at the bottom of the code. It's a GET request with some predefined variables and our three input boxes, named a1 , a2 , and a3 . If you go back to the page, fill in some artists, and click Do It, you'll get a page with the results. Take a second to look at the URL for the page. The first thing to notice is that it is quite long; this is where the GET request parameter for the form comes in, because a GET request means that all the information for the page will be submitted within the URL. Using this knowledge, we can now construct our own URLs with the artists' names in them to retrieve the results.

Once we have the results, we need to figure out how to get the data we need. Back to the HTML source. This time, again near the bottom, we find a single, extremely long line of HTML. Searching through it, you can see there is a simple format: a link for the artist's name , td tags, and two img tags. We'll use the width attribute for the second img tag to find the correlation. It just so happens that the width value is always a number between 1 and 300 ; this value determines the length of the pretty image on the page to the right of each artist.

The Code

Save the following code to a file called audioscrobble.pl :

 #!/usr/bin/perl -w # # AudioScrobble - Finds artists similar to those you already like. # Comments, suggestions, contempt? Email adam@bregenzer.net. # # This code is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # use strict; $++; my $VERSION = "1.0"; # make sure we have the modules we need, else die peacefully. eval("use LWP 5.6.9;"); die "[err] LWP 5.6.9 or greater required.\n" if $@; # base URL for all requests my $base_url = "http://www.audioscrobbler.com/modules.php?".                "op=modload&name=top10&file=scrobblersets"; my $counter = 0;         # counter of artists displayed my $max_count = 10;      # maximum number of artists to display my ($a1, $a2, $a3) = ''; # artist input variables # Reminder: this code checks for arguments, therefore if a band # name has multiple words make sure you put it in quotes. # Also, Audioscrobbler accepts at most three band names so we # will only look at the first three arguments. $a1 = $ARGV[0]  die "No artists passed!\n"; $a2 = $ARGV[1]  ""; $a3 = $ARGV[2]  ""; # create a downloader, faking the User-Agent to get past filters. print "Retrieving data for your matches... "; my $ua = LWP::UserAgent->new(agent => 'Mozilla/4.76 [en] (Win98; U)'); my $data = $ua->get("$base_url&a1=$a1&a2=$a2&a3=$a3")->content; print "done.\n"; # print up a nice header. print "Correlation\tArtist\n"; print "-" x 76, "\n"; # match on the URL before the artist's name through to # the width of the bar image (to determine correlation). while ($counter < $max_count && $data =~ /href="modules\.php\  [RETURN]  ?op=modload&name=top10&file=artistinfo&artist=[^"]+">([^<]+)<\/a>[^<]+<\  [RETURN]  /td><td[^>]+><img[^>]+\/><img[^>]+width="([0-9]+)">(.*)/) {     # print the correlation factor and the artist's name.     printf "%1.2f", ( / 300); print "\t\t" .  . "\n";     # continue with the     # data that is left.     $data = ; $counter++; } if ($counter == 0) {print "No matches.\n";} print "-" x 76, "\n"; 

Running the Hack

Invoke the script on the command line, passing it up to three artists you like. Make sure you put their names in quotes; you do not need to worry about capitalization. Audioscrobbler cannot handle more than three artists at a time; in fact, you might find that three artists is too many for it (i.e., you might not get any results). In such cases, try removing the last artist and running it again. It is also important to keep in mind that you will get better results if you list artists that are similar to each other. This is a proximity search, so listing a heavy metal, country, and classical artist in the same search is unlikely to return any results.

Appropriately prepared, venture forth into the world of new music and find your next favorite artists. Here is an example in which I find artists similar to Aphex Twin and Autechre:

 %  perl audioscrobble.pl "Aphex Twin" "Autechre"  Retrieving data for your matches... done. Correlation     Artist -------------------------------------------------------------------------- 1.00            Boards Of Canada 1.00            Plaid 0.83            Underworld 0.83            Radiohead 0.83            Chemical Brothers 0.83            Orbital 0.67            Mu-Ziq 0.67            Led Zeppelin 0.67            AFX 0.67            Squarepusher 

Hacking the Hack

There are a few ways you can improve upon this hack.

Changing the number of results returned

You can easily change the number of results by changing the hardcoded $max_count value to a different number. However, we are looking for something more elegant. If you add the following code above the comment that starts with #Reminder , you will be able to pass an argument to the script specifying the number of results to return:

 # Check for a '-c' argument first # specifying the number of # results to return. if ($ARGV[0] =~ /-c/) {     shift @ARGV;     $max_count = shift @ARGV; } 

And here is the requisite sample output:

 %  perl audioscrobble.pl -c 5 "Aphex Twin" "Autechre"  Retrieving data...done. Correlation     Artist -------------------------------------------------------------------------- 1.00            Boards Of Canada 1.00            Plaid 0.83            Underworld 0.83            Radiohead 0.83            Chemical Brothers 

If you plan on adding a number of command-line arguments, you might want to use a Perl module designed for the job: Getopt::Long . You can find various examples of its use within other hacks in this book.

Looking up artists

Now that you have a list of new artists, the next step is to have the script research these new artists and download sample songs, customer ratings, and so on. Code away, young grasshopper.

See Also

  • There are other sites available that aggregate and associate artist playlists, the most promising of late being EchoCloud [Hack #20]. As opposed to Audioscrobbler, which is an opt-in service, EchoCloud works by spidering P2P networks, such as Soulseek, for relevant information.

Adam Bregenzer



Spidering Hacks
Spidering Hacks
ISBN: 0596005776
EAN: 2147483647
Year: 2005
Pages: 157

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net