public function __construct($query)
 {
     // Thanks, Lukes <https://github.com/lukes>
     $url = "https://raw.githubusercontent.com/lukes/ISO-3166-Countries-with-Regional-Codes/master/all/all.json";
     $fetcher = new JsonFetcher($url);
     $json = $fetcher->run();
     $this->search = mb_substr($query, mb_strlen('Country Code ⟩'));
     foreach ($json as $key => $value) {
         $currentCountry = array('name' => $value->name, 'code' => $value->{'alpha-2'});
         $this->countries[] = $currentCountry;
     }
     $this->countries[] = array('name' => "I'd rather not give a country!", 'code' => 'not-given');
 }
Esempio n. 2
0
 public function __construct($options)
 {
     $this->search = $options['search'];
     $this->currentURI = $options['URIs'][$options['depth'] - 1];
     $explodedURI = explode(":", $this->currentURI);
     $this->type = $explodedURI[1];
     $this->rawType = $this->type == "artist" ? "album" : "track";
     $fetcher = new JsonFetcher("http://ws.spotify.com/lookup/1/.json?uri={$this->currentURI}&extras={$this->rawType}detail");
     $json = $fetcher->run();
     $this->title = $json->{$this->type}->name;
     $this->raw = array();
     if ($this->rawType == "album") {
         $albums = array();
         $this->query = implode(" ⟩", $options['args']);
         foreach ($json->artist->albums as $key => $value) {
             $value = $value->album;
             if (in_array($value->name, $albums)) {
                 continue;
             }
             $currentResult['title'] = $value->name;
             $currentResult['type'] = 'album';
             $currentResult['href'] = $value->href;
             if ($this->search != '' && !mb_stristr($currentResult['title'], $this->search)) {
                 continue;
             }
             $this->raw[] = $currentResult;
             $albums[] = $value->name;
         }
     } else {
         foreach ($json->album->tracks as $key => $value) {
             $currentResult['title'] = $value->name;
             $currentResult['type'] = 'track';
             $currentResult['href'] = $value->href;
             $currentResult['number'] = $value->{'track-number'};
             $currentResult['popularity'] = $value->popularity;
             $currentResult['length'] = $value->length;
             if ($this->search != '' && !mb_stristr($currentResult['title'], $this->search)) {
                 continue;
             }
             $this->raw[] = $currentResult;
         }
     }
 }
Esempio n. 3
0
 public function __construct($query)
 {
     $this->query = $query;
     // Build the search results
     // for each query type
     foreach (array('artist', 'album', 'track') as $type) {
         /* Fetch and parse the search results. */
         $urlQuery = str_replace("%3A", ":", urlencode($query));
         $url = "http://ws.spotify.com/search/1/{$type}.json?q={$urlQuery}";
         $fetcher = new JsonFetcher($url);
         $json = $fetcher->run();
         // Create the search results array
         foreach ($json->{$type . "s"} as $key => $value) {
             // TODO check region availability
             // Weight popularity
             $popularity = $value->popularity;
             if ($type == 'artist') {
                 $popularity += 0.5;
             }
             if ($type == 'album') {
                 $popularity += 0.15;
             }
             if ($type == 'track') {
                 $currentRaw['album'] = $value->album->name;
                 $currentRaw['artist'] = $value->artists[0]->name;
             } elseif ($type == 'album') {
                 $currentRaw['artist'] = $value->artists[0]->name;
             }
             $currentRaw['type'] = $type;
             $currentRaw['title'] = $value->name;
             $currentRaw['popularity'] = $popularity;
             $currentRaw['href'] = $value->href;
             $this->search[] = $currentRaw;
         }
     }
     if (!empty($this->search)) {
         usort($this->search, array($this, 'popularitySort'));
     }
 }
