// Program: recenttracks.php
// By: linein.org
// Date: 11.3.2006
// File format: timestamp,artist - track
// your last.fm username
function recenttracks($user)
{
//$user = 'linein_org';
// location on server where the cached file will be located (it must be absolute and the folder must have full (777) permission)
$cache = "$_SERVER[DOCUMENT_ROOT]/media/recenttracks.txt";
// sets played date using PHP date
$date_format = 'g:i a m.d.Y'; // 10:31 am 11.03.2006
// display styles
$track_num_style = 'font-family: arial; font-size: 10px; color: #666;';
$artist_style = 'font-family: arial; font-size: 11px; color: #fff;';
$track_style = 'font-family: arial; font-size: 11px; color: #fff;';
$date_style = 'font-family: arial; font-size:9px; color: #666;';
$dash_style = 'font-family: arial; font-size:9px; color: #999;';
// you could add here the ability to only cache ever 30 min or so, but I like it live
filemtime($cache);
$update = @file_get_contents("http://ws.audioscrobbler.com/1.0/user/$user/recenttracks.txt");
$f = fopen($cache, "w");
fwrite($f, $update);
fclose($f);
$cache = file_get_contents($cache);
$cache = str_replace( '–', '-', $cache ); // replaces en dash with regular dash (thanks MrSomeone - http://www.last.fm/user/MrSomeone/)
$cache = explode("\n", $cache);
$track_num = 1; // starting track number (you could change to count($cache) and count down)
foreach($cache as $data)
{
if(!empty($data))
{
$info = explode(",", $data, 2); // sperates date by only seperating at fist instance of a comma (since some artist/track have comma in their names
$played_time = $info[0];
$info_track = explode(" - ", $info[1]); // seperates artist and title
$artist = $info_track[0];
$title = $info_track[1];
$listingto .= ''.$track_num.')
'.$artist.'
-
'.$title.'
('.date("$date_format", $played_time).')
';
$track_num++; // adds 1 to track number
}
}
return $listingto;
}
?>