/** * @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"); } }
static function shorten($input_url) { $url = 'http://is.gd/api.php?longurl=' . urlencode($input_url); $http = new HttpClient($url); $http->setCacheTime(86400); //24 hours $res = $http->getBody(); if (substr($res, 0, 4) == 'http') { return trim($res); } throw new \Exception('Error: ' . $res); }
static function shorten($url) { $url = 'http://tinyurl.com/api-create.php?url=' . urlencode($url); $http = new HttpClient($url); $http->setCacheTime(86400); //24 hours $res = $http->getBody(); if (substr($res, 0, 4) == 'http') { return trim($res); } list($error_code, $error_message) = explode('|', $res); throw new \Exception('Error: ' . $error_message . ' (' . $error_code . ')'); }
static function shorten($input_url) { /* $login = '******'; $key = 'R_0da49e0a9118ff35f52f629d2d71bf07'; */ $login = '******'; $key = 'R_f37747e06a18173096b714827c76b567'; $url = 'http://api.bit.ly/v3/shorten?format=json&login='******'&apiKey=' . $key . '&longUrl=' . urlencode($input_url); $http = new HttpClient($url); $http->setCacheTime(86400); //24 hours $res = Json::decode($http->getBody()); if ($res->status_code != 200) { throw new \Exception('Error code ' . $res->status_code . ': ' . $res->status_txt); } return $res->data->url; }
private function query($method, $params) { $url = 'http://ws.audioscrobbler.com/2.0/?method=' . $method . '&api_key=' . $this->api_key; $http = new HttpClient($url); $http->setCacheTime('12h'); foreach ($params as $key => $val) { $http->Url->setParam($key, $val); } echo "QUERYING " . $http->getUrl() . "\n"; $data = $http->getBody(); //d($data); $x = simplexml_load_string($data); /* $attrs = $x->attributes(); if ($attrs['status'] == 'failed') throw new \Exception ('last.fm api error: '.$x->error); */ return $x; }
function parse($data) { if (is_url($data)) { $u = new HttpClient($data); $u->setCacheTime(60 * 60); //1h $data = $u->getBody(); //FIXME check http client return code for 404 if (substr($data, 0, 5) != '<asx ') { dp('input_asx->parse FAIL: cant parse playlist from ' . $u->getUrl()); return false; } } $reader = new XMLReader(); if ($this->getDebug()) { echo 'Parsing ASX: ' . $data . ln(); } $reader->xml($data); $item = new VideoResource(); while ($reader->read()) { if ($reader->nodeType == XMLReader::END_ELEMENT && $reader->name == 'asx') { $this->items[] = $item; $item = new VideoResource(); } if ($reader->nodeType != XMLReader::ELEMENT) { continue; } switch ($reader->name) { case 'asx': if ($reader->getAttribute('version') != '3.0') { die('XXX FIXME unsupported ASX version ' . $reader->getAttribute('version')); } break; case 'entry': while ($reader->read()) { if ($reader->nodeType == XMLReader::END_ELEMENT && $reader->name == 'entry') { break; } if ($reader->nodeType != XMLReader::ELEMENT) { continue; } switch ($reader->name) { case 'author': break; //<author>svt.se</author> //<author>svt.se</author> case 'copyright': break; //<copyright>Sveriges Television AB 2009</copyright> //<copyright>Sveriges Television AB 2009</copyright> case 'starttime': break; //<starttime value="00:00:00.00"/> //<starttime value="00:00:00.00"/> case 'ref': //<ref href="mms://wm0.c90901.cdn.qbrick.com/90901/kluster/20091026/aekonomi920.wmv"/> $item->setUrl($reader->getAttribute('href')); break; case 'duration': //<duration value="00:03:39.00"/> $item->setDuration($reader->getAttribute('value')); break; default: echo "bad entry " . $reader->name . ln(); } } break; default: echo "unknown " . $reader->name . ln(); break; } } $reader->close(); return true; }
function parse($data) { $base_url = ''; if (is_url($data)) { //d($data); $http = new HttpClient($data); $http->setCacheTime(60 * 60); //1h $data = $http->getBody(); if (strpos($data, '#EXTM3U') === false) { throw new \Exception('M3uReader->parse FAIL: cant parse feed from ' . $http->getUrl()); return false; } $base_url = dirname($http->getUrl()); } $this->items = array(); $rows = explode("\n", $data); $ent = new VideoResource(); //echo '<pre>'; foreach ($rows as $row) { $row = trim($row); $p = explode(':', $row, 2); switch ($p[0]) { case '#EXTM3U': case '': break; /* #EXT-X-VERSION:2 #EXT-X-ALLOW-CACHE:YES #EXT-X-TARGETDURATION:10 #EXT-X-MEDIA-SEQUENCE:0 */ /* #EXT-X-VERSION:2 #EXT-X-ALLOW-CACHE:YES #EXT-X-TARGETDURATION:10 #EXT-X-MEDIA-SEQUENCE:0 */ case '#EXTINF': $x = explode(',', $p[1], 2); $ent->setDuration($x[0] != '-1' ? $x[0] : ''); $ent->setTitle($x[1]); break; // multiple quality streams for same media can exist // multiple quality streams for same media can exist case '#EXT-X-STREAM-INF': // #EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=294332 $x = explode(',', $p[1]); foreach ($x as $kv) { $x2 = explode('=', trim($kv), 2); if ($x2[0] == 'PROGRAM-ID') { $ent->track_id = $x2[1]; } if ($x2[0] == 'BANDWIDTH') { // XXX BITRATE??? $ent->bitrate = $x2[1]; } } break; default: if (substr($row, 0, 1) == '#') { break; } if ($base_url && strpos($row, '://') === false) { $row = $base_url . '/' . $row; } $ent->setUrl($row); $this->items[] = $ent; $ent = new VideoResource(); break; } } }