utilising api call exception

This commit is contained in:
aj 2019-05-25 12:54:18 +01:00
parent a78f520f2f
commit d0671eb970
21 changed files with 420 additions and 289 deletions

View File

@ -1,5 +1,6 @@
package sarsoo.fmframework.cache.puller; package sarsoo.fmframework.cache.puller;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmNetwork; import sarsoo.fmframework.fm.FmNetwork;
import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Album;
@ -12,7 +13,11 @@ public class AlbumPuller implements Puller<Album, Album> {
} }
public Album pull(Album album) { public Album pull(Album album) {
return net.refresh(album); try {
return net.refresh(album);
} catch (ApiCallException e) {}
return null;
} }
} }

View File

@ -1,5 +1,6 @@
package sarsoo.fmframework.cache.puller; package sarsoo.fmframework.cache.puller;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmNetwork; import sarsoo.fmframework.fm.FmNetwork;
import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist; import sarsoo.fmframework.music.Artist;
@ -17,7 +18,11 @@ public class ArtistPuller implements Puller<Artist, Artist> {
} }
public Artist pull(Artist artist) { public Artist pull(Artist artist) {
return net.refresh(artist); try {
return net.refresh(artist);
} catch (ApiCallException e) {}
return null;
} }
} }

View File

@ -1,5 +1,6 @@
package sarsoo.fmframework.cache.puller; package sarsoo.fmframework.cache.puller;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmUserNetwork; import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.util.FMObjList; import sarsoo.fmframework.util.FMObjList;
@ -12,7 +13,11 @@ public class ArtistTagPuller implements Puller<FMObjList, String> {
} }
public FMObjList pull(String name) { public FMObjList pull(String name) {
return net.getPopulatedArtistTag(name); try {
return net.getPopulatedArtistTag(name);
} catch (ApiCallException e) {}
return null;
} }
} }

View File

@ -1,6 +1,7 @@
package sarsoo.fmframework.cache.puller; package sarsoo.fmframework.cache.puller;
import sarsoo.fmframework.cache.StaticCache; import sarsoo.fmframework.cache.StaticCache;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmUserNetwork; import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.music.Artist; import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.util.FMObjList; import sarsoo.fmframework.util.FMObjList;
@ -16,16 +17,22 @@ public class CachedArtistTagPuller implements Puller<FMObjList, String> {
} }
public FMObjList pull(String name) { public FMObjList pull(String name) {
FMObjList list = net.getArtistTag(name); FMObjList list;
try {
list = net.getArtistTag(name);
FMObjList returned = new FMObjList();
returned.setGroupName(list.getGroupName());
for(int i = 0; i < list.size(); i++) {
returned.add(artistPool.get((Artist) list.get(i)));
}
return returned;
} catch (ApiCallException e) {}
FMObjList returned = new FMObjList(); return null;
returned.setGroupName(list.getGroupName());
for(int i = 0; i < list.size(); i++) {
returned.add(artistPool.get((Artist) list.get(i)));
}
return returned;
} }
} }

View File

@ -1,5 +1,6 @@
package sarsoo.fmframework.cache.puller; package sarsoo.fmframework.cache.puller;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmNetwork; import sarsoo.fmframework.fm.FmNetwork;
import sarsoo.fmframework.music.Track; import sarsoo.fmframework.music.Track;
@ -12,7 +13,11 @@ public class TrackPuller implements Puller<Track, Track> {
} }
public Track pull(Track track) { public Track pull(Track track) {
return net.refresh(track); try {
return net.refresh(track);
} catch (ApiCallException e) {}
return null;
} }
} }

View File

@ -1,52 +1,48 @@
package sarsoo.fmframework.error; package sarsoo.fmframework.error;
import sarsoo.fmframework.log.Log;
import sarsoo.fmframework.log.Logger;
import sarsoo.fmframework.log.entry.ErrorEntry;
public class ApiCallException extends Exception { public class ApiCallException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
private int failureCode; private int failureCode;
private String failureString;
private String method;
public ApiCallException(int failureCode) { public ApiCallException(String method, int failureCode, String failureDescription) {
this.failureCode = failureCode; this.failureCode = failureCode;
this.failureString = failureDescription;
this.method = method;
Logger.getLog().logError(getLogEntry());
}
public ApiCallException(String method, int failureCode, String failureDescription, Log log) {
this.method = method;
this.failureCode = failureCode;
this.failureString = failureDescription;
log.logError(getLogEntry());
}
private ErrorEntry getLogEntry() {
return new ErrorEntry("ApiCallException").addArg(method).addArg(Integer.toString(failureCode)).addArg(failureString);
}
public String getCauseMethod() {
return method;
} }
public int getFailureCode() { public int getFailureCode() {
return failureCode; return failureCode;
} }
public String getError() { public String getFailureMessage() {
switch(failureCode) { return failureString;
case 2:
return "Invalid service - This service does not exist";
case 3:
return "Invalid Method - No method with that name in this package";
case 4:
return "Authentication Failed - You do not have permissions to access the service";
case 5:
return "Invalid format - This service doesn't exist in that format";
case 6:
return "Invalid parameters - Your request is missing a required parameter";
case 7:
return "Invalid resource specified";
case 8:
return "Operation failed - Something else went wrong";
case 9:
return "Invalid session key - Please re-authenticate";
case 10:
return "Invalid API key - You must be granted a valid key by last.fm";
case 11:
return "Service Offline - This service is temporarily offline. Try again later.";
case 13:
return "Invalid method signature supplied";
case 16:
return "There was a temporary error processing your request. Please try again";
case 26:
return "Suspended API key - Access for your account has been suspended, please contact Last.fm";
case 29:
return "Rate limit exceeded - Your IP has made too many requests in a short period";
case 400:
return "Bad Request";
default:
return null;
}
} }
} }

View File

