added gettrackscrobbles

This commit is contained in:
aj 2019-05-09 11:48:10 +01:00
parent 7ae40b1c8c
commit 6b465e25fe
2 changed files with 53 additions and 28 deletions

View File

@ -372,47 +372,56 @@ public class FmUserNetwork extends FmNetwork {
return artists; return artists;
} }
public Artist getArtistTracks(String artistName) { public ArrayList<Scrobble> getTrackScrobbles(Track track) {
return getArtistTracks(getArtist(artistName));
Logger.getLog()
.log(new LogEntry("getTrackScrobbles").addArg(track.getName()).addArg(track.getArtist().getName()));
return getRecursiveTrackScrobbles(track, 1);
} }
public Artist getArtistTracks(Artist artist) { private ArrayList<Scrobble> getRecursiveTrackScrobbles(Track track, int page) {
Logger.getLog().log(new LogEntry("getArtistTracks").addArg(artist.getName()));
int limit = 50; int limit = 50;
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("user", userName);
parameters.put("track", track.getName());
parameters.put("artist", track.getArtist().getName());
parameters.put("limit", Integer.toString(limit));
parameters.put("page", Integer.toString(page));
ArrayList<Scrobble> scrobbles = new ArrayList<Scrobble>(); ArrayList<Scrobble> scrobbles = new ArrayList<Scrobble>();
Boolean done = false; JSONObject obj = makeGetRequest("user.gettrackscrobbles", parameters);
int counter = 1;
while (!done) {
HashMap<String, String> parameters = new HashMap<String, String>(); JSONArray returnedScrobbles = obj.getJSONObject("trackscrobbles").getJSONArray("track");
parameters.put("user", userName); System.out.println(returnedScrobbles.length() + " length");
parameters.put("artist", artist.getName());
parameters.put("limit", Integer.toString(limit));
parameters.put("page", Integer.toString(counter + 1));
JSONObject obj = makeGetRequest("user.getartisttracks", parameters); if (returnedScrobbles.length() > 0) {
JSONArray returnedScrobbles = obj.getJSONObject("artisttracks").getJSONArray("track"); for (int i = 0; i < returnedScrobbles.length(); i++) {
if (returnedScrobbles.length() > 0) { JSONObject scrob = returnedScrobbles.getJSONObject(i);
for (int i = 0; i < returnedScrobbles.length(); i++) { Scrobble scrobble = new Scrobble(scrob.getJSONObject("date").getLong("uts"), track);
JSONObject scrob = returnedScrobbles.getJSONObject(i); scrobbles.add(scrobble);
}
} else {
done = true;
} }
int totalPages = obj.getJSONObject("trackscrobbles").getJSONObject("@attr").getInt("totalPages");
if (totalPages > page) {
scrobbles.addAll(getRecursiveTrackScrobbles(track, page + 1));
}
} }
return null; return scrobbles;
} }
public ArrayList<Scrobble> getRecentScrobbles(int number) { public ArrayList<Scrobble> getRecentScrobbles(int number) {

View File

@ -3,6 +3,9 @@ package sarsoo.fmframework.fm;
import org.junit.Test; import org.junit.Test;
import sarsoo.fmframework.music.Scrobble; import sarsoo.fmframework.music.Scrobble;
import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.music.Track.TrackBuilder;
import sarsoo.fmframework.music.Artist.ArtistBuilder;
import sarsoo.fmframework.net.Key; import sarsoo.fmframework.net.Key;
import sarsoo.fmframework.util.FMObjList; import sarsoo.fmframework.util.FMObjList;
@ -26,7 +29,7 @@ public class FmUserNetworkTest {
public void testGetRecentTracks() { public void testGetRecentTracks() {
FmUserNetwork net = new FmUserNetwork(Key.getKey(), "sarsoo"); FmUserNetwork net = new FmUserNetwork(Key.getKey(), "sarsoo");
ArrayList<Scrobble> scrobbles = net.getRecentScrobbles(50); ArrayList<Scrobble> scrobbles = net.getRecentScrobbles(50);
scrobbles.stream().forEach(System.out::println); // scrobbles.stream().forEach(System.out::println);
System.out.println(scrobbles.size()); System.out.println(scrobbles.size());
assertNotNull(1); assertNotNull(1);
} }
@ -35,7 +38,7 @@ public class FmUserNetworkTest {
public void testGetTopAlbums() { public void testGetTopAlbums() {
FmUserNetwork net = new FmUserNetwork(Key.getKey(), "sarsoo"); FmUserNetwork net = new FmUserNetwork(Key.getKey(), "sarsoo");
FMObjList list = net.getTopAlbums("7day", 15); FMObjList list = net.getTopAlbums("7day", 15);
list.stream().forEach(System.out::println); // list.stream().forEach(System.out::println);
assertEquals(15, list.size()); assertEquals(15, list.size());
} }
@ -43,7 +46,7 @@ public class FmUserNetworkTest {
public void testGetTopArtists() { public void testGetTopArtists() {
FmUserNetwork net = new FmUserNetwork(Key.getKey(), "sarsoo"); FmUserNetwork net = new FmUserNetwork(Key.getKey(), "sarsoo");
FMObjList list = net.getTopArtists("7day", 15); FMObjList list = net.getTopArtists("7day", 15);
list.stream().forEach(System.out::println); // list.stream().forEach(System.out::println);
assertEquals(15, list.size()); assertEquals(15, list.size());
} }
@ -51,7 +54,20 @@ public class FmUserNetworkTest {
public void testGetTopTracks() { public void testGetTopTracks() {
FmUserNetwork net = new FmUserNetwork(Key.getKey(), "sarsoo"); FmUserNetwork net = new FmUserNetwork(Key.getKey(), "sarsoo");
FMObjList list = net.getTopTracks("7day", 15); FMObjList list = net.getTopTracks("7day", 15);
list.stream().forEach(System.out::println); // list.stream().forEach(System.out::println);
assertEquals(15, list.size()); assertEquals(15, list.size());
} }
@Test
public void testTrackScrobbles() {
FmUserNetwork net = new FmUserNetwork(Key.getKey(), "sarsoo");
Track track = new TrackBuilder("shitsville", new ArtistBuilder("freddie gibbs").build()).build();
ArrayList<Scrobble> scrobbles = net.getTrackScrobbles(track);
scrobbles.stream().forEach(System.out::println);
System.out.println(scrobbles.size());
assertEquals(53, scrobbles.size());
}
} }