added scrobble and get recent tracks

This commit is contained in:
aj 2019-05-02 22:48:41 +01:00
parent 1e77a32b94
commit 26c9e2d17a
6 changed files with 88 additions and 7 deletions

View File

@ -267,7 +267,8 @@ public class FmNetwork {
try {
builder.setPlayCount(trackJson.getInt("playcount"));
} catch (JSONException e) {
log.logInfo(new InfoEntry("getTrack").addArg("no play count for").addArg(nameIn).addArg(e.getMessage()));
log.logInfo(
new InfoEntry("getTrack").addArg("no play count for").addArg(nameIn).addArg(e.getMessage()));
}
try {
@ -392,7 +393,9 @@ public class FmNetwork {
return new JSONObject(response.getBody().toString());
} else {
Logger.getLog().logError(new ErrorEntry("HTTP Get").setErrorCode(response.getStatus()));
JSONObject obj = new JSONObject(response.getBody().toString());
Logger.getLog().logError(new ErrorEntry("HTTP Get").setErrorCode(response.getStatus())
.addArg(Integer.toString(obj.getInt("error"))).addArg(obj.getString("message")));
return null;
}
} catch (UnirestException e) {

View File

@ -11,9 +11,14 @@ import org.json.JSONObject;
import sarsoo.fmframework.log.Logger;
import sarsoo.fmframework.log.entry.InfoEntry;
import sarsoo.fmframework.log.entry.LogEntry;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Album.AlbumBuilder;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Artist.ArtistBuilder;
import sarsoo.fmframework.music.Scrobble;
import sarsoo.fmframework.music.Tag;
import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.music.Track.TrackBuilder;
import sarsoo.fmframework.util.FMObjList;
public class FmUserNetwork extends FmNetwork {
@ -200,7 +205,7 @@ public class FmUserNetwork extends FmNetwork {
* @return Scrobble count
*/
public int getScrobbleCountByDeltaDay(int day) {
Logger.getLog().log(new LogEntry("getScrobblesByDeltaDay").addArg(Integer.toString(day)));
LocalDate local = LocalDate.now();
@ -225,13 +230,59 @@ public class FmUserNetwork extends FmNetwork {
}
public ArrayList<Scrobble> getRecentTracks(int 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;
}
ArrayList<Scrobble> scrobbles = new ArrayList<Scrobble>();
int counter;
for (counter = 0; counter < pages; counter++) {
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("user", userName);
parameters.put("limit", Integer.toString(limit));
parameters.put("page", Integer.toString(counter + 1));
JSONObject obj = makeGetRequest("user.getrecenttracks", parameters);
JSONArray tracks = obj.getJSONObject("recenttracks").getJSONArray("track");
for (int i = 0; i < tracks.length(); i++) {
JSONObject json = (JSONObject) tracks.get(i);
if (scrobbles.size() < number) {
Artist artist = new ArtistBuilder(json.getJSONObject("artist").getString("#text")).build();
Album album = new AlbumBuilder(json.getJSONObject("album").getString("#text"), artist).build();
Track track = new TrackBuilder(json.getString("name"), artist).build();
track.setAlbum(album);
Scrobble scrobble = new Scrobble(json.getJSONObject("date").getLong("uts"), track);
scrobbles.add(scrobble);
}
}
}
return scrobbles;
}
/**
* Returns list of user tags
*
* @return List of tags
*/
public ArrayList<Tag> getTags() {
Logger.getLog().log(new LogEntry("getTags"));
HashMap<String, String> parameters = new HashMap<String, String>();
@ -268,7 +319,7 @@ public class FmUserNetwork extends FmNetwork {
* @return FMObjList of artists
*/
public FMObjList getTag(String tagName) {
Logger.getLog().log(new LogEntry("getTag").addArg(tagName));
HashMap<String, String> parameters = new HashMap<String, String>();

View File

@ -4,7 +4,7 @@ import java.util.ArrayList;
public class ErrorEntry extends LogEntry {
protected int errorCode;
protected int errorCode = 0;
public ErrorEntry(String methodIn) {
super(methodIn);
@ -29,7 +29,7 @@ public class ErrorEntry extends LogEntry {
String logString = String.format("%s !!%s", timestamp, method);
if (errorCode != 0) {
logString += String.format(" (%i)", errorCode);
logString += String.format(" (%d)", errorCode);
}
if (args != null) {

View File

@ -17,5 +17,9 @@ public class Scrobble {
public Track getTrack() {
return track;
}
public String toString() {
return utc + " " + track.toString();
}
}

View File

@ -20,6 +20,7 @@ public class Track extends FMObj implements Serializable {
protected int duration;
protected boolean isLoved;
protected ArrayList<Tag> tagList;
protected ArrayList<Scrobble> scrobbles;
private int utsFirstListen;
@ -44,6 +45,8 @@ public class Track extends FMObj implements Serializable {
this.isLoved = builder.isLoved;
this.tagList = builder.tagList;
this.scrobbles = new ArrayList<Scrobble>();
}
public Artist getArtist() {
@ -73,6 +76,14 @@ public class Track extends FMObj implements Serializable {
public Album getAlbum() {
return album;
}
public void addScrobble(Scrobble scrobble) {
scrobbles.add(scrobble);
}
public void setScrobbles(ArrayList<Scrobble> scrobble) {
scrobbles = scrobble;
}
@Override
public boolean equals(Object obj) {

View File

@ -2,10 +2,13 @@ package sarsoo.fmframework.fm;
import org.junit.Test;
import sarsoo.fmframework.music.Scrobble;
import sarsoo.fmframework.net.Key;
import static org.junit.Assert.*;
import java.util.ArrayList;
public class FmUserNetworkTest {
@Test
@ -17,4 +20,13 @@ public class FmUserNetworkTest {
public void testGetLastTrack() {
assertNotNull(new FmUserNetwork(Key.getKey(), "sarsoo").getLastTrack());
}
@Test
public void testGetRecentTracks() {
FmUserNetwork net = new FmUserNetwork(Key.getKey(), "sarsoo");
ArrayList<Scrobble> scrobbles = net.getRecentTracks(50);
scrobbles.stream().forEach(System.out::println);
System.out.println(scrobbles.size());
assertNotNull(1);
}
}