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;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmNetwork;
import sarsoo.fmframework.music.Album;
@ -12,7 +13,11 @@ public class AlbumPuller implements Puller<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;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmNetwork;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist;
@ -17,7 +18,11 @@ public class ArtistPuller implements Puller<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;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.util.FMObjList;
@ -12,7 +13,11 @@ public class ArtistTagPuller implements Puller<FMObjList, String> {
}
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;
import sarsoo.fmframework.cache.StaticCache;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.util.FMObjList;
@ -16,16 +17,22 @@ public class CachedArtistTagPuller implements Puller<FMObjList, String> {
}
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());
FMObjList returned = new FMObjList();
returned.setGroupName(list.getGroupName());
for(int i = 0; i < list.size(); i++) {
returned.add(artistPool.get((Artist) list.get(i)));
}
for(int i = 0; i < list.size(); i++) {
returned.add(artistPool.get((Artist) list.get(i)));
}
return returned;
return returned;
} catch (ApiCallException e) {}
return null;
}
}

View File

@ -1,5 +1,6 @@
package sarsoo.fmframework.cache.puller;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmNetwork;
import sarsoo.fmframework.music.Track;
@ -12,7 +13,11 @@ public class TrackPuller implements Puller<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;
import sarsoo.fmframework.log.Log;
import sarsoo.fmframework.log.Logger;
import sarsoo.fmframework.log.entry.ErrorEntry;
public class ApiCallException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
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.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() {
return failureCode;
}
public String getError() {
switch(failureCode) {
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;
}
public String getFailureMessage() {
return failureString;
}
}

View File

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

View File

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

View File

