implementing get top albums and artists

This commit is contained in:
aj 2019-05-07 17:36:18 +01:00
parent 3ab02eb4cb
commit 04fee12fa4
2 changed files with 111 additions and 1 deletions

View File

@ -232,6 +232,99 @@ public class FmUserNetwork extends FmNetwork {
} }
public FMObjList getTopAlbums(String period, int number) {
Logger.getLog().log(new LogEntry("getTopAlbums").addArg(period).addArg(Integer.toString(number)));
int limit = 50;
int pages = 0;
System.out.println(number / limit);
if ((double) number % (double) limit != 0) {
pages = (number / limit) + 1;
} else {
pages = number / limit;
}
FMObjList albums = new FMObjList();
int counter;
for (counter = 0; counter < pages; counter++) {
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("user", userName);
parameters.put("period", period);
parameters.put("limit", Integer.toString(limit));
parameters.put("page", Integer.toString(counter + 1));
JSONObject obj = makeGetRequest("user.gettopalbums", parameters);
JSONArray albumsJson = obj.getJSONObject("topalbums").getJSONArray("album");
for (int i = 0; i < albumsJson.length(); i++) {
JSONObject json = (JSONObject) albumsJson.get(i);
if (albums.size() < number) {
Artist artist = new ArtistBuilder(json.getJSONObject("artist").getString("name")).build();
Album album = new AlbumBuilder(json.getString("name"), artist)
.setUserPlayCount(json.getInt("playcount")).build();
albums.add(album);
}
}
}
return albums;
}
public FMObjList getTopArtists(String period, int number) {
Logger.getLog().log(new LogEntry("getTopArtists").addArg(period).addArg(Integer.toString(number)));
int limit = 50;
int pages = 0;
System.out.println(number / limit);
if ((double) number % (double) limit != 0) {
pages = (number / limit) + 1;
} else {
pages = number / limit;
}
FMObjList artists = new FMObjList();
int counter;
for (counter = 0; counter < pages; counter++) {
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("user", userName);
parameters.put("period", period);
parameters.put("limit", Integer.toString(limit));
parameters.put("page", Integer.toString(counter + 1));
JSONObject obj = makeGetRequest("user.gettopartists", parameters);
JSONArray artistsJson = obj.getJSONObject("topartists").getJSONArray("artist");
for (int i = 0; i < artistsJson.length(); i++) {
JSONObject json = (JSONObject) artistsJson.get(i);
if (artists.size() < number) {
Artist artist = new ArtistBuilder(json.getString("name"))
.setUserPlayCount(json.getInt("playcount")).build();
artists.add(artist);
}
}
}
return artists;
}
public Artist getArtistTracks(String artistName) { public Artist getArtistTracks(String artistName) {
return getArtistTracks(getArtist(artistName)); return getArtistTracks(getArtist(artistName));
} }

View File

@ -4,6 +4,7 @@ import org.junit.Test;
import sarsoo.fmframework.music.Scrobble; import sarsoo.fmframework.music.Scrobble;
import sarsoo.fmframework.net.Key; import sarsoo.fmframework.net.Key;
import sarsoo.fmframework.util.FMObjList;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -29,4 +30,20 @@ public class FmUserNetworkTest {
System.out.println(scrobbles.size()); System.out.println(scrobbles.size());
assertNotNull(1); assertNotNull(1);
} }
@Test
public void testGetTopAlbums() {
FmUserNetwork net = new FmUserNetwork(Key.getKey(), "sarsoo");
FMObjList list = net.getTopAlbums("7day", 15);
list.stream().forEach(System.out::println);
assertEquals(15, list.size());
}
@Test
public void testGetTopArtists() {
FmUserNetwork net = new FmUserNetwork(Key.getKey(), "sarsoo");
FMObjList list = net.getTopArtists("7day", 15);
list.stream().forEach(System.out::println);
assertEquals(15, list.size());
}
} }