adding library getting
This commit is contained in:
parent
63c5d0ef58
commit
0d06d0b6cc
@ -52,7 +52,8 @@ public class SpotNetwork {
|
||||
}
|
||||
|
||||
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<>();
|
||||
|
||||
@ -61,6 +62,16 @@ public class SpotNetwork {
|
||||
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) {
|
||||
|
||||
HttpRequest request;
|
||||
@ -213,6 +224,17 @@ public class SpotNetwork {
|
||||
Album album = new Album(object.getString("name"), object.getString("id"), object.getString("uri"),
|
||||
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;
|
||||
}
|
||||
|
||||
@ -238,8 +260,35 @@ public class SpotNetwork {
|
||||
|
||||
PlaylistTrack track = new PlaylistTrack(trackObj.getString("name"), trackObj.getString("id"),
|
||||
trackObj.getString("uri"), trackObj.getString("href"), album,
|
||||
LocalDateTime.ofInstant(Instant.parse(object.getString("added_at")), ZoneId.systemDefault()), parseUser(object.getJSONObject("added_by")),
|
||||
trackObj.getBoolean("is_local"), artists);
|
||||
LocalDateTime.ofInstant(Instant.parse(object.getString("added_at")), ZoneId.systemDefault()),
|
||||
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;
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import java.util.Scanner;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import sarsoo.spotframework.model.Album;
|
||||
import sarsoo.spotframework.model.Playlist;
|
||||
import sarsoo.spotframework.model.PlaylistTrack;
|
||||
|
||||
@ -18,15 +19,23 @@ class SpotNetworkTest {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
|
||||
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);
|
||||
|
||||
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();
|
||||
assertTrue(true);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user