@ -11,6 +11,7 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.log.Logger;
import sarsoo.fmframework.log.entry.ErrorEntry;
import sarsoo.fmframework.log.entry.InfoEntry;
@ -72,8 +73,10 @@ public class FmUserNetwork extends FmNetwork {
* Return user object from Last.FM
*
* @return User
* @throws ApiCallException
* @throws JSONException
*/
public User getUser() {
public User getUser() throws ApiCallException {
Logger.getLog().log(new LogEntry("getUser"));
@ -93,8 +96,10 @@ public class FmUserNetwork extends FmNetwork {
* 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"));
@ -105,8 +110,10 @@ public class FmUserNetwork extends FmNetwork {
* Return user's 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"));
@ -117,8 +124,10 @@ public class FmUserNetwork extends FmNetwork {
* Returns last or currently listening track
*
* @return Last track
* @throws ApiCallException
* @throws JSONException
*/
public Track getLastTrack() {
public Track getLastTrack() throws ApiCallException {
Logger.getLog().log(new LogEntry("getLastTrack"));
@ -145,8 +154,10 @@ public class FmUserNetwork extends FmNetwork {
* Return scrobble count from today
*
* @return Scrobble count today
* @throws ApiCallException
* @throws JSONException
*/
public int getScrobblesToday() {
public int getScrobblesToday() throws ApiCallException {
Logger.getLog().log(new LogEntry("getScrobblesToday"));
@ -176,8 +187,10 @@ public class FmUserNetwork extends FmNetwork {
* @param month Month int
* @param year Year int
* @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))
.addArg(Integer.toString(month)).addArg(Integer.toString(year)));
@ -208,8 +221,10 @@ public class FmUserNetwork extends FmNetwork {
*
* @param day Negative day offset
* @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)));
@ -235,7 +250,7 @@ public class FmUserNetwork extends FmNetwork {
}
public LocalDateTime getFirstScrobbleDateTime() {
public LocalDateTime getFirstScrobbleDateTime() throws ApiCallException {
Logger.getLog().log(new LogEntry("getFirstScrobbleDates"));
@ -265,7 +280,7 @@ public class FmUserNetwork extends FmNetwork {
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)));
@ -311,7 +326,7 @@ public class FmUserNetwork extends FmNetwork {
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)));
@ -358,7 +373,7 @@ public class FmUserNetwork extends FmNetwork {
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)));
@ -404,7 +419,7 @@ public class FmUserNetwork extends FmNetwork {
return artists;
}
public ArrayList<Scrobble> getTrackScrobbles(Track track) {
public ArrayList<Scrobble> getTrackScrobbles(Track track) throws ApiCallException {
Logger.getLog()
.log(new LogEntry("getTrackScrobbles").addArg(track.getName()).addArg(track.getArtist().getName()));
@ -412,7 +427,7 @@ public class FmUserNetwork extends FmNetwork {
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;
@ -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)));
@ -524,8 +539,10 @@ public class FmUserNetwork extends FmNetwork {
* Returns list of user 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"));
@ -561,8 +578,10 @@ public class FmUserNetwork extends FmNetwork {
*
* @param tagName Tag to explore
* @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));
@ -605,8 +624,10 @@ public class FmUserNetwork extends FmNetwork {
*
* @param tagName Tag to explore
* @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));

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import javafx.scene.chart.PieChart;
import sarsoo.fmframework.config.Config;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmUserNetwork;
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"));
int totalScrobbles = net.getUserScrobbleCount();
int totalScrobbles;
try {
totalScrobbles = net.getUserScrobbleCount();
} catch (ApiCallException e) {
totalScrobbles = 0;
}
int other = totalScrobbles - genreTotal;
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.input.MouseEvent;
import sarsoo.fmframework.config.Config;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.fx.FmFramework;
import sarsoo.fmframework.fx.tab.ArtistTab;
@ -93,33 +94,36 @@ public class FMObjListPaneController {
}
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
new PieChart.Data(list.getGroupName(), list.getTotalUserScrobbles()),
new PieChart.Data("other",
new FmUserNetwork(FmFramework.getSessionConfig().getValue("api_key"), username)
.getUserScrobbleCount() - list.getTotalUserScrobbles()));
ObservableList<PieChart.Data> pieChartData;
try {
pieChartData = FXCollections.observableArrayList(
new PieChart.Data(list.getGroupName(), 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();
int counter2;
for (counter2 = 0; counter2 < list.size(); counter2++) {
PieChart.Data data = new PieChart.Data(list.get(counter2).getName(), list.get(counter2).getUserPlayCount());
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);
pieChartArtists.setData(pieChartArtistsData);
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);
} catch (ApiCallException e) {}
}
@FXML
@ -130,7 +134,11 @@ public class FMObjListPaneController {
String username = config.getValue("username");
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);
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.UK);
@ -177,12 +185,18 @@ public class FMObjListPaneController {
}
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
new PieChart.Data(list.getGroupName(), list.getTotalUserScrobbles()),
new PieChart.Data("other",
new FmUserNetwork(FmFramework.getSessionConfig().getValue("api_key"), username)
.getUserScrobbleCount() - list.getTotalUserScrobbles()));
pieChart.setData(pieChartData);
ObservableList<PieChart.Data> pieChartData;
try {
pieChartData = FXCollections.observableArrayList(
new PieChart.Data(list.getGroupName(), list.getTotalUserScrobbles()),
new PieChart.Data("other",
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.input.MouseEvent;
import sarsoo.fmframework.config.Config;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.file.ListPersister;
import sarsoo.fmframework.fm.FmNetwork;
import sarsoo.fmframework.fm.FmUserNetwork;
@ -55,84 +56,6 @@ public class FMObjListPaneEditController {
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
protected void handleRefresh(ActionEvent event) {
updateList();
@ -148,7 +71,9 @@ public class FMObjListPaneEditController {
int 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);
@ -225,7 +150,12 @@ public class FMObjListPaneEditController {
Config config = FmFramework.getSessionConfig();
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(
new PieChart.Data(String.format("%d%%", (int) list.getTotalUserScrobbles() * 100 / other),
list.getTotalUserScrobbles()),
@ -261,18 +191,23 @@ public class FMObjListPaneEditController {
String artist = textArtist.getText();
if ((name != null) && (artist != null)) {
Track track = net.getTrack(name, artist);
if (album != null) {
Album albumObj = net.getAlbum(album, artist);
track.setAlbum(albumObj);
Track track;
try {
track = net.getTrack(name, artist);
textAlbum.setText(null);
}
if (album != null) {
Album albumObj = net.getAlbum(album, artist);
track.setAlbum(albumObj);
textTrack.setText(null);
textArtist.setText(null);
textAlbum.setText(null);
}
list.add(track);
textTrack.setText(null);
textArtist.setText(null);
list.add(track);
} catch (ApiCallException e) {}
}
refresh();
@ -289,9 +224,14 @@ public class FMObjListPaneEditController {
String artist = textArtist.getText();
if ((album != null) && (artist != null)) {
Album albumObj = net.getAlbum(album, artist);
Album albumObj;
try {
albumObj = net.getAlbum(album, artist);
list.add(albumObj);
} catch (ApiCallException e) {}
list.add(albumObj);
textAlbum.setText(null);
textArtist.setText(null);
}
@ -309,9 +249,13 @@ public class FMObjListPaneEditController {
if (artist != null) {
Artist artistObj = net.getArtist(artist);
Artist artistObj;
try {
artistObj = net.getArtist(artist);
list.add(artistObj);
list.add(artistObj);
} catch (ApiCallException e) {}
textArtist.setText(null);
}

View File

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

View File

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

View File

@ -19,6 +19,7 @@ import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.GridPane;
import sarsoo.fmframework.config.Config;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.fx.FmFramework;
import sarsoo.fmframework.music.FMObj;
@ -113,7 +114,11 @@ public class ScrobblesViewPaneController {
Config config = FmFramework.getSessionConfig();
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);
buttonTracksAlbums.setDisable(false);

View File

@ -14,6 +14,7 @@ import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.AnchorPane;
import sarsoo.fmframework.config.Config;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.fx.FmFramework;
import sarsoo.fmframework.fx.controller.info.ArtistPaneController;
@ -105,7 +106,9 @@ public class ArtistBorderPaneController extends FMObjBorderPaneController{
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 sarsoo.fmframework.config.Config;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.fx.FmFramework;
import sarsoo.fmframework.util.Maths;
@ -67,7 +68,11 @@ public abstract class FMObj implements Comparable<FMObj>, Serializable{
Config config = FmFramework.getSessionConfig();
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() {

View File

@ -5,6 +5,7 @@ import java.util.ArrayList;
import sarsoo.fmframework.cache.Cacheable;
import sarsoo.fmframework.config.Config;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.fx.FmFramework;
import sarsoo.fmframework.music.Album;
@ -109,6 +110,10 @@ public class FMObjList extends ArrayList<FMObj> implements Comparable<FMObjList>
Config config = FmFramework.getSessionConfig();
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.GregorianCalendar;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.fm.FmUserNetwork;
import sarsoo.fmframework.fx.FmFramework;
import sarsoo.fmframework.music.FMObj;
@ -11,34 +12,43 @@ import sarsoo.fmframework.music.FMObj;
public class Maths {
public static double getPercentListening(FMObj obj, String username) {
int userScrobbles = new FmUserNetwork(FmFramework.getSessionConfig().getValue("api_key"), username)
.getUserScrobbleCount();
double plays = (double) obj.getUserPlayCount();
if (userScrobbles > 0 && plays > 0) {
int userScrobbles;
try {
userScrobbles = new FmUserNetwork(FmFramework.getSessionConfig().getValue("api_key"), username)
.getUserScrobbleCount();
double userScrobblesDouble = (double) userScrobbles;
double plays = (double) obj.getUserPlayCount();
if (userScrobbles > 0 && plays > 0) {
double percentage = (plays / userScrobblesDouble) * 100;
double userScrobblesDouble = (double) userScrobbles;
return percentage;
}
double percentage = (plays / userScrobblesDouble) * 100;
return percentage;
}
} catch (ApiCallException e) {}
return 0;
}
public static double getPercentListening(FMObjList objList, String username) {
int userScrobbles = new FmUserNetwork(FmFramework.getSessionConfig().getValue("api_key"), username)
.getUserScrobbleCount();
double plays = (double) objList.getTotalUserScrobbles();
if (userScrobbles > 0 && plays > 0) {
int userScrobbles;
try {
userScrobbles = new FmUserNetwork(FmFramework.getSessionConfig().getValue("api_key"), username)
.getUserScrobbleCount();
double userScrobblesDouble = (double) userScrobbles;
double plays = (double) objList.getTotalUserScrobbles();
if (userScrobbles > 0 && plays > 0) {
double percentage = (plays / userScrobblesDouble) * 100;
double userScrobblesDouble = (double) userScrobbles;
return percentage;
}
double percentage = (plays / userScrobblesDouble) * 100;
return percentage;
}
} catch (ApiCallException e) {}
return 0;
}

View File

@ -3,6 +3,7 @@ package sarsoo.fmframework.fm;
import org.junit.Test;
import static org.junit.Assert.*;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Artist.ArtistBuilder;
@ -18,24 +19,42 @@ public class FmNetworkTest {
@Test
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
public void testAlbumDataMatch() {
Album album = new FmNetwork(Key.getKey()).getAlbum("To Pimp A Butterfly", "Kendrick Lamar");
assertEquals(album.getName(), "To Pimp a Butterfly");
Album album;
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
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
public void testArtistDataMatch() {
Artist artist= new FmNetwork(Key.getKey()).getArtist("Kendrick Lamar");
assertEquals(artist.getName(), "Kendrick Lamar");
Artist artist;
try {
artist = new FmNetwork(Key.getKey()).getArtist("Kendrick Lamar");
assertEquals(artist.getName(), "Kendrick Lamar");
} catch (ApiCallException e) {
e.printStackTrace();
}
}
@Test
@ -51,11 +70,16 @@ public class FmNetworkTest {
public void testArtistTopTracks() {
FmNetwork network = new FmNetwork(Key.getKey());
FMObjList list = network.getArtistTopTracks(new ArtistBuilder("kendrick lamar").build(), 10);
FMObjList list;
try {
list = network.getArtistTopTracks(new ArtistBuilder("kendrick lamar").build(), 10);
list.stream().forEach(System.out::println);
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.Track;
import sarsoo.fmframework.music.Track.TrackBuilder;
import sarsoo.fmframework.error.ApiCallException;
import sarsoo.fmframework.music.Artist.ArtistBuilder;
import sarsoo.fmframework.net.Key;
import sarsoo.fmframework.util.FMObjList;
@ -22,7 +23,11 @@ public class FmUserNetworkTest {
@Test
public void testGetLastTrack() {
assertNotNull(new FmUserNetwork(Key.getKey(), "sarsoo").getLastTrack());
try {
assertNotNull(new FmUserNetwork(Key.getKey(), "sarsoo").getLastTrack());
} catch (ApiCallException e) {
e.printStackTrace();
}
}
@Test
@ -31,10 +36,17 @@ public class FmUserNetworkTest {
int limit = 50;
ArrayList<Scrobble> scrobbles = net.getRecentScrobbles(limit);
// scrobbles.stream().forEach(System.out::println);
System.out.println(scrobbles.size());
assertEquals(limit, scrobbles.size());
ArrayList<Scrobble> scrobbles;
try {
scrobbles = net.getRecentScrobbles(limit);
// scrobbles.stream().forEach(System.out::println);
System.out.println(scrobbles.size());
assertEquals(limit, scrobbles.size());
} catch (ApiCallException e) {
e.printStackTrace();
}
}
@Test
@ -43,9 +55,17 @@ public class FmUserNetworkTest {
int limit = 50;
FMObjList list = net.getTopAlbums("7day", limit);
// list.stream().forEach(System.out::println);
assertEquals(limit, list.size());
FMObjList list;
try {
list = net.getTopAlbums("7day", limit);
// list.stream().forEach(System.out::println);
assertEquals(limit, list.size());
} catch (ApiCallException e) {
e.printStackTrace();
}
}
@Test
@ -54,9 +74,17 @@ public class FmUserNetworkTest {
int limit = 50;
FMObjList list = net.getTopArtists("7day", limit);
// list.stream().forEach(System.out::println);
assertEquals(limit, list.size());
FMObjList list;
try {
list = net.getTopArtists("7day", limit);
// list.stream().forEach(System.out::println);
assertEquals(limit, list.size());
} catch (ApiCallException e) {
e.printStackTrace();
}
}
@Test
@ -65,9 +93,17 @@ public class FmUserNetworkTest {
int limit = 50;
FMObjList list = net.getTopTracks("7day", limit);
// list.stream().forEach(System.out::println);
assertEquals(limit, list.size());
FMObjList list;
try {
list = net.getTopTracks("7day", limit);
// list.stream().forEach(System.out::println);
assertEquals(limit, list.size());
} catch (ApiCallException e) {
e.printStackTrace();
}
}
@Test
@ -76,19 +112,30 @@ public class FmUserNetworkTest {
Track track = new TrackBuilder("shitsville", new ArtistBuilder("freddie gibbs").build()).build();
ArrayList<Scrobble> scrobbles = net.getTrackScrobbles(track);
ArrayList<Scrobble> scrobbles;
try {
scrobbles = net.getTrackScrobbles(track);
scrobbles.stream().forEach(System.out::println);
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
public void testFirstScrobbleDateTime() {
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();
}
}
}