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)));
@ -271,9 +272,14 @@ 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);
Scrobble scrobble = new Scrobble(json.getJSONObject("date").getLong("uts"), track); 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"));
}
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,7 +3,7 @@ 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 {
/** /**
* *
@ -32,7 +32,6 @@ public class Artist extends FMObj implements Serializable{
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,8 +71,7 @@ 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;
@ -91,7 +89,6 @@ public static class ArtistBuilder{
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;
@ -148,4 +145,19 @@ public static class ArtistBuilder{
} }
} }
@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;
@ -73,4 +74,6 @@ 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);