diff --git a/src/main/java/sarsoo/fmframework/fm/FmNetwork.java b/src/main/java/sarsoo/fmframework/fm/FmNetwork.java index 1eef2b2..7914f6a 100644 --- a/src/main/java/sarsoo/fmframework/fm/FmNetwork.java +++ b/src/main/java/sarsoo/fmframework/fm/FmNetwork.java @@ -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) { diff --git a/src/main/java/sarsoo/fmframework/fm/FmUserNetwork.java b/src/main/java/sarsoo/fmframework/fm/FmUserNetwork.java index 4a865a4..aa12c60 100644 --- a/src/main/java/sarsoo/fmframework/fm/FmUserNetwork.java +++ b/src/main/java/sarsoo/fmframework/fm/FmUserNetwork.java @@ -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 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 scrobbles = new ArrayList(); + int counter; + for (counter = 0; counter < pages; counter++) { + + HashMap parameters = new HashMap(); + + 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 getTags() { - + Logger.getLog().log(new LogEntry("getTags")); HashMap parameters = new HashMap(); @@ -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 parameters = new HashMap(); diff --git a/src/main/java/sarsoo/fmframework/log/entry/ErrorEntry.java b/src/main/java/sarsoo/fmframework/log/entry/ErrorEntry.java index 87ec4bc..5394bf4 100644 --- a/src/main/java/sarsoo/fmframework/log/entry/ErrorEntry.java +++ b/src/main/java/sarsoo/fmframework/log/entry/ErrorEntry.java @@ -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) { diff --git a/src/main/java/sarsoo/fmframework/music/Scrobble.java b/src/main/java/sarsoo/fmframework/music/Scrobble.java index d7de257..0549766 100644 --- a/src/main/java/sarsoo/fmframework/music/Scrobble.java +++ b/src/main/java/sarsoo/fmframework/music/Scrobble.java @@ -17,5 +17,9 @@ public class Scrobble { public Track getTrack() { return track; } + + public String toString() { + return utc + " " + track.toString(); + } } diff --git a/src/main/java/sarsoo/fmframework/music/Track.java b/src/main/java/sarsoo/fmframework/music/Track.java index 5873622..cf25159 100644 --- a/src/main/java/sarsoo/fmframework/music/Track.java +++ b/src/main/java/sarsoo/fmframework/music/Track.java @@ -20,6 +20,7 @@ public class Track extends FMObj implements Serializable { protected int duration; protected boolean isLoved; protected ArrayList tagList; + protected ArrayList 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(); + } 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) { + scrobbles = scrobble; + } @Override public boolean equals(Object obj) { diff --git a/src/test/java/sarsoo/fmframework/fm/FmUserNetworkTest.java b/src/test/java/sarsoo/fmframework/fm/FmUserNetworkTest.java index 7ff3a51..3cd26a4 100644 --- a/src/test/java/sarsoo/fmframework/fm/FmUserNetworkTest.java +++ b/src/test/java/sarsoo/fmframework/fm/FmUserNetworkTest.java @@ -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 scrobbles = net.getRecentTracks(50); + scrobbles.stream().forEach(System.out::println); + System.out.println(scrobbles.size()); + assertNotNull(1); + } }