Esempio n. 4
0
 public function __construct($options)
 {
     $this->alfred = new OhAlfred();
     $locale = $this->alfred->options('country');
     $this->currentURI = $options['currentURI'];
     $this->query = $options['query'];
     $this->originalQuery = $options['originalQuery'];
     $this->search = $options['search'];
     $artistFetcher = new JsonFetcher("https://api.spotify.com/v1/albums/{$options['id']}");
     $artistJson = $artistFetcher->run();
     $this->name = $artistJson->name;
     $this->type = $artistJson->type;
     $url = "https://api.spotify.com/v1/albums/{$options['id']}/tracks";
     if ($locale != 'not-given') {
         $url .= "?market={$locale}";
     }
     $tracksFetcher = new JsonFetcher($url);
     $tracksJson = $tracksFetcher->run();
     $this->tracks = array();
     foreach ($tracksJson->items as $key => $value) {
         $this->tracks[] = array('uri' => $value->uri, 'name' => $value->name, 'type' => $value->type, 'number' => $value->track_number, 'duration' => $value->duration_ms, 'explicit' => $value->explicit == 'true');
     }
 }
 public function __construct($options)
 {
     $this->alfred = new OhAlfred();
     $locale = $this->alfred->options('country');
     $this->currentURI = $options['currentURI'];
     $this->query = $options['query'];
     $this->originalQuery = $options['originalQuery'];
     $this->search = $options['search'];
     $artistFetcher = new JsonFetcher("https://api.spotify.com/v1/artists/{$options['id']}");
     $artistJson = $artistFetcher->run();
     $this->name = $artistJson->name;
     $this->type = $artistJson->type;
     $url = "https://api.spotify.com/v1/artists/{$options['id']}/albums";
     if ($locale != 'not-given') {
         $url .= "?market={$locale}";
     }
     $albumFetcher = new JsonFetcher($url);
     $albumsJson = $albumFetcher->run();
     $this->albums = array();
     foreach ($albumsJson->items as $key => $value) {
         $this->albums[] = array('uri' => $value->uri, 'name' => $value->name, 'type' => $value->album_type);
     }
     // TODO search for more!
 }
Esempio n. 6
0
 public function __construct($query)
 {
     $this->query = $query;
     $this->alfred = new OhAlfred();
     $locale = $this->alfred->options('country');
     /* Fetch and parse the search results. */
     $urlQuery = str_replace("%3A", ":", urlencode($query));
     $url = "https://api.spotify.com/v1/search?q={$urlQuery}&type=artist,album,track";
     if ($locale != 'not-given') {
         $url .= "&market={$locale}";
     }
     $fetcher = new JsonFetcher($url);
     $json = $fetcher->run();
     // Albums do not include artist data.
     // Grab all the album ids, and find their artists
     $albumIDs = array();
     foreach ($json->albums->items as $key => $value) {
         $albumIDs[] = $value->id;
     }
     if (sizeof($albumIDs) != 0) {
         $urlQuery = str_replace("%3A", ":", urlencode(join(',', $albumIDs)));
         $url = "https://api.spotify.com/v1/albums?ids={$urlQuery}";
         $albumFetcher = new JsonFetcher($url);
         $albumsJson = $albumFetcher->run();
         $albums = array();
         foreach ($albumsJson->albums as $key => $value) {
             $albums[] = array('artist' => $value->artists[0]->name, 'popularity' => $value->popularity);
         }
     }
     // Build the search results
     // for each query type
     foreach (array('artist', 'album', 'track') as $type) {
         // Create the search results array
         foreach ($json->{$type . "s"}->items as $key => $value) {
             // Weight popularity
             if ($type == 'album') {
                 $popularity = $albums[$key]['popularity'];
             } else {
                 $popularity = $value->popularity;
             }
             if ($type == 'artist') {
                 $popularity += 50;
             }
             if ($type == 'album') {
                 $popularity += 25;
             }
             if ($type == 'track') {
                 $currentRaw['album'] = $value->album->name;
                 $currentRaw['artist'] = $value->artists[0]->name;
             } elseif ($type == 'album') {
                 $currentRaw['artist'] = $albums[$key]['artist'];
             }
             if ($type == 'album') {
                 $currentRaw['type'] = $value->album_type;
             } else {
                 $currentRaw['type'] = $value->type;
             }
             $currentRaw['title'] = $value->name;
             $currentRaw['popularity'] = $popularity;
             $currentRaw['uri'] = $value->uri;
             $this->search[] = $currentRaw;
         }
     }
     if (!empty($this->search)) {
         usort($this->search, array($this, 'popularitySort'));
     }
 }