コード例 #1
0
ファイル: PandoraRadio.java プロジェクト: hsingh23/pandoroid
  /**
   * Description: Gets a list of songs to be played. This function should not be called more
   * frequently than MIN_TIME_BETWEEN_PLAYLIST_CALLS allows or an error will result.
   *
   * @param station_token -A string representing the station's unique identification token.
   * @throws RPCException when a Pandora RPC error has occurred.
   * @throws IOException when Pandora's remote servers could not be reached.
   * @throws HttpResponseException when an unexpected HTTP response occurs.
   * @throws Exception when an improper call has been made, or an unexpected error occurs.
   */
  @SuppressWarnings("unchecked")
  public Vector<Song> getPlaylist(String station_token)
      throws RPCException, IOException, HttpResponseException, Exception {

    // This protects against a temporary account suspension from too many
    // playlist requests.
    if (!isGetPlaylistCallValid(station_token)) {
      throw new Exception("Playlist calls are too frequent");
    }

    if (!this.isUserAuthorized()) {
      throw new Exception(
          "Improper call to getPlaylist(), " + "the user has not been logged in yet.");
    }

    Vector<Song> songs = new Vector<Song>();

    Map<String, Object> request_args = new HashMap<String, Object>();
    request_args.put("stationToken", station_token);

    // Order matters in this URL request. The same order given here is
    // the order received.
    request_args.put("additionalAudioUrl", MP3_128 + "," + AAC_32);

    JSONObject response = this.doCall("station.getPlaylist", request_args, true, true, null);

    JSONArray songs_returned = response.getJSONArray("items");
    for (int i = 0; i < songs_returned.length(); ++i) {
      Map<String, Object> song_data = JSONHelper.toMap(songs_returned.getJSONObject(i));
      ArrayList<PandoraAudioUrl> audio_url_mappings = new ArrayList<PandoraAudioUrl>();
      if (song_data.get("additionalAudioUrl") instanceof Vector<?>) {
        Vector<String> audio_urls = (Vector<String>) song_data.get("additionalAudioUrl");

        // This has to be in the same order as the request.
        audio_url_mappings.add(new PandoraAudioUrl(MP3_128, 128, audio_urls.get(0)));
        audio_url_mappings.add(new PandoraAudioUrl(AAC_32, 32, audio_urls.get(1)));
      }
      // MP3_192 data
      if (isPandoraOneCredentials()) {
        audio_url_mappings.add(
            new PandoraAudioUrl(
                (Map<String, Object>)
                    ((Map<String, Object>) song_data.get("audioUrlMap")).get("highQuality")));
      }
      // AAC_64 data
      audio_url_mappings.add(
          new PandoraAudioUrl(
              (Map<String, Object>)
                  ((Map<String, Object>) song_data.get("audioUrlMap")).get("mediumQuality")));
      songs.add(new Song(song_data, audio_url_mappings));
    }

    this.last_acquired_playlist_time = System.currentTimeMillis() / 1000L;
    this.last_acquired_playlist_station = station_token;

    return songs;
  }