adding library getting

This commit is contained in:
aj 2019-05-26 19:05:04 +01:00
parent 63c5d0ef58
commit 0d06d0b6cc
2 changed files with 67 additions and 9 deletions

View File

@ -52,7 +52,8 @@ public class SpotNetwork {
} }
public ArrayList<PlaylistTrack> getPlaylistTracks(String playlistId) { public ArrayList<PlaylistTrack> getPlaylistTracks(String playlistId) {
List<JSONObject> list = makePagedGetRequest(String.format("https://api.spotify.com/v1/playlists/%s/tracks", playlistId)); List<JSONObject> list = makePagedGetRequest(
String.format("https://api.spotify.com/v1/playlists/%s/tracks", playlistId));
ArrayList<PlaylistTrack> tracks = new ArrayList<>(); ArrayList<PlaylistTrack> tracks = new ArrayList<>();
@ -61,6 +62,16 @@ public class SpotNetwork {
return tracks; return tracks;
} }
public ArrayList<Album> getLibrary() {
List<JSONObject> list = makePagedGetRequest("https://api.spotify.com/v1/me/albums");
ArrayList<Album> albums = new ArrayList<>();
list.stream().forEach(t -> albums.add(parseSimplifiedAlbum(t.getJSONObject("album"))));
return albums;
}
private ArrayList<JSONObject> makePagedGetRequest(String url) { private ArrayList<JSONObject> makePagedGetRequest(String url) {
HttpRequest request; HttpRequest request;
@ -213,6 +224,17 @@ public class SpotNetwork {
Album album = new Album(object.getString("name"), object.getString("id"), object.getString("uri"), Album album = new Album(object.getString("name"), object.getString("id"), object.getString("uri"),
object.getString("href"), type, artists); object.getString("href"), type, artists);
try {
ArrayList<Track> tracks = new ArrayList<>();
object.getJSONObject("tracks").getJSONArray("items").forEach(t -> tracks.add(parseTrack((JSONObject) t)));
album.setTracks(tracks);
} catch (JSONException e) {
System.out.println("no tracks");
}
return album; return album;
} }
@ -238,8 +260,35 @@ public class SpotNetwork {
PlaylistTrack track = new PlaylistTrack(trackObj.getString("name"), trackObj.getString("id"), PlaylistTrack track = new PlaylistTrack(trackObj.getString("name"), trackObj.getString("id"),
trackObj.getString("uri"), trackObj.getString("href"), album, trackObj.getString("uri"), trackObj.getString("href"), album,
LocalDateTime.ofInstant(Instant.parse(object.getString("added_at")), ZoneId.systemDefault()), parseUser(object.getJSONObject("added_by")), LocalDateTime.ofInstant(Instant.parse(object.getString("added_at")), ZoneId.systemDefault()),
trackObj.getBoolean("is_local"), artists); parseUser(object.getJSONObject("added_by")), trackObj.getBoolean("is_local"), artists);
return track;
}
private Track parseTrack(JSONObject object) {
JSONObject trackObj = object.getJSONObject("track");
Album album = null;
try {
album = parseSimplifiedAlbum(trackObj.getJSONObject("album"));
} catch (JSONException e) {
System.out.println("no album");
}
ArrayList<Artist> artists = new ArrayList<>();
JSONArray artistObj = trackObj.getJSONArray("artists");
for (Object i : artistObj) {
JSONObject artist = (JSONObject) i;
artists.add(parseSimplifiedArtist(artist));
}
Track track = new Track(trackObj.getString("name"), trackObj.getString("id"), trackObj.getString("uri"),
trackObj.getString("href"), album, artists);
return track; return track;
} }

View File

@ -7,6 +7,7 @@ import java.util.Scanner;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import sarsoo.spotframework.model.Album;
import sarsoo.spotframework.model.Playlist; import sarsoo.spotframework.model.Playlist;
import sarsoo.spotframework.model.PlaylistTrack; import sarsoo.spotframework.model.PlaylistTrack;
@ -18,15 +19,23 @@ class SpotNetworkTest {
Scanner scan = new Scanner(System.in); Scanner scan = new Scanner(System.in);
SpotNetwork net = new SpotNetwork(scan.nextLine()); SpotNetwork net = new SpotNetwork(scan.nextLine());
ArrayList<Playlist> playlists = net.getUserPlaylists(); // ArrayList<Playlist> playlists = net.getUserPlaylists();
ArrayList<PlaylistTrack> tracks = net.getPlaylistTracks(playlists.get(0).getId()); // ArrayList<PlaylistTrack> tracks = net.getPlaylistTracks(playlists.get(0).getId());
tracks.stream().forEach(t -> System.out.println(t.getAddedAt())); // tracks.stream().forEach(t -> System.out.println(t.getAddedAt()));
System.out.println(tracks.size()); // System.out.println(tracks.size());
// .stream().forEach(System.out::println); // .stream().forEach(System.out::println);
ArrayList<Album> albums = net.getLibrary();
// albums.stream().forEach(System.out::println);
albums.get(50).getTracks().stream().forEach(System.out::println);
System.out.println(albums.size());
scan.close(); scan.close();
assertTrue(true); assertTrue(true);
} }