* -- All of the output code is done in the lafms() function, so feel free * to tailor it to your own design. * * Current deficiency: * If this page is not called LESS THAN every 10 tracks played, it will not keep track * of all of your tracks played. Dammit. You can fix this by calling a cron job * that requests this page every 15 minutes. Unfortunately, due to the state of the * last.fm feeds, there's not much you can do to fix this. * * File Format (BNF-type-thingy): * * ENTRY: timestamp<<<>>>Artist - Song * LIST: ENTRY | ENTRY<<<<>>>>LIST * * I used <<<>>>'s as delimeters since most simpler delimiters frequently appear in actual * song titles, which may be a slight problem */ //CONFIGURATION DIRECTIVES //Ones that really SHOULD be changed $NUM_SONGS = 50; //How long the history of songs should be. $USER_NAME = "noogz"; $CURDIR = "/home/golsoft/public_html/noogz.net/music/"; //Directory to pull stuff down from. $OFFSET = 3 * 60 * 60; //Clock offset -- number of seconds you are out of time with your web server //.If you are east, number is positive, if you are west, number is negative. if (substr($CURDIR,-1) != "/") {$CURDIR .= "/";}//Append a / if necessary //Ones that don't need to be changed. $FILE = $CURDIR . "recentSongs.txt"; //Tracks database $CACHE = $CURDIR . "cache.txt"; //Timestamp cache $LAST_FM = "http://ws.audioscrobbler.com/1.0/user/$USER_NAME/recenttracks.txt"; //The LAST.FM list to pull down. //END CONFIGURATION DIRECTIVES... MAIN SCRIPT FOLLOWS //ensure that files are writable function setUpFiles() { global $FILE, $CACHE; //Check if $FILE or $CACHE exists, and if not, set them up for writing. if (!file_exists($FILE)){ $handle = fopen($FILE,"w"); fwrite($handle," "); fclose($handle); } if (!file_exists($CACHE)){ $handle = fopen($CACHE,"w"); fwrite($handle," "); fclose($handle); } } //Display the list function lafms() { global $musicList, $OFFSET; $day = -1; //Let's assume that most tracks were played AFTER 31/12/1969 :) //Traverse the song list foreach($musicList as $song) { $song[0]+= $OFFSET; if (date("z",$song[0]) != $day) { //A new day -- update the date: print "
This list of tracks was generated by The Last.fm script, which is ©2006 Chris Neugebauer. If you use last.fm and would like a simlar feed on your page, feel free to download one!
'; } //Returns the list of recently played songs. function getList() { global $FILE; //If making a new list, return an empty list. if(filesize($FILE)==0){ return array(); } $handle = fopen($FILE,"r") or die("No such file"); $contents = fread($handle,filesize($FILE)); fclose($handle); //Build a list of songs with their access time as keys. foreach (explode("<<<<>>>>",$contents) as $item) { list($time,$song) = explode("<<<>>>",$item); $theList[$time] = array($time,stripcslashes($song)); } krsort($theList); //Return it in reverse-chrolological order. return $theList; } //Writes the list of recently played songs back to a file. function writeList($theList) { global $NUM_SONGS, $LAST_FM, $FILE; krsort($theList); //By sorting the keys in reverse order. array_splice($theList,$NUM_SONGS); $output = ""; $run = 0; foreach($theList as $data){ //Add a separator if necessary. if ($run > 0){ $output .= "<<<<>>>>"; } $time = $data[0]; $song = $data[1]; $output.="$time<<<>>>$song"; //Add to list of songs $run++; } // print $output; $handle = fopen($FILE,"w") or die("Couldn't open file"); fwrite($handle,$output) or die("Nothing happened"); fclose($handle); } function shouldWrite() { //Returns "true" if we should re-process the list global $LAST_FM, $CURDIR, $lfm, $CACHE; $contents = explode("<<<<>>>>",file_get_contents($CACHE)); $timeStamp = $contents[0]; //Timestamp of last download $md5 = $contents[1]; //MD5 of previous download. //DOn't build new list if less than 60 seconds old if (time() - $timeStamp <= 60) { return false; } //Don't build new list if list has not changed. if (trim($lfm)=="") { //Ensures we only pull down the stats once. $lfm = file_get_contents($LAST_FM); } if (md5($lfm) == $md5) { return false; } return true; } function addToList($theList) { //Builds a new list from the last.fm list. global $CURDIR, $LAST_FM, $lfm, $CACHE; // Ensure we only pull down the stats once if (trim($lfm)=="") { $lfm = file_get_contents($LAST_FM); } //Build array out of last.fm page $temp = explode("\n",$lfm); foreach($temp as $line) { $ln = explode(",",$line); $time = $ln[0]; //Since certain song titles may have a "," in them, there may be extra //elements containing the song title. $song = implode(",", array_slice($ln,1)); //Reform list based on remaining elements $newSongs[$time] = array($time,$song); } //Sort the keys of both last.fm list and old list. krsort($theList); krsort($newSongs); foreach($newSongs as $song) { $t = $song[0]; $theList[$t] = $song; } //Finally, update the timestamp to reflect the current state of the page. $handle = fopen($CACHE, "w"); fwrite($handle, time()."<<<<>>>>".md5($lfm)); fclose($handle); krsort($theList); return $theList; } setUpFiles(); $musicList = getList(); //If we should build a new list if (shouldWrite()) { //Then build one. $musicList = addToList($musicList); writeList($musicList); }