*/ class LastfmComponent { var $url_base = "http://ws.audioscrobbler.com/1.0/"; /* * Artist Data */ function topTracksForArtist($artist) { $url_form = $this->url_base.'artist/'.rawurlencode($artist).'/toptracks.xml'; return $this->processRequest($url_form); } function topAlbumsForArtist($artist) { $url_form = $this->url_base.'artist/'.rawurlencode($artist).'/topalbums.xml'; return $this->processRequest($url_form); } function topTagsForArtist($artist) { $url_form = $this->url_base.'artist/'.rawurlencode($artist).'/toptags.xml'; return $this->processRequest($url_form); } function relatedArtistsForArtist($artist) { $url_form = $this->url_base.'artist/'.rawurlencode($artist).'/similar.xml'; return $this->processRequest($url_form); } /* * Album Data */ function artistAlbum($artist,$album) { $url_form = $this->url_base.'album/'.rawurlencode($artist).'/'.rawurlencode($album).'/info.xml'; return $this->processRequest($url_form); } /* * User related functions. * * These can be used to obtain information related to a specific * users listening habits. */ function topArtistsForUser($user,$span = null) { $url_form = $this->url_base.'user/'.rawurlencode($user).'/topartists.xml'; return $this->processRequest($url_form); } function topAlbumsForUser($user,$span = null) { $url_form = $this->url_base.'user/'.rawurlencode($user).'/topalbums.xml'; return $this->processRequest($url_form); } function topTrackForUser($user,$span = null) { $url_form = $this->url_base.'user/'.rawurlencode($user).'/toptracks.xml'; return $this->processRequest($url_form); } function recentTracksForUser($user) { $url_form = $this->url_base.'user/'.rawurlencode($user).'/recenttracks.xml'; return $this->processRequest($url_form); } /** * Helper function to load the url of the requested lastfm information * * This function takes the url of a valid lastfm REST request and returns * a simplexml object from the incoming xml. This function will also optional * cache the request. * * @author Troy Stanger */ private function processRequest($url, $use_cache = true) { $key = md5($url); $cache_name = "cache/lastfm.php?".$key; $cache_expires = "+1 hour"; // Load the data from cache //$cache_data = $use_cache ? cache($cache_name, null, $cache_expires) : null; $cache_data = array(); if (empty($cache_data)) { // Big Hack to make sure we aren't making too many requests in a row // Last.fm only allows 1 request per second sleep(2); //fix requests for non-existant files $headers = get_headers($url); if($headers[0] != "HTTP/1.0 404 Not Found") { $data = file_get_contents($url); } else { $data = null; } // Save the request unless told not to if ($use_cache) { //cache($cache_name, $data, $cache_expires); } } else { //$this->log("Cache Hit"); $data = $cache_data; } $xml = simplexml_load_string($data); return $xml; } } ?>