Note 1: Last.FM request that you do not query their servers more than once per second. Imagine 50,000 websites all performing 10 queries at the same time. To be sure that mine never will, I fetch the XML files once every 5 minutes, and then query them locally. The code I used to do that can be found
here.
Note 2: 'allow_url_fopen' needs to be set to "on" in your php.ini file. This is something your hosting company might be reluctant to turn on, and for good reason. It is not exactly secure, but it does make life so much easier.
<?PHP
$recenttracks = 'http://www.uplinkzero.com/lastfm/recenttracks.xml';
$weeklyartistchart = 'http://www.uplinkzero.com/lastfm/weeklyartistchart.xml';
$weeklytrackchart = 'http://www.uplinkzero.com/lastfm/weeklytrackchart.xml';
$topartists = 'http://www.uplinkzero.com/lastfm/topartists.xml';
$toptracks = 'http://www.uplinkzero.com/lastfm/toptracks.xml';
/******************************************
* Format the timestamp for last played
******************************************/
function FormatTimeStamp($whenplayed) {
$timenow = time();
$difference = $timenow - $whenplayed;
$days = floor($difference/86400);
$difference = $difference - ($days*86400);
$hours = floor($difference/3600);
$difference = $difference - ($hours*3600);
$minutes = floor($difference/60);
$difference = $difference - ($minutes*60);
$seconds = $difference;
if (($days == 0) && ($hours == 0) && ($minutes == 0)) {
$output = "$seconds Seconds ago.";
}
if (($days == 0) && ($hours == 0) && ($minutes != 0)) {
$output = "$minutes Minutes, $seconds Seconds ago.";
}
if (($days == 0) && ($hours != 0) && ($minutes != 0)) {
$output = "$hours Hours, $minutes Minutes ago.";
}
if (($days != 0) && ($hours != 0) && ($minutes != 0)) {
$output = "$days Days, $hours Hours ago.";
}
return $output;
}
/*******************************
* Inline CSS
*******************************/
function callCSS() {
echo '<style type="text/css">';
echo '.bar1 {text-align: right; background-color: #aaa}';
echo '.bar2 {text-align: right; background-color: #bbb}';
echo '.not {text-align: left; background-color: #eee}';
echo '</style>';
}
/*******************************
* Recently Played Tracks
*******************************/
function RecentTracks($recenttracks) {
if (!$dom = DOMDocument::load($recenttracks)) {
echo "Error while parsing lastfm/myrecenttracks.xml \n";
exit;
}
$tracks = $dom->getElementsByTagName("track");
$counter = 0;
foreach($tracks as $track) {
$dates = $track->getElementsByTagName("date");
foreach($dates as $date) {
$song[$counter]['time'] = $date->getAttribute('uts');
}
$artists = $track->getElementsByTagName("artist");
foreach($artists as $artist) {
$song[$counter]['artist'] = $artist->textContent;
$song[$counter]['mbid'] = $artist->getAttribute('mbid');
}
$names = $track->getElementsByTagName("name");
foreach($names as $name) {
$song[$counter]['track'] = $name->textContent;
}
$urls = $track->getElementsByTagName("url");
foreach($urls as $url) {
$song[$counter]['url'] = $url->textContent;
}
$counter++;
}
echo '<h2>Recently Played Tracks</h2>';
echo '<ul>';
for ($i = 0; $i <= 9; $i++) {
// echo '<li> <strong>Date: </strong>' . date('Y-m-d (H:i)', $song[$i]['time']) . '<br />';
echo '<strong>Artist: </strong>' . $song[$i]['artist'] . '<br />';
echo '<strong>Track: </strong>' . $song[$i]['track'] . '<br />';
$when = FormatTimeStamp($song[$i]['time']);
echo $when.'<br />';
echo '<a href="' . $song[$i]['url'] . '">' . $song[$i]['url'] . '</a> </li> <br /><br />';
}
echo '</ul>';
echo '<br />';
echo '<br />';
}
/*******************************
* Recent Weekly Artist Chart
*******************************/
function RecentWeekArtists($weeklyartistchart) {
if (!$dom = DOMDocument::load($weeklyartistchart)) {
echo "Error while parsing lastfm/weeklyartistchart.xml \n";
exit;
}
$artists = $dom->getElementsByTagName("artist");
$counter = 0;
foreach($artists as $artist) {
$names = $artist->getElementsByTagName("name");
foreach($names as $name) {
$song[$counter]['name'] = $name->textContent;
}
$plays = $artist->getElementsByTagName("playcount");
foreach($plays as $play) {
$song[$counter]['playcount'] = $play->textContent;
}
$urls = $artist->getElementsByTagName("url");
foreach($urls as $url) {
$song[$counter]['url'] = $url->textContent;
}
$counter++;
}
echo '<h2>Recent Weekly Top Artist Chart</h2>';
echo '<ul>';
for ($i = 0; $i <= 9; $i++) {
echo '<li> <strong>Artist: </strong>' . $song[$i]['name'] . '<br />';
echo '<strong>Play Count: </strong>' . $song[$i]['playcount'] . '<br />';
echo '<a href="' . $song[$i]['url'] . '">' . $song[$i]['url'] . '</a> </li> <br />';
}
echo '</ul>';
echo '<br />';
echo '<br />';
}
/*******************************
* Recent Weekly Track Chart
*******************************/
function RecentWeekTracks($weeklytrackchart) {
if (!$dom = DOMDocument::load($weeklytrackchart)) {
echo "Error while parsing lastfm/weeklytrackchart.xml \n";
exit;
}
$tracks = $dom->getElementsByTagName("track");
$counter = 0;
foreach($tracks as $track) {
$artists = $track->getElementsByTagName("artist");
foreach($artists as $artist) {
$song[$counter]['artist'] = $artist->textContent;
}
$names = $track->getElementsByTagName("name");
foreach($names as $name) {
$song[$counter]['track'] = $name->textContent;
}
$plays = $track->getElementsByTagName("playcount");
foreach($plays as $play) {
$song[$counter]['playcount'] = $play->textContent;
}
$urls = $track->getElementsByTagName("url");
foreach($urls as $url) {
$song[$counter]['url'] = $url->textContent;
}
$counter++;
}
echo '<h2>Recent Weekly Top Track Chart</h2>';
echo '<ul>';
for ($i = 0; $i <= 9; $i++) {
echo '<li> <strong>Artist: </strong>' . $song[$i]['artist'] . '<br />';
echo '<strong>Title: </strong>' . $song[$i]['track'] . '<br />';
echo '<strong>Play Count: </strong>' . $song[$i]['playcount'] . '<br />';
echo '<a href="' . $song[$i]['url'] . '">' . $song[$i]['url'] . '</a> </li> <br />';
}
echo '</ul>';
echo '<br />';
echo '<br />';
}
/*******************************
* Top 50 Artists
*******************************/
function Top50artists($topartists) {
if (!$dom = DOMDocument::load($topartists)) {
echo "Error while parsing lastfm/weeklyartistchart.xml \n";
exit;
}
$artists = $dom->getElementsByTagName("artist");
$counter = 0;
foreach($artists as $artist) {
$names = $artist->getElementsByTagName("name");
foreach($names as $name) {
$song[$counter]['name'] = $name->textContent;
}
$plays = $artist->getElementsByTagName("playcount");
foreach($plays as $play) {
$song[$counter]['playcount'] = $play->textContent;
}
$urls = $artist->getElementsByTagName("url");
foreach($urls as $url) {
$song[$counter]['url'] = $url->textContent;
}
$counter++;
}
echo '<h2>Top 50 Artist Chart</h2>';
echo '<br />';
echo '<ol>';
for ($i = 0; $i < $counter; $i++) {
echo '<li> <strong>Artist: </strong>' . $song[$i]['name'] . '<br />';
echo '<strong>Play Count: </strong>' . $song[$i]['playcount'] . '<br />';
echo '<a href="' . $song[$i]['url'] . '">' . $song[$i]['url'] . '</a> </li> <br />';
}
echo '</ol>';
echo '<br />';
echo '<br />';
}
/*******************************
* Top 50 Tracks
*******************************/
function Top50tracks($toptracks) {
if (!$dom = DOMDocument::load($toptracks)) {
echo "Error while parsing lastfm/weeklyartistchart.xml \n";
exit;
}
$tracks = $dom->getElementsByTagName("track");
$counter = 0;
foreach($tracks as $track) {
$artists = $track->getElementsByTagName("artist");
foreach($artists as $artist) {
$song[$counter]['artist'] = $artist->textContent;
}
$names = $track->getElementsByTagName("name");
foreach($names as $name) {
$song[$counter]['track'] = $name->textContent;
}
$plays = $track->getElementsByTagName("playcount");
foreach($plays as $play) {
$song[$counter]['playcount'] = $play->textContent;
}
$urls = $track->getElementsByTagName("url");
foreach($urls as $url) {
$song[$counter]['url'] = $url->textContent;
}
$counter++;
}
echo '<h2>Top 50 Track Chart</h2>';
echo '<br />';
echo '<ol>';
for ($i = 0; $i <= 49; $i++) {
echo '<li> <strong>Artist: </strong>' . $song[$i]['artist'] . '<br />';
echo '<strong>Title: </strong>' . $song[$i]['track'] . '<br />';
echo '<strong>Play Count: </strong>' . $song[$i]['playcount'] . '<br />';
echo '<a href="' . $song[$i]['url'] . '">' . $song[$i]['url'] . '</a> </li> <br />';
}
echo '</ol>';
echo '<br />';
echo '<br />';
}
/*******************************
* Top Artists Graph
*******************************/
function Top50artistsGraph($topartists) {
if (!$dom = DOMDocument::load($topartists)) {
echo "Error while parsing lastfm/weeklyartistchart.xml \n";
exit;
}
$artists = $dom->getElementsByTagName("artist");
$counter = 0;
foreach($artists as $artist) {
$names = $artist->getElementsByTagName("name");
foreach($names as $name) {
$song[$counter]['name'] = $name->textContent;
}
$plays = $artist->getElementsByTagName("playcount");
foreach($plays as $play) {
$song[$counter]['playcount'] = $play->textContent;
}
$urls = $artist->getElementsByTagName("url");
foreach($urls as $url) {
$song[$counter]['url'] = $url->textContent;
}
$counter++;
}
echo '<h2>Top Artist Graph</h2>';
echo '<br />';
$width = '600';
$maxlength = '300';
$factor = $song['0']['playcount'] / $maxlength; // set factor from first (most played) artist
echo '<br />';
echo '<table width="'.$width.'" border="0" cellpadding="0" cellspacing="0">';
echo '<tr>';
echo '<td>';
for ($i = 0; $i < $counter; $i++) {
$barlength = round($song[$i]['playcount'] / $factor) -1; // set the legth of the bar for each run of the loop
$notbar = round($width - $barlength); // set what will not be used for the bar in our graph
echo '<table width="'.$width.'" border="0" cellpadding="0" cellspacing="0">';
echo '<tr>';
echo '<td width="'.$barlength.'" border="1" height="10" valign="center" class="bar1"> '.$song[$i]['playcount'].' </td>';
echo '<td width="'.$notbar.'" border="0" height="10" valign="center" class="not"> - '.$song[$i]['name'].' </td>';
echo '</tr>';
echo '</table>';
$i++;
$barlength = round($song[$i]['playcount'] / $factor) -1; // set the legth of the bar for each run of the loop
$notbar = round($width - $barlength); // set what will not be used for the bar in our graph
echo '<table width="'.$width.'" border="0" cellpadding="0" cellspacing="0">';
echo '<tr>';
echo '<td width="'.$barlength.'" border="1" height="10" valign="center" class="bar2"> '.$song[$i]['playcount'].' </td>';
echo '<td width="'.$notbar.'" border="0" height="10" valign="center" class="not"> - '.$song[$i]['name'].' </td>';
echo '</tr>';
echo '</table>';
}
echo '</td>';
echo '</tr>';
echo '</table>';
echo '<br />';
echo '<br />';
}
/*******************************
* Top Tracks Graph
*******************************/
function Top50tracksGraph($toptracks) {
if (!$dom = DOMDocument::load($toptracks)) {
echo "Error while parsing lastfm/weeklyartistchart.xml \n";
exit;
}
$tracks = $dom->getElementsByTagName("track");
$counter = 0;
foreach($tracks as $track) {
$artists = $track->getElementsByTagName("artist");
foreach($artists as $artist) {
$song[$counter]['artist'] = $artist->textContent;
}
$names = $track->getElementsByTagName("name");
foreach($names as $name) {
$song[$counter]['track'] = $name->textContent;
}
$plays = $track->getElementsByTagName("playcount");
foreach($plays as $play) {
$song[$counter]['playcount'] = $play->textContent;
}
$urls = $track->getElementsByTagName("url");
foreach($urls as $url) {
$song[$counter]['url'] = $url->textContent;
}
$counter++;
}
echo '<h2>Top Track Graph</h2>';
echo '<br />';
$width = '600';
$maxlength = '300';
$factor = $song['0']['playcount'] / $maxlength; // set factor from first (most played) artist
echo '<br />';
echo '<table width="'.$width.'" border="0" cellpadding="0" cellspacing="0">';
echo '<tr>';
echo '<td>';
for ($i = 0; $i < $counter; $i++) {
$barlength = round($song[$i]['playcount'] / $factor) -1; // set the legth of the bar for each run of the loop
$notbar = round($width - $barlength) -1; // set what will not be used for the bar in our graph
echo '<table width="'.$width.'" border="0" cellpadding="0" cellspacing="0">';
echo '<tr>';
echo '<td width="'.$barlength.'" border="1" height="10" valign="center" class="bar1"> '.$song[$i]['playcount'].' </td>';
echo '<td width="'.$notbar.'" border="0" height="10" valign="center" class="not"> - '.$song[$i]['artist'].' - '.$song[$i]['track'].' </td>';
echo '</tr>';
echo '</table>';
$i++;
$barlength = round($song[$i]['playcount'] / $factor) -1; // set the legth of the bar for each run of the loop
$notbar = round($width - $barlength) -1; // set what will not be used for the bar in our graph
echo '<table width="'.$width.'" border="0" cellpadding="0" cellspacing="0">';
echo '<tr>';
echo '<td width="'.$barlength.'" border="1" height="10" valign="center" class="bar2"> '.$song[$i]['playcount'].' </td>';
echo '<td width="'.$notbar.'" border="0" height="10" valign="center" class="not"> - '.$song[$i]['artist'].' - '.$song[$i]['track'].' </td>';
echo '</tr>';
echo '</table>';
}
echo '</td>';
echo '</tr>';
echo '</table>';
echo '<br />';
echo '<br />';
}
callCSS();
Top50artistsGraph($topartists);
Top50tracksGraph($toptracks);
RecentTracks($recenttracks);
RecentWeekArtists($weeklyartistchart);
RecentWeekTracks($weeklytrackchart);
Top50artists($topartists);
Top50tracks($toptracks);
?>