added get artist top tracks
This commit is contained in:
parent
2ca1930615
commit
d9fb9195f9
@ -13,9 +13,11 @@ import sarsoo.fmframework.music.FMObj;
|
|||||||
import sarsoo.fmframework.music.Track;
|
import sarsoo.fmframework.music.Track;
|
||||||
import sarsoo.fmframework.music.Track.TrackBuilder;
|
import sarsoo.fmframework.music.Track.TrackBuilder;
|
||||||
import sarsoo.fmframework.music.Wiki;
|
import sarsoo.fmframework.music.Wiki;
|
||||||
|
import sarsoo.fmframework.util.FMObjList;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
@ -240,11 +242,19 @@ public class FmNetwork {
|
|||||||
|
|
||||||
JSONObject obj = makeGetRequest("track.getinfo", parameters);
|
JSONObject obj = makeGetRequest("track.getinfo", parameters);
|
||||||
|
|
||||||
|
return parseTrack(obj.getJSONObject("track"), artist);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Track parseTrack(JSONObject trackJson, Artist artist) {
|
||||||
|
|
||||||
|
Log log = Logger.getLog();
|
||||||
|
|
||||||
String nameIn;
|
String nameIn;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
JSONObject trackJson = obj.getJSONObject("track");
|
// JSONObject trackJson = obj.getJSONObject("track");
|
||||||
|
|
||||||
nameIn = trackJson.getString("name");
|
nameIn = trackJson.getString("name");
|
||||||
|
|
||||||
@ -253,32 +263,33 @@ public class FmNetwork {
|
|||||||
try {
|
try {
|
||||||
builder.setMbid(trackJson.getString("mbid"));
|
builder.setMbid(trackJson.getString("mbid"));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
log.logInfo(new InfoEntry("getTrack").addArg("no mbid for").addArg(nameIn).addArg(e.getMessage()));
|
log.logInfo(new InfoEntry("parseTrack").addArg("no mbid for").addArg(nameIn).addArg(e.getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
builder.setUrl(trackJson.getString("url"));
|
builder.setUrl(trackJson.getString("url"));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
log.logInfo(new InfoEntry("getTrack").addArg("no url for").addArg(nameIn).addArg(e.getMessage()));
|
log.logInfo(new InfoEntry("parseTrack").addArg("no url for").addArg(nameIn).addArg(e.getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
builder.setListeners(trackJson.getInt("listeners"));
|
builder.setListeners(trackJson.getInt("listeners"));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
log.logInfo(new InfoEntry("getTrack").addArg("no listeners for").addArg(nameIn).addArg(e.getMessage()));
|
log.logInfo(
|
||||||
|
new InfoEntry("parseTrack").addArg("no listeners for").addArg(nameIn).addArg(e.getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
builder.setPlayCount(trackJson.getInt("playcount"));
|
builder.setPlayCount(trackJson.getInt("playcount"));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
log.logInfo(
|
log.logInfo(
|
||||||
new InfoEntry("getTrack").addArg("no play count for").addArg(nameIn).addArg(e.getMessage()));
|
new InfoEntry("parseTrack").addArg("no play count for").addArg(nameIn).addArg(e.getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
builder.setUserPlayCount(trackJson.getInt("userplaycount"));
|
builder.setUserPlayCount(trackJson.getInt("userplaycount"));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
log.logInfo(new InfoEntry("getTrack").addArg("no user play count for").addArg(nameIn)
|
log.logInfo(new InfoEntry("parseTrack").addArg("no user play count for").addArg(nameIn)
|
||||||
.addArg(e.getMessage()));
|
.addArg(e.getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -291,18 +302,63 @@ public class FmNetwork {
|
|||||||
builder.setWiki(wiki);
|
builder.setWiki(wiki);
|
||||||
|
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
log.logInfo(new InfoEntry("getTrack").addArg("no wiki for").addArg(nameIn).addArg(e.getMessage()));
|
log.logInfo(new InfoEntry("parseTrack").addArg("no wiki for").addArg(nameIn).addArg(e.getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return builder.build();
|
return builder.build();
|
||||||
|
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
log.logInfo(new InfoEntry("getTrack").addArg("track name not found").addArg(e.getMessage()));
|
log.logInfo(new InfoEntry("parseTrack").addArg("track name not found").addArg(e.getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public FMObjList getArtistTopTracks(Artist artist, int number) {
|
||||||
|
|
||||||
|
Logger.getLog()
|
||||||
|
.log(new LogEntry("getArtistTopTracks").addArg(artist.getName()).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 tracks = new FMObjList();
|
||||||
|
int counter;
|
||||||
|
for (counter = 0; counter < pages; counter++) {
|
||||||
|
|
||||||
|
HashMap<String, String> parameters = new HashMap<String, String>();
|
||||||
|
|
||||||
|
parameters.put("artist", artist.getName());
|
||||||
|
parameters.put("limit", Integer.toString(limit));
|
||||||
|
parameters.put("page", Integer.toString(counter + 1));
|
||||||
|
|
||||||
|
JSONObject obj = makeGetRequest("artist.gettoptracks", parameters);
|
||||||
|
|
||||||
|
JSONArray tracksJson = obj.getJSONObject("toptracks").getJSONArray("track");
|
||||||
|
|
||||||
|
for (int i = 0; i < tracksJson.length(); i++) {
|
||||||
|
JSONObject json = (JSONObject) tracksJson.get(i);
|
||||||
|
|
||||||
|
if (tracks.size() < number) {
|
||||||
|
|
||||||
|
tracks.add(parseTrack(json, artist));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return tracks;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exchanges album object for stat-updated album from Last.FM
|
* Exchanges album object for stat-updated album from Last.FM
|
||||||
*
|
*
|
||||||
|
@ -300,8 +300,7 @@ public class FmUserNetwork extends FmNetwork {
|
|||||||
|
|
||||||
if (tracks.size() < number) {
|
if (tracks.size() < number) {
|
||||||
Artist artist = new ArtistBuilder(json.getJSONObject("artist").getString("name")).build();
|
Artist artist = new ArtistBuilder(json.getJSONObject("artist").getString("name")).build();
|
||||||
Track track = new TrackBuilder(json.getString("name"), artist)
|
Track track = parseTrack(json, artist);
|
||||||
.setUserPlayCount(json.getInt("playcount")).build();
|
|
||||||
|
|
||||||
tracks.add(track);
|
tracks.add(track);
|
||||||
|
|
||||||
|
@ -5,7 +5,9 @@ import static org.junit.Assert.*;
|
|||||||
|
|
||||||
import sarsoo.fmframework.music.Album;
|
import sarsoo.fmframework.music.Album;
|
||||||
import sarsoo.fmframework.music.Artist;
|
import sarsoo.fmframework.music.Artist;
|
||||||
|
import sarsoo.fmframework.music.Artist.ArtistBuilder;
|
||||||
import sarsoo.fmframework.net.Key;
|
import sarsoo.fmframework.net.Key;
|
||||||
|
import sarsoo.fmframework.util.FMObjList;
|
||||||
|
|
||||||
public class FmNetworkTest {
|
public class FmNetworkTest {
|
||||||
|
|
||||||
@ -45,4 +47,15 @@ public class FmNetworkTest {
|
|||||||
assertTrue(true);
|
assertTrue(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testArtistTopTracks() {
|
||||||
|
FmNetwork network = new FmNetwork(Key.getKey());
|
||||||
|
|
||||||
|
FMObjList list = network.getArtistTopTracks(new ArtistBuilder("kendrick lamar").build(), 10);
|
||||||
|
|
||||||
|
list.stream().forEach(System.out::println);
|
||||||
|
|
||||||
|
assertTrue(true);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user