added abstract getScrobbles method, fixed get recent scrobbles

This commit is contained in:
aj 2019-05-04 02:21:31 +01:00
parent f1f85ada87
commit b776e91436
7 changed files with 87 additions and 48 deletions

View File

@ -6,6 +6,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import sarsoo.fmframework.log.Logger; import sarsoo.fmframework.log.Logger;
@ -232,7 +233,7 @@ public class FmUserNetwork extends FmNetwork {
} }
public ArrayList<Scrobble> getRecentTracks(int number) { public ArrayList<Scrobble> getRecentScrobbles(int number) {
Logger.getLog().log(new LogEntry("getRecentTracks").addArg(Integer.toString(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 track = new TrackBuilder(json.getString("name"), artist).build();
track.setAlbum(album); 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);
} }
} }
} }

View File

@ -136,4 +136,18 @@ public class Album extends FMObj implements Serializable{
return new Album(this); return new Album(this);
} }
} }
@Override
public ArrayList<Scrobble> getScrobbles() {
if(trackList != null) {
if (trackList.size() > 0) {
ArrayList<Scrobble> scrobbles = new ArrayList<Scrobble>();
for (Track i: trackList) {
scrobbles.addAll(i.getScrobbles());
}
return scrobbles;
}
}
return null;
}
} }

View File

@ -3,8 +3,8 @@ package sarsoo.fmframework.music;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; 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) { private Artist(ArtistBuilder builder) {
this.name = builder.name; this.name = builder.name;
this.url = builder.url; this.url = builder.url;
this.listeners = builder.listeners; this.listeners = builder.listeners;
this.playCount = builder.playCount; this.playCount = builder.playCount;
this.userPlayCount = builder.userPlayCount; this.userPlayCount = builder.userPlayCount;
this.wiki = builder.wiki; this.wiki = builder.wiki;
this.mbid = builder.mbid; this.mbid = builder.mbid;
this.albums = builder.albums; this.albums = builder.albums;
this.similarArtists = builder.similarArtists; this.similarArtists = builder.similarArtists;
this.tagList = builder.tagList; this.tagList = builder.tagList;
} }
public ArrayList<Album> getAlbum() { public ArrayList<Album> getAlbum() {
@ -72,80 +71,93 @@ public class Artist extends FMObj implements Serializable{
return "Artist: " + name; return "Artist: " + name;
} }
public static class ArtistBuilder {
public static class ArtistBuilder{
protected String name; protected String name;
protected String url; protected String url;
protected int listeners; protected int listeners;
protected int playCount; protected int playCount;
protected int userPlayCount; protected int userPlayCount;
protected Wiki wiki; protected Wiki wiki;
protected String mbid; protected String mbid;
protected ArrayList<Album> albums; protected ArrayList<Album> albums;
protected ArrayList<Artist> similarArtists; protected ArrayList<Artist> similarArtists;
protected ArrayList<Tag> tagList; protected ArrayList<Tag> tagList;
public ArtistBuilder(String name) { public ArtistBuilder(String name) {
this.name = name; this.name = name;
} }
public ArtistBuilder setUrl(String url) { public ArtistBuilder setUrl(String url) {
this.url = url; this.url = url;
return this; return this;
} }
public ArtistBuilder setListeners(int listeners) { public ArtistBuilder setListeners(int listeners) {
this.listeners = listeners; this.listeners = listeners;
return this; return this;
} }
public ArtistBuilder setPlayCount(int playCount) { public ArtistBuilder setPlayCount(int playCount) {
this.playCount = playCount; this.playCount = playCount;
return this; return this;
} }
public ArtistBuilder setUserPlayCount(int userPlayCount) { public ArtistBuilder setUserPlayCount(int userPlayCount) {
this.userPlayCount = userPlayCount; this.userPlayCount = userPlayCount;
return this; return this;
} }
public ArtistBuilder setWiki(Wiki wiki) { public ArtistBuilder setWiki(Wiki wiki) {
this.wiki = wiki; this.wiki = wiki;
return this; return this;
} }
public ArtistBuilder setMbid(String Mbid) { public ArtistBuilder setMbid(String Mbid) {
this.mbid = Mbid; this.mbid = Mbid;
return this; return this;
} }
public ArtistBuilder setAlbums(ArrayList<Album> albums) { public ArtistBuilder setAlbums(ArrayList<Album> albums) {
this.albums = albums; this.albums = albums;
return this; return this;
} }
public ArtistBuilder setSimilarArtists(ArrayList<Artist> similarArtists) { public ArtistBuilder setSimilarArtists(ArrayList<Artist> similarArtists) {
this.similarArtists = similarArtists; this.similarArtists = similarArtists;
return this; return this;
} }
public ArtistBuilder setTagList(ArrayList<Tag> tagList) { public ArtistBuilder setTagList(ArrayList<Tag> tagList) {
this.tagList = tagList; this.tagList = tagList;
return this; return this;
} }
public Artist build() { public Artist build() {
return new Artist(this); return new Artist(this);
} }
} }
@Override
public ArrayList<Scrobble> getScrobbles() {
if (albums != null) {
if (albums.size() > 0) {
ArrayList<Scrobble> scrobbles = new ArrayList<Scrobble>();
for (Album i : albums) {
scrobbles.addAll(i.getScrobbles());
}
return scrobbles;
}
}
return null;
}
} }

