/** * @param $name artist name * @return spotify:artist:uri or false */ function getArtistId($name) { if (!$name) { return false; } $url = 'http://ws.spotify.com/search/1/artist?q=' . urlencode($name); $http = new HttpClient($url); $http->setCacheTime(60 * 60 * 24); //24 hours $data = $http->getBody(); //TODO: use expire time for cached response $expires = strtotime($http->getResponseHeader('Expires')) - time(); if ($http->getStatus() != 200) { d('SpotifyMetadata->getArtistId server error: ' . $http->getStatus()); d($http->getResponseHeaders()); return false; } $arr = $this->parseArtists($data); foreach ($arr as $a) { if ($a['artist'] == $name) { //d("exact match"); return $a['id']; } if (soundex($a['artist']) == soundex($name)) { //d("fuzzy match"); return $a['id']; } } return false; }
function parse($raw) { // TODO XmlReader should not handle HTTP protocol details if (is_url($raw)) { $url = $raw; $h = new HttpClient($url); // $h->setCacheTime('30m'); $raw = $h->getBody(); // d( $h->getResponseHeaders() ); if ($h->getStatus() == 404) { // not found return false; } if ($h->getStatus() == 302) { $redir = $h->getResponseHeader('location'); // echo "REDIRECT: ".$redir."\n"; $h = new HttpClient($redir); //XXX: reuse previous client? $h->setCacheTime('30m'); $url = $redir; $raw = $h->getBody(); } // prepend XML header if nonexistent if (strpos($raw, '<?xml ') === false) { $raw = '<?xml version="1.0"?>' . $raw; } } if (!$this->xml($raw)) { if (isset($url)) { throw new \Exception("Failed to parse XML from " . $url); } throw new \Exception("Failed to parse XML"); } }