playlistUris.add("inbox");
playlistUris.add("starred");
try {
System.out.println("Making call to: " + SERVICE_URL + "/playlists.json");
JSONObject response = c.resource(SERVICE_URL + "/playlists.json").accept("application/json").get(JSONObject.class);
try {
JSONArray playlistArray = response.getJSONArray("playlists");
for (int i = 0; i < playlistArray.length(); i++) {
try {
playlistUris.add(playlistArray.getJSONObject(i).getString("uri"));
} catch (JSONException e) {
e.printStackTrace();
//TODO: Error handling ?
}
}
} catch (JSONException e) {
e.printStackTrace();
//TODO: Error handling ?
}
int noOfTracks = 0;
for (String playlistUri : playlistUris) {
response = c.resource(SERVICE_URL + "/" + playlistUri + "/playlists.json").accept("application/json").get(JSONObject.class);
try {
SpotifyPlaylistData playlistData = mapper.readValue(response.toString(), SpotifyPlaylistData.class);
playlists.add(playlistData);
noOfTracks += playlistData.getTracks().size();
} catch (JsonMappingException e) {
e.printStackTrace();
//TODO: Error handling ?
} catch (JsonParseException e) {
e.printStackTrace();
//TODO: Error handling ?
} catch (IOException e) {
e.printStackTrace();
//TODO: Error handling ?
}
}
long i = 0;
long offset = 0;
final long CHUNK_SIZE = 10;
for (SpotifyPlaylistData playlist : playlists) {
for (SpotifyPlaylistTrackData trackData : playlist.getTracks()) {
if (!isAborted()) {
if (i == 0) {
entityManager.getTransaction().begin();
}
TrackData track = new TrackData();
track.setFile(trackData.getUri());
track.setFormat("spotify");
track.setSmdID(trackData.getUri());
track.setUrl(trackData.getUri());
track.setTags(new ArrayList<TagData>());
track.getTags().add(new TagData(TagData.ALBUM, trackData.getAlbum()));
track.getTags().add(new TagData(TagData.TITLE, trackData.getName()));
track.getTags().add(new TagData(TagData.SPOTIFY_TRACK_ID, trackData.getUri()));
track.getTags().add(new TagData("SPOTIFYIMAGE", trackData.getCover()));
track.getTags().add(new TagData("DURATION", "" + trackData.getDuration()));
if (trackData.getIndex() != null) {
if (trackData.getIndex() != null && trackData.getIndex() != 0) {
track.getTags().add(new TagData(TagData.TRACKNUM, "" + trackData.getIndex()));
}
if (trackData.getDisc() != null && trackData.getDisc() != 0) {
track.getTags().add(new TagData(TagData.DISC, "" + trackData.getDisc()));
}
if (trackData.getAlbumuri() != null) {
track.getTags().add(new TagData(TagData.SPOTIFY_ALBUM_ID, trackData.getAlbumuri()));
}
if (trackData.getArtists() != null) {
for (SpotifyArtistData artistData : trackData.getArtists()) {
track.getTags().add(new TagData(TagData.ARTIST, artistData.getName()));
track.getTags().add(new TagData(TagData.SPOTIFY_ARTIST_ID, artistData.getUri()));
}
}
} else {
//TODO: Remove this as soon as 2.2.7 of spotifyd has been officially released
response = c.resource(SERVICE_URL + "/" + trackData.getUri() + "/browse.json").accept("application/json").get(JSONObject.class);
try {
if (response.has("index") && !response.getString("index").equals("0")) {
track.getTags().add(new TagData(TagData.TRACKNUM, response.getString("index")));
}
if (response.has("disc") && !response.getString("disc").equals("0")) {
track.getTags().add(new TagData(TagData.DISC, response.getString("disc")));
}
if (response.has("albumuri") && response.getString("albumuri").length() > 0) {
track.getTags().add(new TagData(TagData.SPOTIFY_ALBUM_ID, response.getString("albumuri")));
}
if (response.has("artists")) {
JSONArray artistsArray = response.getJSONArray("artists");
for (int j = 0; j < artistsArray.length(); j++) {
JSONObject artistObject = artistsArray.getJSONObject(j);
track.getTags().add(new TagData(TagData.ARTIST, artistObject.getString("name")));
track.getTags().add(new TagData(TagData.SPOTIFY_ARTIST_ID, artistObject.getString("uri")));
}
}
} catch (JSONException e) {
e.printStackTrace();
//TODO: Implement error handling