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.




