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;
}
public Artist getArtistTracks(String artistName) {
return getArtistTracks(getArtist(artistName));
public ArrayList<Scrobble> getTrackScrobbles(Track track) {
Logger.getLog()
.log(new LogEntry("getTrackScrobbles").addArg(track.getName()).addArg(track.getArtist().getName()));
return getRecursiveTrackScrobbles(track, 1);
}
public Artist getArtistTracks(Artist artist) {
Logger.getLog().log(new LogEntry("getArtistTracks").addArg(artist.getName()));
private ArrayList<Scrobble> getRecursiveTrackScrobbles(Track track, int page) {
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>();
Boolean done = false;
int counter = 1;
while (!done) {
JSONObject obj = makeGetRequest("user.gettrackscrobbles", parameters);
HashMap<String, String> parameters = new HashMap<String, String>();
JSONArray returnedScrobbles = obj.getJSONObject("trackscrobbles").getJSONArray("track");
System.out.println(returnedScrobbles.length() + " length");
parameters.put("user", userName);
parameters.put("artist", artist.getName());
parameters.put("limit", Integer.toString(limit));
parameters.put("page", Integer.toString(counter + 1));
if (returnedScrobbles.length() > 0) {
JSONObject obj = makeGetRequest("user.getartisttracks", parameters);
for (int i = 0; i < returnedScrobbles.length(); i++) {
JSONArray returnedScrobbles = obj.getJSONObject("artisttracks").getJSONArray("track");
JSONObject scrob = returnedScrobbles.getJSONObject(i);
if (returnedScrobbles.length() > 0) {
Scrobble scrobble = new Scrobble(scrob.getJSONObject("date").getLong("uts"), track);
for (int i = 0; i < returnedScrobbles.length(); i++) {
JSONObject scrob = returnedScrobbles.getJSONObject(i);
}
} else {
done = true;
scrobbles.add(scrobble);
}
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) {

View File

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