Listing your Last.fm last 10 tracks



I saw that KoRnDragon had a textual list of his last 10 played songs on his website. I asked him how to do it, and upon realizing last.fm has an RSS feed of the information I decided to just write the code myself. (As of writing this he hasn't sent me the code).

What is last.fm? It is a service that keeps track of all the songs you play from your favorite media player (through a plug-in) and allows you to display the tracks on your website. One problem, they only give you the resources to display an image of the last 10 tracks you have played. This solution allows you to embed the code in your site and allow's you to theme the tracks to follow your site theme. Download their plugin here (You need their plug-in AND this code to display the tracks on your website).

Here it is:

<?php
#program made by nucleocide.net
#released under the GNU GPL license
#so do whatever the hell you want to with it
#the only thing you need to do is change the username
#all styling *should* be done with CSS, but if you don't know css
#    figure out where to put the <font> tags.

$name = 'nucleocide';

$file = fopen("http://ws.audioscrobbler.com/1.0/user/$name/recenttracks.rss", "r");
if (!$file) {
    echo "Cannot open RSS feed.\n";

} else {
    $title = '<div class="tracks">';
    $i = 0;
    while (!feof ($file)) {
        if (!($i++ % 2)) { #optional every other row coloring
            $data = 'even';
        } else {
            $data = 'odd';
        }
        $line = fgets ($file, 1024);
        if (eregi ("<title>(.*)</title>", $line, $out)) {
            if (isset($j)) { #gets rid of first occurance of title (user info)
                $title .= "<div class=\"$data\">" . $out[1] . "</div>\n";
            }
            $j=1;
        }
    }
    $title .= "</div>\n";
    $title = nukeClean($title);
    echo $title;
    fclose($file);
}


function nukeClean($value) {
    #we get three weird symbols for the track seperator
    #this fixes it (first char is â followed by two others
    return ereg_replace("â(.?.?)", "-", $value);
}
?>

It's pretty simple. Have all the fun with it you want. (Copy and paste the code or download the file here. *File contains the update to show how long ago the song was played. View demo here.

Tutorial Added Nov 4, 2006 @ 5:33pm

Login

Username:
Password:

Shoutbox

By !nucleo
Jun 12, 2008 7:56pm
About time you're back... Shoot me an email with your 88x31.
By ?Lee
Jun 12, 2008 3:27pm
vimixx.net has moved to lee-stewart.co.uk!!!
By ?Medvedko
May 4, 2008 7:04am
That's right. Sorry, I am not making much sense, am I? Basically, I created the DB myself instead of using your install script. All necessary tables are there, I added few more based on plugin POST options and added those to the update.php also. At the beginning I added mysql.ssi.php function to both update and winamp_playing scripts as I had some troubles before with include_once.
By !nucleo
May 2, 2008 9:56am
Now I'm really confused, are you modifying my shoutbox script?
By ?Medvedko
Apr 30, 2008 1:08am
another thing is that update script terminates with invalid password (I added the pass in the Additional Options in the plugin and set the pass in the update script.
By ?Medvedko
Apr 30, 2008 1:06am
my fault really. I modded the scripts and added more tables to the DB such as "TIMESTAMP". I think include_once doesn't fetch the mysql function. Or atleast this is what seems to me is the problem.
Name: Shout: This Number: [View All]

Partners

Rand Affiliates

Homework Inspiration RenownedMedia.com BlueSfear Andreas on the Web