View File

@ -1,6 +1,7 @@
package sarsoo.fmframework.music; package sarsoo.fmframework.music;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import sarsoo.fmframework.fm.FmUserNetwork; import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.net.Key; import sarsoo.fmframework.net.Key;
@ -72,5 +73,7 @@ public abstract class FMObj implements Comparable<FMObj>, Serializable{
} }
abstract public String getMusicBrainzURL(); abstract public String getMusicBrainzURL();
public abstract ArrayList<Scrobble> getScrobbles();
} }

View File

@ -1,17 +1,21 @@
package sarsoo.fmframework.music; package sarsoo.fmframework.music;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class Scrobble { public class Scrobble {
private long utc; private LocalDateTime dateTime;
private Track track; private Track track;
public Scrobble(long utc, Track track) { public Scrobble(long uts, Track track) {
this.utc = utc; this.track = track;
this.track = track; this.dateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(uts), ZoneId.systemDefault());
} }
public long getUTC() { public LocalDateTime getDateTime() {
return utc; return dateTime;
} }
public Track getTrack() { public Track getTrack() {
@ -19,7 +23,7 @@ public class Scrobble {
} }
public String toString() { public String toString() {
return utc + " " + track.toString(); return dateTime + " " + track.toString();
} }
} }

View File

@ -3,11 +3,6 @@ package sarsoo.fmframework.music;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; 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 { public class Track extends FMObj implements Serializable {
/** /**
@ -213,4 +208,9 @@ public class Track extends FMObj implements Serializable {
return new Track(this); return new Track(this);
} }
} }
@Override
public ArrayList<Scrobble> getScrobbles() {
return scrobbles;
}
} }

View File

@ -24,7 +24,7 @@ public class FmUserNetworkTest {
@Test @Test
public void testGetRecentTracks() { public void testGetRecentTracks() {
FmUserNetwork net = new FmUserNetwork(Key.getKey(), "sarsoo"); FmUserNetwork net = new FmUserNetwork(Key.getKey(), "sarsoo");
ArrayList<Scrobble> scrobbles = net.getRecentTracks(50); ArrayList<Scrobble> scrobbles = net.getRecentScrobbles(50);
scrobbles.stream().forEach(System.out::println); scrobbles.stream().forEach(System.out::println);
System.out.println(scrobbles.size()); System.out.println(scrobbles.size());
assertNotNull(1); assertNotNull(1);