@ -9,6 +9,7 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.TreeMap; import java.util.TreeMap;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.HttpResponse;
@ -18,6 +19,7 @@ import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.HttpRequest; import com.mashape.unirest.request.HttpRequest;
import com.mashape.unirest.request.HttpRequestWithBody; import com.mashape.unirest.request.HttpRequestWithBody;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.log.Logger; import sarsoo.fmframework.log.Logger;
import sarsoo.fmframework.log.entry.ErrorEntry; import sarsoo.fmframework.log.entry.ErrorEntry;
import sarsoo.fmframework.log.entry.LogEntry; import sarsoo.fmframework.log.entry.LogEntry;
@ -32,7 +34,7 @@ public class FmAuthNetwork extends FmUserNetwork {
this.secretKey = secretKey; this.secretKey = secretKey;
} }
public JSONObject scrobble(Scrobble scrobble, String sk) { public JSONObject scrobble(Scrobble scrobble, String sk) throws ApiCallException {
Logger.getLog().log(new LogEntry("scrobble").addArg(scrobble.toString())); Logger.getLog().log(new LogEntry("scrobble").addArg(scrobble.toString()));
@ -54,7 +56,7 @@ public class FmAuthNetwork extends FmUserNetwork {
} }
public String getToken() { public String getToken() throws ApiCallException {
Logger.getLog().log(new LogEntry("getToken")); Logger.getLog().log(new LogEntry("getToken"));
@ -63,7 +65,7 @@ public class FmAuthNetwork extends FmUserNetwork {
return obj.getString("token"); return obj.getString("token");
} }
public String getSession(String token) { public String getSession(String token) throws ApiCallException {
Logger.getLog().log(new LogEntry("getSession")); Logger.getLog().log(new LogEntry("getSession"));
@ -117,20 +119,20 @@ public class FmAuthNetwork extends FmUserNetwork {
return null; return null;
} }
protected JSONObject makeAuthGetRequest(String method) { protected JSONObject makeAuthGetRequest(String method) throws ApiCallException {
return makeAuthGetRequest(method, new HashMap<String, String>(), null); return makeAuthGetRequest(method, new HashMap<String, String>(), null);
} }
protected JSONObject makeAuthGetRequest(String method, HashMap<String, String> parameters) { protected JSONObject makeAuthGetRequest(String method, HashMap<String, String> parameters) throws ApiCallException {
return makeAuthGetRequest(method, parameters, null); return makeAuthGetRequest(method, parameters, null);
} }
protected JSONObject makeAuthGetRequest(String method, HashMap<String, String> parameters, protected JSONObject makeAuthGetRequest(String method, HashMap<String, String> parameters,
HashMap<String, String> headers) { HashMap<String, String> headers) throws ApiCallException {
HttpRequest request; HttpRequest request;
try { try {
@ -163,9 +165,7 @@ public class FmAuthNetwork extends FmUserNetwork {
} else { } else {
JSONObject obj = new JSONObject(response.getBody().toString()); JSONObject obj = new JSONObject(response.getBody().toString());
Logger.getLog().logError(new ErrorEntry("HTTP Get").setErrorCode(response.getStatus()) throw new ApiCallException(method, obj.getInt("error"), obj.getString("message"));
.addArg(Integer.toString(obj.getInt("error"))).addArg(obj.getString("message")));
return null;
} }
} catch (UnirestException e) { } catch (UnirestException e) {
e.printStackTrace(); e.printStackTrace();
@ -174,20 +174,20 @@ public class FmAuthNetwork extends FmUserNetwork {
return null; return null;
} }
protected JSONObject makeAuthPostRequest(String method) { protected JSONObject makeAuthPostRequest(String method) throws ApiCallException {
return makeAuthPostRequest(method, new HashMap<String, String>(), null); return makeAuthPostRequest(method, new HashMap<String, String>(), null);
} }
protected JSONObject makeAuthPostRequest(String method, HashMap<String, String> parameters) { protected JSONObject makeAuthPostRequest(String method, HashMap<String, String> parameters) throws ApiCallException {
return makeAuthPostRequest(method, parameters, null); return makeAuthPostRequest(method, parameters, null);
} }
protected JSONObject makeAuthPostRequest(String method, HashMap<String, String> parameters, protected JSONObject makeAuthPostRequest(String method, HashMap<String, String> parameters,
HashMap<String, String> headers) { HashMap<String, String> headers) throws ApiCallException {
HttpRequestWithBody request; HttpRequestWithBody request;
try { try {
@ -218,15 +218,14 @@ public class FmAuthNetwork extends FmUserNetwork {
HttpResponse<JsonNode> response = request.asJson(); HttpResponse<JsonNode> response = request.asJson();
if (response.getStatus() == 200) { if (response.getStatus() >= 200 && response.getStatus() < 300) {
return new JSONObject(response.getBody().toString()); return new JSONObject(response.getBody().toString());
} else { } else {
JSONObject obj = new JSONObject(response.getBody().toString()); JSONObject obj = new JSONObject(response.getBody().toString());
Logger.getLog().logError(new ErrorEntry("HTTP post").setErrorCode(response.getStatus())
.addArg(Integer.toString(obj.getInt("error"))).addArg(obj.getString("message"))); throw new ApiCallException(method, obj.getInt("error"), obj.getString("message"));
return null;
} }
} catch (UnirestException e) { } catch (UnirestException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -1,5 +1,6 @@
package sarsoo.fmframework.fm; package sarsoo.fmframework.fm;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.log.Log; import sarsoo.fmframework.log.Log;
import sarsoo.fmframework.log.Logger; import sarsoo.fmframework.log.Logger;
import sarsoo.fmframework.log.entry.ErrorEntry; import sarsoo.fmframework.log.entry.ErrorEntry;
@ -41,7 +42,7 @@ public class FmNetwork {
this.key = key; this.key = key;
} }
public Album getAlbum(String name, String artist) { public Album getAlbum(String name, String artist) throws ApiCallException {
return getAlbum(name, getArtist(artist)); return getAlbum(name, getArtist(artist));
} }
@ -51,8 +52,10 @@ public class FmNetwork {
* @param name Album Name * @param name Album Name
* @param artist Artist Name * @param artist Artist Name
* @return Album * @return Album
* @throws ApiCallException
* @throws JSONException
*/ */
public Album getAlbum(String name, Artist artist) { public Album getAlbum(String name, Artist artist) throws ApiCallException {
Log log = Logger.getLog(); Log log = Logger.getLog();
log.log(new LogEntry("getAlbum").addArg(name).addArg(artist.getName())); log.log(new LogEntry("getAlbum").addArg(name).addArg(artist.getName()));
@ -136,8 +139,10 @@ public class FmNetwork {
* *
* @param name Artist Name * @param name Artist Name
* @return Artist * @return Artist
* @throws ApiCallException
* @throws JSONException
*/ */
public Artist getArtist(String name) { public Artist getArtist(String name) throws ApiCallException {
Log log = Logger.getLog(); Log log = Logger.getLog();
log.log(new LogEntry("getArtist").addArg(name)); log.log(new LogEntry("getArtist").addArg(name));
@ -216,7 +221,7 @@ public class FmNetwork {
} }
public Track getTrack(String name, String artist) { public Track getTrack(String name, String artist) throws ApiCallException {
return getTrack(name, getArtist(artist)); return getTrack(name, getArtist(artist));
} }
@ -226,8 +231,10 @@ public class FmNetwork {
* @param name Track Name * @param name Track Name
* @param artist Artist Name * @param artist Artist Name
* @return Track * @return Track
* @throws ApiCallException
* @throws JSONException
*/ */
public Track getTrack(String name, Artist artist) { public Track getTrack(String name, Artist artist) throws ApiCallException {
Log log = Logger.getLog(); Log log = Logger.getLog();
log.log(new LogEntry("getTrack").addArg(name).addArg(artist.getName())); log.log(new LogEntry("getTrack").addArg(name).addArg(artist.getName()));
@ -314,7 +321,7 @@ public class FmNetwork {
return null; return null;
} }
public FMObjList getArtistTopTracks(Artist artist, int number) { public FMObjList getArtistTopTracks(Artist artist, int number) throws ApiCallException {
Logger.getLog() Logger.getLog()
.log(new LogEntry("getArtistTopTracks").addArg(artist.getName()).addArg(Integer.toString(number))); .log(new LogEntry("getArtistTopTracks").addArg(artist.getName()).addArg(Integer.toString(number)));
@ -364,8 +371,10 @@ public class FmNetwork {
* *
* @param album Old Album Object * @param album Old Album Object
* @return Refreshed Album * @return Refreshed Album
* @throws ApiCallException
* @throws JSONException
*/ */
public Album refresh(Album album) { public Album refresh(Album album) throws ApiCallException {
Logger.getLog().log(new LogEntry("refreshAlbum").addArg(album.getName()).addArg(album.getArtist().getName())); Logger.getLog().log(new LogEntry("refreshAlbum").addArg(album.getName()).addArg(album.getArtist().getName()));
@ -377,8 +386,10 @@ public class FmNetwork {
* *
* @param artist Old Artist Object * @param artist Old Artist Object
* @return Refreshed Artist * @return Refreshed Artist
* @throws ApiCallException
* @throws JSONException
*/ */
public Artist refresh(Artist artist) { public Artist refresh(Artist artist) throws ApiCallException {
Logger.getLog().log(new LogEntry("refreshArtist").addArg(artist.getName())); Logger.getLog().log(new LogEntry("refreshArtist").addArg(artist.getName()));
@ -390,8 +401,10 @@ public class FmNetwork {
* *
* @param track Old Track Object * @param track Old Track Object
* @return Refreshed Track * @return Refreshed Track
* @throws ApiCallException
* @throws JSONException
*/ */
public Track refresh(Track track) { public Track refresh(Track track) throws ApiCallException {
Logger.getLog().log(new LogEntry("refreshTrack").addArg(track.getName()).addArg(track.getArtist().getName())); Logger.getLog().log(new LogEntry("refreshTrack").addArg(track.getName()).addArg(track.getArtist().getName()));
@ -410,8 +423,10 @@ public class FmNetwork {
* *
* @param obj FMObj for refreshing * @param obj FMObj for refreshing
* @return Updated FMObj * @return Updated FMObj
* @throws ApiCallException
* @throws JSONException
*/ */
public FMObj refresh(FMObj obj) { public FMObj refresh(FMObj obj) throws ApiCallException {
if (obj.getClass() == Track.class) if (obj.getClass() == Track.class)
return refresh((Track) obj); return refresh((Track) obj);
if (obj.getClass() == Album.class) if (obj.getClass() == Album.class)
@ -421,14 +436,14 @@ public class FmNetwork {
return null; return null;
} }
protected JSONObject makeGetRequest(String method, HashMap<String, String> parameters) { protected JSONObject makeGetRequest(String method, HashMap<String, String> parameters) throws ApiCallException {
return makeGetRequest(method, parameters, null); return makeGetRequest(method, parameters, null);
} }
protected JSONObject makeGetRequest(String method, HashMap<String, String> parameters, protected JSONObject makeGetRequest(String method, HashMap<String, String> parameters,
HashMap<String, String> headers) { HashMap<String, String> headers) throws ApiCallException {
HttpRequest request; HttpRequest request;
try { try {
@ -453,15 +468,14 @@ public class FmNetwork {
HttpResponse<JsonNode> response = request.asJson(); HttpResponse<JsonNode> response = request.asJson();
if (response.getStatus() == 200) { if (response.getStatus() >= 200 && response.getStatus() < 300) {
return new JSONObject(response.getBody().toString()); return new JSONObject(response.getBody().toString());
} else { } else {
JSONObject obj = new JSONObject(response.getBody().toString()); 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"))); throw new ApiCallException(method, obj.getInt("error"), obj.getString("message"));
return null;
} }
} catch (UnirestException e) { } catch (UnirestException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -11,6 +11,7 @@ import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.log.Logger; import sarsoo.fmframework.log.Logger;
import sarsoo.fmframework.log.entry.ErrorEntry; import sarsoo.fmframework.log.entry.ErrorEntry;
import sarsoo.fmframework.log.entry.InfoEntry; import sarsoo.fmframework.log.entry.InfoEntry;
@ -72,8 +73,10 @@ public class FmUserNetwork extends FmNetwork {
* Return user object from Last.FM * Return user object from Last.FM
* *
* @return User * @return User
* @throws ApiCallException
* @throws JSONException
*/ */
public User getUser() { public User getUser() throws ApiCallException {
Logger.getLog().log(new LogEntry("getUser")); Logger.getLog().log(new LogEntry("getUser"));
@ -93,8 +96,10 @@ public class FmUserNetwork extends FmNetwork {
* Return user real name * Return user real name
* *
* @return User real name * @return User real name
* @throws ApiCallException
* @throws JSONException
*/ */
public String getUserRealName() { public String getUserRealName() throws ApiCallException {
Logger.getLog().log(new LogEntry("getUserRealName")); Logger.getLog().log(new LogEntry("getUserRealName"));
@ -105,8 +110,10 @@ public class FmUserNetwork extends FmNetwork {
* Return user's total scrobble count * Return user's total scrobble count
* *
* @return Total scrobble count * @return Total scrobble count
* @throws ApiCallException
* @throws JSONException
*/ */
public int getUserScrobbleCount() { public int getUserScrobbleCount() throws ApiCallException {
Logger.getLog().log(new LogEntry("getUserScrobbleCount")); Logger.getLog().log(new LogEntry("getUserScrobbleCount"));
@ -117,8 +124,10 @@ public class FmUserNetwork extends FmNetwork {
* Returns last or currently listening track * Returns last or currently listening track
* *
* @return Last track * @return Last track
* @throws ApiCallException
* @throws JSONException
*/ */
public Track getLastTrack() { public Track getLastTrack() throws ApiCallException {
Logger.getLog().log(new LogEntry("getLastTrack")); Logger.getLog().log(new LogEntry("getLastTrack"));
@ -145,8 +154,10 @@ public class FmUserNetwork extends FmNetwork {
* Return scrobble count from today * Return scrobble count from today
* *
* @return Scrobble count today * @return Scrobble count today
* @throws ApiCallException
* @throws JSONException
*/ */
public int getScrobblesToday() { public int getScrobblesToday() throws ApiCallException {
Logger.getLog().log(new LogEntry("getScrobblesToday")); Logger.getLog().log(new LogEntry("getScrobblesToday"));
@ -176,8 +187,10 @@ public class FmUserNetwork extends FmNetwork {
* @param month Month int * @param month Month int
* @param year Year int * @param year Year int
* @return Scrobble count * @return Scrobble count
* @throws ApiCallException
* @throws JSONException
*/ */
public int getScrobbleCountByDate(int day, int month, int year) { public int getScrobbleCountByDate(int day, int month, int year) throws ApiCallException {
Logger.getLog().log(new LogEntry("getScrobblesByDate").addArg(Integer.toString(day)) Logger.getLog().log(new LogEntry("getScrobblesByDate").addArg(Integer.toString(day))
.addArg(Integer.toString(month)).addArg(Integer.toString(year))); .addArg(Integer.toString(month)).addArg(Integer.toString(year)));
@ -208,8 +221,10 @@ public class FmUserNetwork extends FmNetwork {
* *
* @param day Negative day offset * @param day Negative day offset
* @return Scrobble count * @return Scrobble count
* @throws ApiCallException
* @throws JSONException
*/ */
public int getScrobbleCountByDeltaDay(int day) { public int getScrobbleCountByDeltaDay(int day) throws ApiCallException {
Logger.getLog().log(new LogEntry("getScrobblesByDeltaDay").addArg(Integer.toString(day))); Logger.getLog().log(new LogEntry("getScrobblesByDeltaDay").addArg(Integer.toString(day)));
@ -235,7 +250,7 @@ public class FmUserNetwork extends FmNetwork {
} }
public LocalDateTime getFirstScrobbleDateTime() { public LocalDateTime getFirstScrobbleDateTime() throws ApiCallException {
Logger.getLog().log(new LogEntry("getFirstScrobbleDates")); Logger.getLog().log(new LogEntry("getFirstScrobbleDates"));
@ -265,7 +280,7 @@ public class FmUserNetwork extends FmNetwork {
return LocalDateTime.ofInstant(Instant.ofEpochSecond(uts), ZoneId.systemDefault()); return LocalDateTime.ofInstant(Instant.ofEpochSecond(uts), ZoneId.systemDefault());
} }
public FMObjList getTopTracks(String period, int number) { public FMObjList getTopTracks(String period, int number) throws ApiCallException {
Logger.getLog().log(new LogEntry("getTopTracks").addArg(period).addArg(Integer.toString(number))); Logger.getLog().log(new LogEntry("getTopTracks").addArg(period).addArg(Integer.toString(number)));
@ -311,7 +326,7 @@ public class FmUserNetwork extends FmNetwork {
return tracks; return tracks;
} }
public FMObjList getTopAlbums(String period, int number) { public FMObjList getTopAlbums(String period, int number) throws ApiCallException {
Logger.getLog().log(new LogEntry("getTopAlbums").addArg(period).addArg(Integer.toString(number))); Logger.getLog().log(new LogEntry("getTopAlbums").addArg(period).addArg(Integer.toString(number)));
@ -358,7 +373,7 @@ public class FmUserNetwork extends FmNetwork {
return albums; return albums;
} }
public FMObjList getTopArtists(String period, int number) { public FMObjList getTopArtists(String period, int number) throws ApiCallException {
Logger.getLog().log(new LogEntry("getTopArtists").addArg(period).addArg(Integer.toString(number))); Logger.getLog().log(new LogEntry("getTopArtists").addArg(period).addArg(Integer.toString(number)));
@ -404,7 +419,7 @@ public class FmUserNetwork extends FmNetwork {
return artists; return artists;
} }
public ArrayList<Scrobble> getTrackScrobbles(Track track) { public ArrayList<Scrobble> getTrackScrobbles(Track track) throws ApiCallException {
Logger.getLog() Logger.getLog()
.log(new LogEntry("getTrackScrobbles").addArg(track.getName()).addArg(track.getArtist().getName())); .log(new LogEntry("getTrackScrobbles").addArg(track.getName()).addArg(track.getArtist().getName()));
@ -412,7 +427,7 @@ public class FmUserNetwork extends FmNetwork {
return getRecursiveTrackScrobbles(track, 1); return getRecursiveTrackScrobbles(track, 1);
} }
private ArrayList<Scrobble> getRecursiveTrackScrobbles(Track track, int page) { private ArrayList<Scrobble> getRecursiveTrackScrobbles(Track track, int page) throws ApiCallException {
int limit = 50; int limit = 50;
@ -466,7 +481,7 @@ public class FmUserNetwork extends FmNetwork {
} }
public ArrayList<Scrobble> getRecentScrobbles(int number) { public ArrayList<Scrobble> getRecentScrobbles(int number) throws ApiCallException {
Logger.getLog().log(new LogEntry("getRecentTracks").addArg(Integer.toString(number))); Logger.getLog().log(new LogEntry("getRecentTracks").addArg(Integer.toString(number)));
@ -524,8 +539,10 @@ public class FmUserNetwork extends FmNetwork {
* Returns list of user tags * Returns list of user tags
* *
* @return List of tags * @return List of tags
* @throws ApiCallException
* @throws JSONException
*/ */
public ArrayList<Tag> getTags() { public ArrayList<Tag> getTags() throws ApiCallException {
Logger.getLog().log(new LogEntry("getTags")); Logger.getLog().log(new LogEntry("getTags"));
@ -561,8 +578,10 @@ public class FmUserNetwork extends FmNetwork {
* *
* @param tagName Tag to explore * @param tagName Tag to explore
* @return FMObjList of artists * @return FMObjList of artists
* @throws ApiCallException
* @throws JSONException
*/ */
public FMObjList getArtistTag(String tagName) { public FMObjList getArtistTag(String tagName) throws ApiCallException {
Logger.getLog().log(new LogEntry("getArtistTag").addArg(tagName)); Logger.getLog().log(new LogEntry("getArtistTag").addArg(tagName));
@ -605,8 +624,10 @@ public class FmUserNetwork extends FmNetwork {
* *
* @param tagName Tag to explore * @param tagName Tag to explore
* @return FMObjList of artists * @return FMObjList of artists
* @throws ApiCallException
* @throws JSONException
*/ */
public FMObjList getPopulatedArtistTag(String tagName) { public FMObjList getPopulatedArtistTag(String tagName) throws ApiCallException {
Logger.getLog().log(new LogEntry("getPopulatedArtistTag").addArg(tagName)); Logger.getLog().log(new LogEntry("getPopulatedArtistTag").addArg(tagName));

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import javafx.scene.chart.PieChart; import javafx.scene.chart.PieChart;
import sarsoo.fmframework.config.Config; import sarsoo.fmframework.config.Config;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmUserNetwork; import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.fx.FmFramework; import sarsoo.fmframework.fx.FmFramework;
@ -20,7 +21,12 @@ public class GenreTotalPieChart extends GenrePieChart{
FmUserNetwork net = new FmUserNetwork(config.getValue("api_key"), config.getValue("username")); FmUserNetwork net = new FmUserNetwork(config.getValue("api_key"), config.getValue("username"));
int totalScrobbles = net.getUserScrobbleCount(); int totalScrobbles;
try {
totalScrobbles = net.getUserScrobbleCount();
} catch (ApiCallException e) {
totalScrobbles = 0;
}
int other = totalScrobbles - genreTotal; int other = totalScrobbles - genreTotal;
pieChartData.add(new PieChart.Data(String.format("other %d%%", (int) other * 100 / totalScrobbles), other)); pieChartData.add(new PieChart.Data(String.format("other %d%%", (int) other * 100 / totalScrobbles), other));

View File

@ -15,6 +15,7 @@ import javafx.fxml.FXML;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.input.MouseEvent; import javafx.scene.input.MouseEvent;
import sarsoo.fmframework.config.Config; import sarsoo.fmframework.config.Config;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmUserNetwork; import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.fx.FmFramework; import sarsoo.fmframework.fx.FmFramework;
import sarsoo.fmframework.fx.tab.ArtistTab; import sarsoo.fmframework.fx.tab.ArtistTab;
@ -93,33 +94,36 @@ public class FMObjListPaneController {
} }
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList( ObservableList<PieChart.Data> pieChartData;
new PieChart.Data(list.getGroupName(), list.getTotalUserScrobbles()), try {
new PieChart.Data("other", pieChartData = FXCollections.observableArrayList(
new FmUserNetwork(FmFramework.getSessionConfig().getValue("api_key"), username) new PieChart.Data(list.getGroupName(), list.getTotalUserScrobbles()),
.getUserScrobbleCount() - list.getTotalUserScrobbles())); new PieChart.Data("other",
new FmUserNetwork(FmFramework.getSessionConfig().getValue("api_key"), username)
.getUserScrobbleCount() - list.getTotalUserScrobbles()));
ObservableList<PieChart.Data> pieChartArtistsData = FXCollections.observableArrayList();
int counter2;
for (counter2 = 0; counter2 < list.size(); counter2++) {
ObservableList<PieChart.Data> pieChartArtistsData = FXCollections.observableArrayList(); PieChart.Data data = new PieChart.Data(list.get(counter2).getName(), list.get(counter2).getUserPlayCount());
int counter2;
for (counter2 = 0; counter2 < list.size(); counter2++) {
PieChart.Data data = new PieChart.Data(list.get(counter2).getName(), list.get(counter2).getUserPlayCount()); pieChartArtistsData.add(data);
pieChartArtistsData.add(data);
}
Collections.sort(pieChartArtistsData, new Comparator<PieChart.Data>() {
@Override
public int compare(Data arg0, Data arg1) {
return (int) (arg1.getPieValue() - arg0.getPieValue());
} }
});
pieChart.setData(pieChartData); Collections.sort(pieChartArtistsData, new Comparator<PieChart.Data>() {
pieChartArtists.setData(pieChartArtistsData);
@Override
public int compare(Data arg0, Data arg1) {
return (int) (arg1.getPieValue() - arg0.getPieValue());
}
});
pieChart.setData(pieChartData);
pieChartArtists.setData(pieChartArtistsData);
} catch (ApiCallException e) {}
} }
@FXML @FXML
@ -130,7 +134,11 @@ public class FMObjListPaneController {
String username = config.getValue("username"); String username = config.getValue("username");
String api_key = config.getValue("api_key"); String api_key = config.getValue("api_key");
list = new FmUserNetwork(api_key, username).getPopulatedArtistTag(list.getGroupName()); try {
list = new FmUserNetwork(api_key, username).getPopulatedArtistTag(list.getGroupName());
} catch (ApiCallException e1) {
e1.printStackTrace();
}
double percent = Maths.getPercentListening(list, username); double percent = Maths.getPercentListening(list, username);
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.UK); NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.UK);
@ -177,12 +185,18 @@ public class FMObjListPaneController {
} }
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList( ObservableList<PieChart.Data> pieChartData;
new PieChart.Data(list.getGroupName(), list.getTotalUserScrobbles()), try {
new PieChart.Data("other", pieChartData = FXCollections.observableArrayList(
new FmUserNetwork(FmFramework.getSessionConfig().getValue("api_key"), username) new PieChart.Data(list.getGroupName(), list.getTotalUserScrobbles()),
.getUserScrobbleCount() - list.getTotalUserScrobbles())); new PieChart.Data("other",
pieChart.setData(pieChartData); new FmUserNetwork(FmFramework.getSessionConfig().getValue("api_key"), username)
.getUserScrobbleCount() - list.getTotalUserScrobbles()));
pieChart.setData(pieChartData);
} catch (ApiCallException e) {}
} }
} }

View File

@ -15,6 +15,7 @@ import javafx.fxml.FXML;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.input.MouseEvent; import javafx.scene.input.MouseEvent;
import sarsoo.fmframework.config.Config; import sarsoo.fmframework.config.Config;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.file.ListPersister; import sarsoo.fmframework.file.ListPersister;
import sarsoo.fmframework.fm.FmNetwork; import sarsoo.fmframework.fm.FmNetwork;
import sarsoo.fmframework.fm.FmUserNetwork; import sarsoo.fmframework.fm.FmUserNetwork;
@ -55,84 +56,6 @@ public class FMObjListPaneEditController {
this.list = list; this.list = list;
} }
// public void populate(FMObjList list) {
// this.list = list;
//
// double percent = Maths.getPercentListening(list, Reference.getUserName());
// NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.US);
//
// labelTotalScrobbles.setText("Σ " + list.getTotalUserScrobbles());
// labelPercent.setText(String.format("%.2f%%", percent));
//
// Collections.sort(list);
// Collections.reverse(list);
//
// int counter;
// for (counter = 0; counter < list.size(); counter++) {
//
// FMObj obj = list.get(counter);
//
// Label name = new Label(obj.getName().toLowerCase());
//
// name.getStyleClass().add("nameLabel");
//
// name.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<Event>() {
//
// @Override
// public void handle(Event event) {
//
// try {
// FmFramework.getController().addTab(new ArtistTab((Artist) obj));
// } catch (IOException e) {
//
// e.printStackTrace();
// }
//
// }
//
// });
//
// Label userScrobbles = new Label(numberFormat.format(obj.getUserPlayCount()));
// Label totalScrobbles = new Label(numberFormat.format(obj.getPlayCount()));
//
// gridPaneFMObjs.add(name, 0, counter);
// gridPaneFMObjs.add(userScrobbles, 1, counter);
// gridPaneFMObjs.add(totalScrobbles, 2, counter);
//
// }
//
//
// ObservableList<PieChart.Data> pieChartData =
// FXCollections.observableArrayList(
// new PieChart.Data(list.getGroupName(), list.getTotalUserScrobbles()),
// new PieChart.Data("other", Getter.getScrobbles(Reference.getUserName()) -
// list.getTotalUserScrobbles()));
//
// ObservableList<PieChart.Data> pieChartArtistsData =
// FXCollections.observableArrayList();
// int counter2;
// for(counter2 = 0; counter2 < list.size(); counter2++) {
//
// PieChart.Data data = new PieChart.Data(list.get(counter2).getName(),
// list.get(counter2).getUserPlayCount());
//
// pieChartArtistsData.add(data);
//
// }
//
// Collections.sort(pieChartArtistsData, new Comparator<PieChart.Data>() {
//
// @Override
// public int compare(Data arg0, Data arg1) {
// return (int) (arg1.getPieValue() - arg0.getPieValue());
// }
// });
//
// pieChart.setData(pieChartData);
// pieChartArtists.setData(pieChartArtistsData);
//
// }
@FXML @FXML
protected void handleRefresh(ActionEvent event) { protected void handleRefresh(ActionEvent event) {
updateList(); updateList();
@ -148,7 +71,9 @@ public class FMObjListPaneEditController {
int counter; int counter;
for (counter = 0; counter < list.size(); counter++) { for (counter = 0; counter < list.size(); counter++) {
newList.add(net.refresh(list.get(counter))); try {
newList.add(net.refresh(list.get(counter)));
} catch (ApiCallException e) {}
} }
setList(newList); setList(newList);
@ -225,7 +150,12 @@ public class FMObjListPaneEditController {
Config config = FmFramework.getSessionConfig(); Config config = FmFramework.getSessionConfig();
FmUserNetwork net = new FmUserNetwork(config.getValue("api_key"), config.getValue("username")); FmUserNetwork net = new FmUserNetwork(config.getValue("api_key"), config.getValue("username"));
int other = net.getUserScrobbleCount() - list.getTotalUserScrobbles(); int other;
try {
other = net.getUserScrobbleCount() - list.getTotalUserScrobbles();
} catch (ApiCallException e) {
other = 0;
}
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList( ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
new PieChart.Data(String.format("%d%%", (int) list.getTotalUserScrobbles() * 100 / other), new PieChart.Data(String.format("%d%%", (int) list.getTotalUserScrobbles() * 100 / other),
list.getTotalUserScrobbles()), list.getTotalUserScrobbles()),
@ -261,18 +191,23 @@ public class FMObjListPaneEditController {
String artist = textArtist.getText(); String artist = textArtist.getText();
if ((name != null) && (artist != null)) { if ((name != null) && (artist != null)) {
Track track = net.getTrack(name, artist); Track track;
if (album != null) { try {
Album albumObj = net.getAlbum(album, artist); track = net.getTrack(name, artist);
track.setAlbum(albumObj);
if (album != null) {
Album albumObj = net.getAlbum(album, artist);
track.setAlbum(albumObj);
textAlbum.setText(null); textAlbum.setText(null);
} }
textTrack.setText(null); textTrack.setText(null);
textArtist.setText(null); textArtist.setText(null);
list.add(track); list.add(track);
} catch (ApiCallException e) {}
} }
refresh(); refresh();
@ -289,9 +224,14 @@ public class FMObjListPaneEditController {
String artist = textArtist.getText(); String artist = textArtist.getText();
if ((album != null) && (artist != null)) { if ((album != null) && (artist != null)) {
Album albumObj = net.getAlbum(album, artist); Album albumObj;
try {
list.add(albumObj); albumObj = net.getAlbum(album, artist);
list.add(albumObj);
} catch (ApiCallException e) {}
textAlbum.setText(null); textAlbum.setText(null);
textArtist.setText(null); textArtist.setText(null);
} }
@ -309,9 +249,13 @@ public class FMObjListPaneEditController {
if (artist != null) { if (artist != null) {
Artist artistObj = net.getArtist(artist); Artist artistObj;
try {
list.add(artistObj); artistObj = net.getArtist(artist);
list.add(artistObj);
} catch (ApiCallException e) {}
textArtist.setText(null); textArtist.setText(null);
} }

View File

@ -19,6 +19,7 @@ import sarsoo.fmframework.config.ConfigPersister;
import sarsoo.fmframework.config.ConfigVariable; import sarsoo.fmframework.config.ConfigVariable;
import sarsoo.fmframework.config.VariableEvent; import sarsoo.fmframework.config.VariableEvent;
import sarsoo.fmframework.config.VariableListener; import sarsoo.fmframework.config.VariableListener;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.file.ListPersister; import sarsoo.fmframework.file.ListPersister;
import sarsoo.fmframework.fm.FmAuthNetwork; import sarsoo.fmframework.fm.FmAuthNetwork;
import sarsoo.fmframework.fx.TextAreaConsole; import sarsoo.fmframework.fx.TextAreaConsole;
@ -248,16 +249,20 @@ public class RootController {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} } catch (ApiCallException e) {}
} }
protected void completeAuth(FmAuthNetwork net, String token) { protected void completeAuth(FmAuthNetwork net, String token) {
String sk = net.getSession(token); String sk;
try {
if (sk != null) { sk = net.getSession(token);
FmFramework.getSessionConfig().addVariable(new ConfigVariable("session_key", sk));
} if (sk != null) {
FmFramework.getSessionConfig().addVariable(new ConfigVariable("session_key", sk));
}
} catch (ApiCallException e) {}
} }
public void changeUsername() { public void changeUsername() {

View File

@ -14,6 +14,7 @@ import javafx.scene.control.Label;
import javafx.scene.control.Slider; import javafx.scene.control.Slider;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import sarsoo.fmframework.config.Config; import sarsoo.fmframework.config.Config;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmAuthNetwork; import sarsoo.fmframework.fm.FmAuthNetwork;
import sarsoo.fmframework.fx.FmFramework; import sarsoo.fmframework.fx.FmFramework;
import sarsoo.fmframework.music.Artist; import sarsoo.fmframework.music.Artist;
@ -101,13 +102,18 @@ public class ScrobblePaneController {
scrobble.setAlbum(new AlbumBuilder(textAlbum.getText(), albumArtist).build()); scrobble.setAlbum(new AlbumBuilder(textAlbum.getText(), albumArtist).build());
} }
JSONObject obj = net.scrobble(scrobble, config.getValue("session_key")); JSONObject obj;
if(obj.getJSONObject("scrobbles").getJSONObject("@attr").getInt("accepted") == 1) { try {
labelStatus.setText("sent!"); obj = net.scrobble(scrobble, config.getValue("session_key"));
}else {
labelStatus.setText("failed"); if(obj.getJSONObject("scrobbles").getJSONObject("@attr").getInt("accepted") == 1) {
} labelStatus.setText("sent!");
}else {
labelStatus.setText("failed");
}
} catch (ApiCallException e) {}
}else { }else {
labelStatus.setText("unauthorized"); labelStatus.setText("unauthorized");

View File

@ -19,6 +19,7 @@ import javafx.scene.control.Label;
import javafx.scene.control.SplitPane; import javafx.scene.control.SplitPane;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
import sarsoo.fmframework.config.Config; import sarsoo.fmframework.config.Config;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmUserNetwork; import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.fx.FmFramework; import sarsoo.fmframework.fx.FmFramework;
import sarsoo.fmframework.music.FMObj; import sarsoo.fmframework.music.FMObj;
@ -113,7 +114,11 @@ public class ScrobblesViewPaneController {
Config config = FmFramework.getSessionConfig(); Config config = FmFramework.getSessionConfig();
FmUserNetwork net = new FmUserNetwork(config.getValue("api_key"), config.getValue("username")); FmUserNetwork net = new FmUserNetwork(config.getValue("api_key"), config.getValue("username"));
firstDate = net.getFirstScrobbleDateTime().toLocalDate(); try {
firstDate = net.getFirstScrobbleDateTime().toLocalDate();
} catch (ApiCallException e) {
firstDate = LocalDate.now();
}
buttonAlbums.setDisable(false); buttonAlbums.setDisable(false);
buttonTracksAlbums.setDisable(false); buttonTracksAlbums.setDisable(false);

View File

@ -14,6 +14,7 @@ import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.layout.AnchorPane; import javafx.scene.layout.AnchorPane;
import sarsoo.fmframework.config.Config; import sarsoo.fmframework.config.Config;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmUserNetwork; import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.fx.FmFramework; import sarsoo.fmframework.fx.FmFramework;
import sarsoo.fmframework.fx.controller.info.ArtistPaneController; import sarsoo.fmframework.fx.controller.info.ArtistPaneController;
@ -105,7 +106,9 @@ public class ArtistBorderPaneController extends FMObjBorderPaneController{
topTracks.stream().forEach(t -> { topTracks.stream().forEach(t -> {
scrobbles.addAll(net.getTrackScrobbles((Track) t)); try {
scrobbles.addAll(net.getTrackScrobbles((Track) t));
} catch (ApiCallException e) {}
}); });

View File

@ -4,6 +4,7 @@ import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import sarsoo.fmframework.config.Config; import sarsoo.fmframework.config.Config;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmUserNetwork; import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.fx.FmFramework; import sarsoo.fmframework.fx.FmFramework;
import sarsoo.fmframework.util.Maths; import sarsoo.fmframework.util.Maths;
@ -67,7 +68,11 @@ public abstract class FMObj implements Comparable<FMObj>, Serializable{
Config config = FmFramework.getSessionConfig(); Config config = FmFramework.getSessionConfig();
FmUserNetwork net = new FmUserNetwork(config.getValue("api_key"), config.getValue("username")); FmUserNetwork net = new FmUserNetwork(config.getValue("api_key"), config.getValue("username"));
return ((double)userPlayCount*100)/(double) net.getUser().getScrobbleCount(); try {
return ((double)userPlayCount*100)/(double) net.getUser().getScrobbleCount();
} catch (ApiCallException e) {}
return 0;
} }
public Wiki getWiki() { public Wiki getWiki() {

View File

@ -5,6 +5,7 @@ import java.util.ArrayList;
import sarsoo.fmframework.cache.Cacheable; import sarsoo.fmframework.cache.Cacheable;
import sarsoo.fmframework.config.Config; import sarsoo.fmframework.config.Config;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmUserNetwork; import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.fx.FmFramework; import sarsoo.fmframework.fx.FmFramework;
import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Album;
@ -109,6 +110,10 @@ public class FMObjList extends ArrayList<FMObj> implements Comparable<FMObjList>
Config config = FmFramework.getSessionConfig(); Config config = FmFramework.getSessionConfig();
FmUserNetwork net = new FmUserNetwork(config.getValue("api_key"), config.getValue("username")); FmUserNetwork net = new FmUserNetwork(config.getValue("api_key"), config.getValue("username"));
stream().forEach(item -> net.refresh(item)); stream().forEach(item -> {
try {
net.refresh(item);
} catch (ApiCallException e) {}
});
} }
} }

View File

@ -4,6 +4,7 @@ import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmUserNetwork; import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.fx.FmFramework; import sarsoo.fmframework.fx.FmFramework;
import sarsoo.fmframework.music.FMObj; import sarsoo.fmframework.music.FMObj;
@ -11,35 +12,44 @@ import sarsoo.fmframework.music.FMObj;
public class Maths { public class Maths {
public static double getPercentListening(FMObj obj, String username) { public static double getPercentListening(FMObj obj, String username) {
int userScrobbles = new FmUserNetwork(FmFramework.getSessionConfig().getValue("api_key"), username) int userScrobbles;
.getUserScrobbleCount(); try {
double plays = (double) obj.getUserPlayCount(); userScrobbles = new FmUserNetwork(FmFramework.getSessionConfig().getValue("api_key"), username)
if (userScrobbles > 0 && plays > 0) { .getUserScrobbleCount();
double plays = (double) obj.getUserPlayCount();
if (userScrobbles > 0 && plays > 0) {
double userScrobblesDouble = (double) userScrobbles; double userScrobblesDouble = (double) userScrobbles;
double percentage = (plays / userScrobblesDouble) * 100; double percentage = (plays / userScrobblesDouble) * 100;
return percentage;
}
return percentage;
}
} catch (ApiCallException e) {}
return 0; return 0;
} }
public static double getPercentListening(FMObjList objList, String username) { public static double getPercentListening(FMObjList objList, String username) {
int userScrobbles = new FmUserNetwork(FmFramework.getSessionConfig().getValue("api_key"), username) int userScrobbles;
.getUserScrobbleCount(); try {
double plays = (double) objList.getTotalUserScrobbles(); userScrobbles = new FmUserNetwork(FmFramework.getSessionConfig().getValue("api_key"), username)
if (userScrobbles > 0 && plays > 0) { .getUserScrobbleCount();
double plays = (double) objList.getTotalUserScrobbles();
if (userScrobbles > 0 && plays > 0) {
double userScrobblesDouble = (double) userScrobbles; double userScrobblesDouble = (double) userScrobbles;
double percentage = (plays / userScrobblesDouble) * 100; double percentage = (plays / userScrobblesDouble) * 100;
return percentage;
}
return percentage;
}
} catch (ApiCallException e) {}
return 0; return 0;
} }

View File

@ -3,6 +3,7 @@ package sarsoo.fmframework.fm;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.music.Album; import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist; import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Artist.ArtistBuilder; import sarsoo.fmframework.music.Artist.ArtistBuilder;
@ -18,24 +19,42 @@ public class FmNetworkTest {
@Test @Test
public void testGetNonNullAlbum() { public void testGetNonNullAlbum() {
assertNotNull(new FmNetwork(Key.getKey()).getAlbum("To Pimp A Butterfly", "Kendrick Lamar")); try {
assertNotNull(new FmNetwork(Key.getKey()).getAlbum("To Pimp A Butterfly", "Kendrick Lamar"));
} catch (ApiCallException e) {
e.printStackTrace();
}
} }
@Test @Test
public void testAlbumDataMatch() { public void testAlbumDataMatch() {
Album album = new FmNetwork(Key.getKey()).getAlbum("To Pimp A Butterfly", "Kendrick Lamar"); Album album;
assertEquals(album.getName(), "To Pimp a Butterfly"); try {
album = new FmNetwork(Key.getKey()).getAlbum("To Pimp A Butterfly", "Kendrick Lamar");
assertEquals(album.getName(), "To Pimp a Butterfly");
} catch (ApiCallException e) {
e.printStackTrace();
}
} }
@Test @Test
public void testGetNonNullArtist() { public void testGetNonNullArtist() {
assertNotNull(new FmNetwork(Key.getKey()).getArtist("Kendrick Lamar")); try {
assertNotNull(new FmNetwork(Key.getKey()).getArtist("Kendrick Lamar"));
} catch (ApiCallException e) {
e.printStackTrace();
}
} }
@Test @Test
public void testArtistDataMatch() { public void testArtistDataMatch() {
Artist artist= new FmNetwork(Key.getKey()).getArtist("Kendrick Lamar"); Artist artist;
assertEquals(artist.getName(), "Kendrick Lamar"); try {
artist = new FmNetwork(Key.getKey()).getArtist("Kendrick Lamar");
assertEquals(artist.getName(), "Kendrick Lamar");
} catch (ApiCallException e) {
e.printStackTrace();
}
} }
@Test @Test
@ -51,11 +70,16 @@ public class FmNetworkTest {
public void testArtistTopTracks() { public void testArtistTopTracks() {
FmNetwork network = new FmNetwork(Key.getKey()); FmNetwork network = new FmNetwork(Key.getKey());
FMObjList list = network.getArtistTopTracks(new ArtistBuilder("kendrick lamar").build(), 10); FMObjList list;
try {
list.stream().forEach(System.out::println); list = network.getArtistTopTracks(new ArtistBuilder("kendrick lamar").build(), 10);
list.stream().forEach(System.out::println);
assertTrue(true);
assertTrue(true);
} catch (ApiCallException e) {
e.printStackTrace();
}
} }
} }

View File

@ -5,6 +5,7 @@ import org.junit.Test;
import sarsoo.fmframework.music.Scrobble; import sarsoo.fmframework.music.Scrobble;
import sarsoo.fmframework.music.Track; import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.music.Track.TrackBuilder; import sarsoo.fmframework.music.Track.TrackBuilder;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.music.Artist.ArtistBuilder; import sarsoo.fmframework.music.Artist.ArtistBuilder;
import sarsoo.fmframework.net.Key; import sarsoo.fmframework.net.Key;
import sarsoo.fmframework.util.FMObjList; import sarsoo.fmframework.util.FMObjList;
@ -22,7 +23,11 @@ public class FmUserNetworkTest {
@Test @Test
public void testGetLastTrack() { public void testGetLastTrack() {
assertNotNull(new FmUserNetwork(Key.getKey(), "sarsoo").getLastTrack()); try {
assertNotNull(new FmUserNetwork(Key.getKey(), "sarsoo").getLastTrack());
} catch (ApiCallException e) {
e.printStackTrace();
}
} }
@Test @Test
@ -31,10 +36,17 @@ public class FmUserNetworkTest {
int limit = 50; int limit = 50;
ArrayList<Scrobble> scrobbles = net.getRecentScrobbles(limit); ArrayList<Scrobble> scrobbles;
// scrobbles.stream().forEach(System.out::println); try {
System.out.println(scrobbles.size()); scrobbles = net.getRecentScrobbles(limit);
assertEquals(limit, scrobbles.size());
// scrobbles.stream().forEach(System.out::println);
System.out.println(scrobbles.size());
assertEquals(limit, scrobbles.size());
} catch (ApiCallException e) {
e.printStackTrace();
}
} }
@Test @Test
@ -43,9 +55,17 @@ public class FmUserNetworkTest {
int limit = 50; int limit = 50;
FMObjList list = net.getTopAlbums("7day", limit); FMObjList list;
// list.stream().forEach(System.out::println); try {
assertEquals(limit, list.size()); list = net.getTopAlbums("7day", limit);
// list.stream().forEach(System.out::println);
assertEquals(limit, list.size());
} catch (ApiCallException e) {
e.printStackTrace();
}
} }
@Test @Test
@ -54,9 +74,17 @@ public class FmUserNetworkTest {
int limit = 50; int limit = 50;
FMObjList list = net.getTopArtists("7day", limit); FMObjList list;
// list.stream().forEach(System.out::println); try {
assertEquals(limit, list.size()); list = net.getTopArtists("7day", limit);
// list.stream().forEach(System.out::println);
assertEquals(limit, list.size());
} catch (ApiCallException e) {
e.printStackTrace();
}
} }
@Test @Test
@ -65,9 +93,17 @@ public class FmUserNetworkTest {
int limit = 50; int limit = 50;
FMObjList list = net.getTopTracks("7day", limit); FMObjList list;
// list.stream().forEach(System.out::println); try {
assertEquals(limit, list.size()); list = net.getTopTracks("7day", limit);
// list.stream().forEach(System.out::println);
assertEquals(limit, list.size());
} catch (ApiCallException e) {
e.printStackTrace();
}
} }
@Test @Test
@ -76,19 +112,30 @@ public class FmUserNetworkTest {
Track track = new TrackBuilder("shitsville", new ArtistBuilder("freddie gibbs").build()).build(); Track track = new TrackBuilder("shitsville", new ArtistBuilder("freddie gibbs").build()).build();
ArrayList<Scrobble> scrobbles = net.getTrackScrobbles(track); ArrayList<Scrobble> scrobbles;
try {
scrobbles.stream().forEach(System.out::println); scrobbles = net.getTrackScrobbles(track);
System.out.println(scrobbles.size());
assertTrue(true); scrobbles.stream().forEach(System.out::println);
System.out.println(scrobbles.size());
assertTrue(true);
} catch (ApiCallException e) {
e.printStackTrace();
}
} }
@Test @Test
public void testFirstScrobbleDateTime() { public void testFirstScrobbleDateTime() {
FmUserNetwork net = new FmUserNetwork(Key.getKey(), "sarsoo"); FmUserNetwork net = new FmUserNetwork(Key.getKey(), "sarsoo");
System.out.println(net.getFirstScrobbleDateTime()); try {
System.out.println(net.getFirstScrobbleDateTime());
assertTrue(true);
assertTrue(true);
} catch (ApiCallException e) {
e.printStackTrace();
}
} }
} }