diff --git a/src/main/java/sarsoo/fmframework/fm/FmUserNetwork.java b/src/main/java/sarsoo/fmframework/fm/FmUserNetwork.java index e494794..4fbcc82 100644 --- a/src/main/java/sarsoo/fmframework/fm/FmUserNetwork.java +++ b/src/main/java/sarsoo/fmframework/fm/FmUserNetwork.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.HashMap; import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; import sarsoo.fmframework.log.Logger; @@ -232,7 +233,7 @@ public class FmUserNetwork extends FmNetwork { } - public ArrayList getRecentTracks(int number) { + public ArrayList getRecentScrobbles(int number) { Logger.getLog().log(new LogEntry("getRecentTracks").addArg(Integer.toString(number))); @@ -270,10 +271,15 @@ public class FmUserNetwork extends FmNetwork { Track track = new TrackBuilder(json.getString("name"), artist).build(); track.setAlbum(album); + + try { + Scrobble scrobble = new Scrobble(json.getJSONObject("date").getLong("uts"), track); + scrobbles.add(scrobble); + }catch(JSONException e){ + Logger.getLog().logInfo(new InfoEntry("getRecentTracks").addArg("first track")); - Scrobble scrobble = new Scrobble(json.getJSONObject("date").getLong("uts"), track); - - scrobbles.add(scrobble); + } + } } } diff --git a/src/main/java/sarsoo/fmframework/music/Album.java b/src/main/java/sarsoo/fmframework/music/Album.java index 0392c80..15bd309 100644 --- a/src/main/java/sarsoo/fmframework/music/Album.java +++ b/src/main/java/sarsoo/fmframework/music/Album.java @@ -136,4 +136,18 @@ public class Album extends FMObj implements Serializable{ return new Album(this); } } + + @Override + public ArrayList getScrobbles() { + if(trackList != null) { + if (trackList.size() > 0) { + ArrayList scrobbles = new ArrayList(); + for (Track i: trackList) { + scrobbles.addAll(i.getScrobbles()); + } + return scrobbles; + } + } + return null; + } } diff --git a/src/main/java/sarsoo/fmframework/music/Artist.java b/src/main/java/sarsoo/fmframework/music/Artist.java index 41d2c63..8ade396 100644 --- a/src/main/java/sarsoo/fmframework/music/Artist.java +++ b/src/main/java/sarsoo/fmframework/music/Artist.java @@ -3,8 +3,8 @@ package sarsoo.fmframework.music; import java.io.Serializable; import java.util.ArrayList; -public class Artist extends FMObj implements Serializable{ - +public class Artist extends FMObj implements Serializable { + /** * */ @@ -17,22 +17,21 @@ public class Artist extends FMObj implements Serializable{ private Artist(ArtistBuilder builder) { this.name = builder.name; - + this.url = builder.url; - + this.listeners = builder.listeners; this.playCount = builder.playCount; this.userPlayCount = builder.userPlayCount; - + this.wiki = builder.wiki; - + this.mbid = builder.mbid; - + this.albums = builder.albums; this.similarArtists = builder.similarArtists; this.tagList = builder.tagList; - - + } public ArrayList getAlbum() { @@ -72,80 +71,93 @@ public class Artist extends FMObj implements Serializable{ return "Artist: " + name; } - -public static class ArtistBuilder{ - + public static class ArtistBuilder { + protected String name; - + protected String url; - + protected int listeners; protected int playCount; protected int userPlayCount; - + protected Wiki wiki; - + protected String mbid; - + protected ArrayList albums; protected ArrayList similarArtists; protected ArrayList tagList; - - + public ArtistBuilder(String name) { - + this.name = name; - + } - + public ArtistBuilder setUrl(String url) { this.url = url; return this; } - + public ArtistBuilder setListeners(int listeners) { this.listeners = listeners; return this; } - + public ArtistBuilder setPlayCount(int playCount) { this.playCount = playCount; return this; } - + public ArtistBuilder setUserPlayCount(int userPlayCount) { this.userPlayCount = userPlayCount; return this; } - + public ArtistBuilder setWiki(Wiki wiki) { this.wiki = wiki; return this; } - + public ArtistBuilder setMbid(String Mbid) { this.mbid = Mbid; return this; } - + public ArtistBuilder setAlbums(ArrayList albums) { this.albums = albums; return this; } - + public ArtistBuilder setSimilarArtists(ArrayList similarArtists) { this.similarArtists = similarArtists; return this; } - + public ArtistBuilder setTagList(ArrayList tagList) { this.tagList = tagList; return this; } - + public Artist build() { return new Artist(this); } } + @Override + public ArrayList getScrobbles() { + if (albums != null) { + if (albums.size() > 0) { + ArrayList scrobbles = new ArrayList(); + for (Album i : albums) { + scrobbles.addAll(i.getScrobbles()); + } + return scrobbles; + } + } + + return null; + } + } diff --git a/src/main/java/sarsoo/fmframework/music/FMObj.java b/src/main/java/sarsoo/fmframework/music/FMObj.java index 4c35b68..c6c3512 100644 --- a/src/main/java/sarsoo/fmframework/music/FMObj.java +++ b/src/main/java/sarsoo/fmframework/music/FMObj.java @@ -1,6 +1,7 @@ package sarsoo.fmframework.music; import java.io.Serializable; +import java.util.ArrayList; import sarsoo.fmframework.fm.FmUserNetwork; import sarsoo.fmframework.net.Key; @@ -72,5 +73,7 @@ public abstract class FMObj implements Comparable, Serializable{ } abstract public String getMusicBrainzURL(); + + public abstract ArrayList getScrobbles(); } diff --git a/src/main/java/sarsoo/fmframework/music/Scrobble.java b/src/main/java/sarsoo/fmframework/music/Scrobble.java index 0549766..493b204 100644 --- a/src/main/java/sarsoo/fmframework/music/Scrobble.java +++ b/src/main/java/sarsoo/fmframework/music/Scrobble.java @@ -1,17 +1,21 @@ package sarsoo.fmframework.music; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; + public class Scrobble { - private long utc; + private LocalDateTime dateTime; private Track track; - public Scrobble(long utc, Track track) { - this.utc = utc; - this.track = track; + public Scrobble(long uts, Track track) { + this.track = track; + this.dateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(uts), ZoneId.systemDefault()); } - public long getUTC() { - return utc; + public LocalDateTime getDateTime() { + return dateTime; } public Track getTrack() { @@ -19,7 +23,7 @@ public class Scrobble { } public String toString() { - return utc + " " + track.toString(); + return dateTime + " " + track.toString(); } } diff --git a/src/main/java/sarsoo/fmframework/music/Track.java b/src/main/java/sarsoo/fmframework/music/Track.java index cf25159..f3c0b8b 100644 --- a/src/main/java/sarsoo/fmframework/music/Track.java +++ b/src/main/java/sarsoo/fmframework/music/Track.java @@ -3,11 +3,6 @@ package sarsoo.fmframework.music; import java.io.Serializable; import java.util.ArrayList; -import org.w3c.dom.Document; - -import sarsoo.fmframework.net.Network; -import sarsoo.fmframework.util.Reference; - public class Track extends FMObj implements Serializable { /** @@ -213,4 +208,9 @@ public class Track extends FMObj implements Serializable { return new Track(this); } } + + @Override + public ArrayList getScrobbles() { + return scrobbles; + } } diff --git a/src/test/java/sarsoo/fmframework/fm/FmUserNetworkTest.java b/src/test/java/sarsoo/fmframework/fm/FmUserNetworkTest.java index 3cd26a4..bd4edcc 100644 --- a/src/test/java/sarsoo/fmframework/fm/FmUserNetworkTest.java +++ b/src/test/java/sarsoo/fmframework/fm/FmUserNetworkTest.java @@ -24,7 +24,7 @@ public class FmUserNetworkTest { @Test public void testGetRecentTracks() { FmUserNetwork net = new FmUserNetwork(Key.getKey(), "sarsoo"); - ArrayList scrobbles = net.getRecentTracks(50); + ArrayList scrobbles = net.getRecentScrobbles(50); scrobbles.stream().forEach(System.out::println); System.out.println(scrobbles.size()); assertNotNull(1);