adding status code check on http requests for error catching

This commit is contained in:
aj 2019-02-06 20:59:13 +00:00
parent 73373a6565
commit 6ecf2ad982
2 changed files with 435 additions and 343 deletions

View File

@ -28,6 +28,7 @@ public class FmNetwork {
this.key = key;
}
@Deprecated
public FmNetwork(String key, String userName) {
this.key = key;
this.userName = userName;
@ -61,6 +62,8 @@ public class FmNetwork {
HttpResponse<JsonNode> response = request.asJson();
if (response.getStatus() == 200) {
JSONObject obj = new JSONObject(response.getBody().toString());
String nameIn;
@ -97,7 +100,8 @@ public class FmNetwork {
builder.setListeners(albumJson.getInt("listeners"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
ConsoleHandler.getConsole()
.write("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
}
@ -142,6 +146,15 @@ public class FmNetwork {
System.err.println("ERROR: Album Name Not Found, " + e.getMessage());
}
} else {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR (getAlbum): " + name + " " + artist + " HTTP REQUEST ERROR");
else
System.err.println("ERROR (getAlbum): " + name + " " + artist + " HTTP REQUEST ERROR");
return null;
}
} catch (UnirestException e) {
e.printStackTrace();
}
@ -165,6 +178,8 @@ public class FmNetwork {
HttpResponse<JsonNode> response = request.asJson();
if (response.getStatus() == 200) {
JSONObject obj = new JSONObject(response.getBody().toString());
String artistName;
@ -181,7 +196,8 @@ public class FmNetwork {
builder.setMbid(artistJson.getString("mbid"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No MBID for " + artistName + " , " + e.getMessage());
ConsoleHandler.getConsole()
.write("ERROR: No MBID for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No MBID for " + artistName + " , " + e.getMessage());
}
@ -190,7 +206,8 @@ public class FmNetwork {
builder.setUrl(artistJson.getString("url"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No Url for " + artistName + " , " + e.getMessage());
ConsoleHandler.getConsole()
.write("ERROR: No Url for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No Url for " + artistName + " , " + e.getMessage());
}
@ -231,7 +248,8 @@ public class FmNetwork {
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No wiki for " + artistName + " , " + e.getMessage());
ConsoleHandler.getConsole()
.write("ERROR: No wiki for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No wiki for " + artistName + " , " + e.getMessage());
}
@ -244,6 +262,13 @@ public class FmNetwork {
else
System.err.println("ERROR: Arist Name Not Found, " + e.getMessage());
}
} else {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR (getArtist): " + name + " HTTP REQUEST ERROR");
else
System.err.println("ERROR (getArtist): " + name + " HTTP REQUEST ERROR");
return null;
}
} catch (UnirestException e) {
e.printStackTrace();
@ -269,6 +294,8 @@ public class FmNetwork {
HttpResponse<JsonNode> response = request.asJson();
if (response.getStatus() == 200) {
JSONObject obj = new JSONObject(response.getBody().toString());
String nameIn;
@ -305,7 +332,8 @@ public class FmNetwork {
builder.setListeners(trackJson.getInt("listeners"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
ConsoleHandler.getConsole()
.write("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
}
@ -350,6 +378,14 @@ public class FmNetwork {
System.err.println("ERROR: Album Name Not Found, " + e.getMessage());
}
} else {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR (getTrack): " + name + " " + artist + " HTTP REQUEST ERROR");
else
System.err.println("ERROR (getTrack): " + name + " " + artist + " HTTP REQUEST ERROR");
return null;
}
} catch (UnirestException e) {
e.printStackTrace();
}
@ -383,9 +419,12 @@ public class FmNetwork {
}
public FMObj refresh(FMObj obj) {
if(obj.getClass() == Track.class) return refresh((Track)obj);
if(obj.getClass() == Album.class) return refresh((Album)obj);
if(obj.getClass() == Artist.class) return refresh((Artist)obj);
if (obj.getClass() == Track.class)
return refresh((Track) obj);
if (obj.getClass() == Album.class)
return refresh((Album) obj);
if (obj.getClass() == Artist.class)
return refresh((Artist) obj);
return null;
}
}

View File

@ -25,7 +25,9 @@ public class FmUserNetwork extends FmNetwork {
}
public FmUserNetwork(String key, String userName) {
super(key, userName);
// super(key, userName);
super(key);
this.userName = userName;
}
public User getUser() {
@ -38,12 +40,22 @@ public class FmUserNetwork extends FmNetwork {
.queryString("method", "user.getinfo").queryString("user", userName).queryString("api_key", key)
.queryString("format", "json").asJson();
if (response.getStatus() == 200) {
JSONObject obj = new JSONObject(response.getBody().toString()).getJSONObject("user");
return new User(obj.getString("name"), obj.getString("realname"), obj.getString("url"),
obj.getString("country"), obj.getInt("age"), obj.getString("gender").charAt(0),
obj.getInt("playcount"));
} else {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR (getUser): HTTP REQUEST ERROR");
else
System.err.println("ERROR (getUser): HTTP REQUEST ERROR");
return null;
}
} catch (UnirestException e) {
e.printStackTrace();
}
@ -75,6 +87,8 @@ public class FmUserNetwork extends FmNetwork {
.queryString("method", "user.getrecenttracks").queryString("user", userName)
.queryString("api_key", key).queryString("format", "json").queryString("limit", "1").asJson();
if (response.getStatus() == 200) {
JSONArray obj = new JSONObject(response.getBody().toString()).getJSONObject("recenttracks")
.getJSONArray("track");
@ -86,6 +100,14 @@ public class FmUserNetwork extends FmNetwork {
return trackObj;
} else {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR (getLastTrack): HTTP REQUEST ERROR");
else
System.err.println("ERROR (getLastTrack): HTTP REQUEST ERROR");
return null;
}
} catch (UnirestException e) {
e.printStackTrace();
}
@ -103,21 +125,27 @@ public class FmUserNetwork extends FmNetwork {
long epoch = local.atStartOfDay(zoneId).toEpochSecond();
try {
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/").
header("Accept", "application/json").
header("User-Agent", "fmframework").
queryString("method","user.getrecenttracks").
queryString("user", userName).
queryString("from", epoch).
queryString("limit", 1).
queryString("api_key", key).
queryString("format", "json").
asJson();
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/")
.header("Accept", "application/json").header("User-Agent", "fmframework")
.queryString("method", "user.getrecenttracks").queryString("user", userName)
.queryString("from", epoch).queryString("limit", 1).queryString("api_key", key)
.queryString("format", "json").asJson();
int total = new JSONObject(response.getBody().toString()).getJSONObject("recenttracks").getJSONObject("@attr").getInt("total");
if (response.getStatus() == 200) {
int total = new JSONObject(response.getBody().toString()).getJSONObject("recenttracks")
.getJSONObject("@attr").getInt("total");
return total;
} else {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR (getScrobblesToday): HTTP REQUEST ERROR");
else
System.err.println("ERROR (getScrobblesToday): HTTP REQUEST ERROR");
return 0;
}
} catch (UnirestException e) {
e.printStackTrace();
}
@ -136,22 +164,29 @@ public class FmUserNetwork extends FmNetwork {
long endEpoch = epoch + (24 * 60 * 60);
try {
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/").
header("Accept", "application/json").
header("User-Agent", "fmframework").
queryString("method","user.getrecenttracks").
queryString("user", userName).
queryString("from", epoch).
queryString("to", endEpoch).
queryString("limit", 1).
queryString("api_key", key).
queryString("format", "json").
asJson();
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/")
.header("Accept", "application/json").header("User-Agent", "fmframework")
.queryString("method", "user.getrecenttracks").queryString("user", userName)
.queryString("from", epoch).queryString("to", endEpoch).queryString("limit", 1)
.queryString("api_key", key).queryString("format", "json").asJson();
int total = new JSONObject(response.getBody().toString()).getJSONObject("recenttracks").getJSONObject("@attr").getInt("total");
if (response.getStatus() == 200) {
int total = new JSONObject(response.getBody().toString()).getJSONObject("recenttracks")
.getJSONObject("@attr").getInt("total");
return total;
} else {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR (getScrobbleCountByDate): " + day + " " + month + " "
+ year + " HTTP REQUEST ERROR");
else
System.err.println("ERROR (getScrobbleCountByDate): " + day + " " + month + " " + year
+ " HTTP REQUEST ERROR");
return 0;
}
} catch (UnirestException e) {
e.printStackTrace();
}
@ -171,22 +206,28 @@ public class FmUserNetwork extends FmNetwork {
long endEpoch = epoch + (24 * 60 * 60);
try {
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/").
header("Accept", "application/json").
header("User-Agent", "fmframework").
queryString("method","user.getrecenttracks").
queryString("user", userName).
queryString("from", epoch).
queryString("to", endEpoch).
queryString("limit", 1).
queryString("api_key", key).
queryString("format", "json").
asJson();
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/")
.header("Accept", "application/json").header("User-Agent", "fmframework")
.queryString("method", "user.getrecenttracks").queryString("user", userName)
.queryString("from", epoch).queryString("to", endEpoch).queryString("limit", 1)
.queryString("api_key", key).queryString("format", "json").asJson();
int total = new JSONObject(response.getBody().toString()).getJSONObject("recenttracks").getJSONObject("@attr").getInt("total");
if (response.getStatus() == 200) {
int total = new JSONObject(response.getBody().toString()).getJSONObject("recenttracks")
.getJSONObject("@attr").getInt("total");
return total;
} else {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR (getScrobbleCountByDeltaDay): " + day + " HTTP REQUEST ERROR");
else
System.err.println("ERROR (getScrobbleCountByDeltaDay): " + day + " HTTP REQUEST ERROR");
return 0;
}
} catch (UnirestException e) {
e.printStackTrace();
}
@ -199,16 +240,15 @@ public class FmUserNetwork extends FmNetwork {
ConsoleHandler.getConsole().write(">>getTags");
try {
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/").
header("Accept", "application/json").
header("User-Agent", "fmframework").
queryString("method","user.gettoptags").
queryString("user", userName).
queryString("api_key", key).
queryString("format", "json").
asJson();
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/")
.header("Accept", "application/json").header("User-Agent", "fmframework")
.queryString("method", "user.gettoptags").queryString("user", userName).queryString("api_key", key)
.queryString("format", "json").asJson();
JSONArray tagJsonArray = new JSONObject(response.getBody().toString()).getJSONObject("toptags").getJSONArray("tag");
if (response.getStatus() == 200) {
JSONArray tagJsonArray = new JSONObject(response.getBody().toString()).getJSONObject("toptags")
.getJSONArray("tag");
JSONObject tagJson;
@ -227,6 +267,14 @@ public class FmUserNetwork extends FmNetwork {
return tags;
} else {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR (getTags): HTTP REQUEST ERROR");
else
System.err.println("ERROR (getTags): HTTP REQUEST ERROR");
return null;
}
} catch (UnirestException e) {
e.printStackTrace();
}
@ -240,19 +288,16 @@ public class FmUserNetwork extends FmNetwork {
ConsoleHandler.getConsole().write(">>getTag: " + tagName);
try {
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/").
header("Accept", "application/json").
header("User-Agent", "fmframework").
queryString("method","user.getpersonaltags").
queryString("user", userName).
queryString("tag", tagName).
queryString("taggingtype", "artist").
queryString("limit", 70).
queryString("api_key", key).
queryString("format", "json").
asJson();
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/")
.header("Accept", "application/json").header("User-Agent", "fmframework")
.queryString("method", "user.getpersonaltags").queryString("user", userName)
.queryString("tag", tagName).queryString("taggingtype", "artist").queryString("limit", 70)
.queryString("api_key", key).queryString("format", "json").asJson();
JSONArray tagJsonArray = new JSONObject(response.getBody().toString()).getJSONObject("taggings").getJSONObject("artists").getJSONArray("artist");
if (response.getStatus() == 200) {
JSONArray tagJsonArray = new JSONObject(response.getBody().toString()).getJSONObject("taggings")
.getJSONObject("artists").getJSONArray("artist");
JSONObject artistJson;
@ -276,6 +321,14 @@ public class FmUserNetwork extends FmNetwork {
return list;
} else {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR (getTag): " + tagName + " HTTP REQUEST ERROR");
else
System.err.println("ERROR (getTag): " + tagName + " HTTP REQUEST ERROR");
return null;
}
} catch (UnirestException e) {
e.printStackTrace();
}