completed abstraction of http requests

This commit is contained in:
aj 2019-05-01 19:13:49 +01:00
parent 69627ffe96
commit 42d327c969
3 changed files with 349 additions and 494 deletions

View File

@ -52,113 +52,92 @@ public class FmNetwork {
if (ConsoleHandler.isVerbose()) if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getAlbum: " + name + " " + artist); ConsoleHandler.getConsole().write(">>getAlbum: " + name + " " + artist);
HttpRequest request; HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("album", name);
parameters.put("artist", artist);
if (userName != null)
parameters.put("username", userName);
JSONObject obj = makeGetRequest("album.getinfo", parameters);
String nameIn;
String artistIn;
try { try {
request = Unirest.get("http://ws.audioscrobbler.com/2.0/").header("Accept", "application/json")
.header("User-Agent", "fmframework").queryString("method", "album.getinfo")
.queryString("artist", artist).queryString("album", name).queryString("api_key", key)
.queryString("format", "json");
if (userName != null) JSONObject albumJson = obj.getJSONObject("album");
request.queryString("username", userName);
HttpResponse<JsonNode> response = request.asJson(); nameIn = albumJson.getString("name");
artistIn = albumJson.getString("artist");
if (response.getStatus() == 200) { AlbumBuilder builder = new AlbumBuilder(nameIn, getArtist(artistIn));
JSONObject obj = new JSONObject(response.getBody().toString()); try {
builder.setMbid(albumJson.getString("mbid"));
String nameIn; } catch (JSONException e) {
String artistIn;
try {
JSONObject albumJson = obj.getJSONObject("album");
nameIn = albumJson.getString("name");
artistIn = albumJson.getString("artist");
AlbumBuilder builder = new AlbumBuilder(nameIn, getArtist(artistIn));
try {
builder.setMbid(albumJson.getString("mbid"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No MBID for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No MBID for " + nameIn + " , " + e.getMessage());
}
try {
builder.setUrl(albumJson.getString("url"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No Url for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No Url for " + nameIn + " , " + e.getMessage());
}
try {
builder.setListeners(albumJson.getInt("listeners"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
}
try {
builder.setPlayCount(albumJson.getInt("playcount"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR: No play count for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No play count for " + nameIn + " , " + e.getMessage());
}
try {
builder.setUserPlayCount(albumJson.getInt("userplaycount"));
} catch (JSONException e) {
}
try {
JSONObject wikiJson = albumJson.getJSONObject("wiki");
Wiki wiki = new Wiki(wikiJson.getString("published"), wikiJson.getString("summary"),
wikiJson.getString("content"));
builder.setWiki(wiki);
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No wiki for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No wiki for " + nameIn + " , " + e.getMessage());
}
return builder.build();
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: Album Name Not Found, " + e.getMessage());
else
System.err.println("ERROR: Album Name Not Found, " + e.getMessage());
}
} else {
if (ConsoleHandler.isVerbose()) if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole() ConsoleHandler.getConsole().write("ERROR: No MBID for " + nameIn + " , " + e.getMessage());
.write("ERROR (getAlbum): " + name + " " + artist + " HTTP REQUEST ERROR");
else else
System.err.println("ERROR (getAlbum): " + name + " " + artist + " HTTP REQUEST ERROR"); System.err.println("ERROR: No MBID for " + nameIn + " , " + e.getMessage());
return null;
} }
} catch (UnirestException e) { try {
e.printStackTrace(); builder.setUrl(albumJson.getString("url"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No Url for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No Url for " + nameIn + " , " + e.getMessage());
}
try {
builder.setListeners(albumJson.getInt("listeners"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
}
try {
builder.setPlayCount(albumJson.getInt("playcount"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No play count for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No play count for " + nameIn + " , " + e.getMessage());
}
try {
builder.setUserPlayCount(albumJson.getInt("userplaycount"));
} catch (JSONException e) {
}
try {
JSONObject wikiJson = albumJson.getJSONObject("wiki");
Wiki wiki = new Wiki(wikiJson.getString("published"), wikiJson.getString("summary"),
wikiJson.getString("content"));
builder.setWiki(wiki);
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No wiki for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No wiki for " + nameIn + " , " + e.getMessage());
}
return builder.build();
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: Album Name Not Found, " + e.getMessage());
else
System.err.println("ERROR: Album Name Not Found, " + e.getMessage());
} }
return null; return null;
@ -175,121 +154,91 @@ public class FmNetwork {
if (ConsoleHandler.isVerbose()) if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getArtist: " + name); ConsoleHandler.getConsole().write(">>getArtist: " + name);
//HttpRequest request; HashMap<String, String> parameters = new HashMap<String, String>();
// try {
// request = Unirest.get("http://ws.audioscrobbler.com/2.0/").header("Accept", "application/json")
// .header("User-Agent", "fmframework").queryString("method", "artist.getinfo")
// .queryString("artist", name).queryString("api_key", key).queryString("format", "json");
HashMap<String, String> parameters = new HashMap<String, String>(); parameters.put("artist", name);
parameters.put("artist", name); if (userName != null)
parameters.put("username", userName);
if (userName != null) JSONObject obj = makeGetRequest("artist.getinfo", parameters);
parameters.put("username", userName);
//request.queryString("username", userName);
// HttpResponse<JsonNode> response = request.asJson(); String artistName;
if (true) { try {
// if (response.getStatus() == 200) {
// JSONObject obj = new JSONObject(response.getBody().toString()); JSONObject artistJson = obj.getJSONObject("artist");
JSONObject obj = makeGetRequest("artist.getinfo", parameters); artistName = artistJson.getString("name");
String artistName; ArtistBuilder builder = new ArtistBuilder(artistName);
try { try {
builder.setMbid(artistJson.getString("mbid"));
JSONObject artistJson = obj.getJSONObject("artist"); } catch (JSONException e) {
artistName = artistJson.getString("name");
ArtistBuilder builder = new ArtistBuilder(artistName);
try {
builder.setMbid(artistJson.getString("mbid"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR: No MBID for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No MBID for " + artistName + " , " + e.getMessage());
}
try {
builder.setUrl(artistJson.getString("url"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR: No Url for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No Url for " + artistName + " , " + e.getMessage());
}
try {
builder.setListeners(artistJson.getJSONObject("stats").getInt("listeners"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR: No listeners for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No listeners for " + artistName + " , " + e.getMessage());
}
try {
builder.setPlayCount(artistJson.getJSONObject("stats").getInt("playcount"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR: No play count for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No play count for " + artistName + " , " + e.getMessage());
}
try {
builder.setUserPlayCount(artistJson.getJSONObject("stats").getInt("userplaycount"));
} catch (JSONException e) {
}
try {
JSONObject wikiJson = artistJson.getJSONObject("bio");
Wiki wiki = new Wiki(wikiJson.getString("published"), wikiJson.getString("summary"),
wikiJson.getString("content"));
builder.setWiki(wiki);
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR: No wiki for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No wiki for " + artistName + " , " + e.getMessage());
}
return builder.build();
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: Arist Name Not Found, " + e.getMessage());
else
System.err.println("ERROR: Arist Name Not Found, " + e.getMessage());
}
} else {
if (ConsoleHandler.isVerbose()) if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR (getArtist): " + name + " HTTP REQUEST ERROR"); ConsoleHandler.getConsole().write("ERROR: No MBID for " + artistName + " , " + e.getMessage());
else else
System.err.println("ERROR (getArtist): " + name + " HTTP REQUEST ERROR"); System.err.println("ERROR: No MBID for " + artistName + " , " + e.getMessage());
return null;
} }
// } try {
// catch (UnirestException e) { builder.setUrl(artistJson.getString("url"));
// e.printStackTrace(); } catch (JSONException e) {
// } if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No Url for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No Url for " + artistName + " , " + e.getMessage());
}
try {
builder.setListeners(artistJson.getJSONObject("stats").getInt("listeners"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No listeners for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No listeners for " + artistName + " , " + e.getMessage());
}
try {
builder.setPlayCount(artistJson.getJSONObject("stats").getInt("playcount"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR: No play count for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No play count for " + artistName + " , " + e.getMessage());
}
try {
builder.setUserPlayCount(artistJson.getJSONObject("stats").getInt("userplaycount"));
} catch (JSONException e) {
}
try {
JSONObject wikiJson = artistJson.getJSONObject("bio");
Wiki wiki = new Wiki(wikiJson.getString("published"), wikiJson.getString("summary"),
wikiJson.getString("content"));
builder.setWiki(wiki);
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No wiki for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No wiki for " + artistName + " , " + e.getMessage());
}
return builder.build();
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: Arist Name Not Found, " + e.getMessage());
else
System.err.println("ERROR: Arist Name Not Found, " + e.getMessage());
}
return null; return null;
@ -306,113 +255,92 @@ public class FmNetwork {
if (ConsoleHandler.isVerbose()) if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getTrack: " + name + " " + artist); ConsoleHandler.getConsole().write(">>getTrack: " + name + " " + artist);
HttpRequest request; HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("artist", artist);
parameters.put("track", name);
if (userName != null)
parameters.put("username", userName);
JSONObject obj = makeGetRequest("track.getinfo", parameters);
String nameIn;
String artistIn;
try { try {
request = Unirest.get("http://ws.audioscrobbler.com/2.0/").header("Accept", "application/json")
.header("User-Agent", "fmframework").queryString("method", "track.getinfo")
.queryString("artist", artist).queryString("track", name).queryString("api_key", key)
.queryString("format", "json");
if (userName != null) JSONObject trackJson = obj.getJSONObject("track");
request.queryString("username", userName);
HttpResponse<JsonNode> response = request.asJson(); nameIn = trackJson.getString("name");
artistIn = trackJson.getJSONObject("artist").getString("name");
if (response.getStatus() == 200) { TrackBuilder builder = new TrackBuilder(nameIn, getArtist(artistIn));
JSONObject obj = new JSONObject(response.getBody().toString()); try {
builder.setMbid(trackJson.getString("mbid"));
String nameIn; } catch (JSONException e) {
String artistIn;
try {
JSONObject trackJson = obj.getJSONObject("track");
nameIn = trackJson.getString("name");
artistIn = trackJson.getJSONObject("artist").getString("name");
TrackBuilder builder = new TrackBuilder(nameIn, getArtist(artistIn));
try {
builder.setMbid(trackJson.getString("mbid"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No MBID for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No MBID for " + nameIn + " , " + e.getMessage());
}
try {
builder.setUrl(trackJson.getString("url"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No Url for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No Url for " + nameIn + " , " + e.getMessage());
}
try {
builder.setListeners(trackJson.getInt("listeners"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
}
try {
builder.setPlayCount(trackJson.getInt("playcount"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR: No play count for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No play count for " + nameIn + " , " + e.getMessage());
}
try {
builder.setUserPlayCount(trackJson.getInt("userplaycount"));
} catch (JSONException e) {
}
try {
JSONObject wikiJson = trackJson.getJSONObject("wiki");
Wiki wiki = new Wiki(wikiJson.getString("published"), wikiJson.getString("summary"),
wikiJson.getString("content"));
builder.setWiki(wiki);
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No wiki for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No wiki for " + nameIn + " , " + e.getMessage());
}
return builder.build();
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: Album Name Not Found, " + e.getMessage());
else
System.err.println("ERROR: Album Name Not Found, " + e.getMessage());
}
} else {
if (ConsoleHandler.isVerbose()) if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole() ConsoleHandler.getConsole().write("ERROR: No MBID for " + nameIn + " , " + e.getMessage());
.write("ERROR (getTrack): " + name + " " + artist + " HTTP REQUEST ERROR");
else else
System.err.println("ERROR (getTrack): " + name + " " + artist + " HTTP REQUEST ERROR"); System.err.println("ERROR: No MBID for " + nameIn + " , " + e.getMessage());
return null;
} }
} catch (UnirestException e) { try {
e.printStackTrace(); builder.setUrl(trackJson.getString("url"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No Url for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No Url for " + nameIn + " , " + e.getMessage());
}
try {
builder.setListeners(trackJson.getInt("listeners"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
}
try {
builder.setPlayCount(trackJson.getInt("playcount"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No play count for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No play count for " + nameIn + " , " + e.getMessage());
}
try {
builder.setUserPlayCount(trackJson.getInt("userplaycount"));
} catch (JSONException e) {
}
try {
JSONObject wikiJson = trackJson.getJSONObject("wiki");
Wiki wiki = new Wiki(wikiJson.getString("published"), wikiJson.getString("summary"),
wikiJson.getString("content"));
builder.setWiki(wiki);
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No wiki for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No wiki for " + nameIn + " , " + e.getMessage());
}
return builder.build();
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: Album Name Not Found, " + e.getMessage());
else
System.err.println("ERROR: Album Name Not Found, " + e.getMessage());
} }
return null; return null;
@ -479,16 +407,32 @@ public class FmNetwork {
protected JSONObject makeGetRequest(String method, HashMap<String, String> parameters) { protected JSONObject makeGetRequest(String method, HashMap<String, String> parameters) {
return makeGetRequest(method, parameters, null);
}
protected JSONObject makeGetRequest(String method, HashMap<String, String> parameters,
HashMap<String, String> headers) {
HttpRequest request; HttpRequest request;
try { try {
request = Unirest.get("http://ws.audioscrobbler.com/2.0/").header("Accept", "application/json") request = Unirest.get("http://ws.audioscrobbler.com/2.0/").header("Accept", "application/json")
.header("User-Agent", "fmframework").queryString("api_key", key).queryString("format", "json"); .header("User-Agent", "fmframework").queryString("method", method);
for (String key : parameters.keySet()) { if (headers != null) {
request = request.queryString(key, parameters.get(key)); for (String key : headers.keySet()) {
System.out.println(key + " " + parameters.get(key)); request = request.header(key, headers.get(key));
}
} }
if (parameters != null) {
for (String key : parameters.keySet()) {
request = request.queryString(key, parameters.get(key));
}
}
request = request.queryString("api_key", key).queryString("format", "json");
HttpResponse<JsonNode> response = request.asJson(); HttpResponse<JsonNode> response = request.asJson();
if (response.getStatus() == 200) { if (response.getStatus() == 200) {
@ -496,10 +440,11 @@ public class FmNetwork {
return new JSONObject(response.getBody().toString()); return new JSONObject(response.getBody().toString());
} else { } else {
System.out.println(response.getBody());
if (ConsoleHandler.isVerbose()) if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR (getAlbum): HTTP REQUEST ERROR"); ConsoleHandler.getConsole().write("ERROR : HTTP Request Error " + response.getStatus());
else else
System.err.println("ERROR (getAlbum): HTTP REQUEST ERROR"); System.err.println("ERROR : HTTP Request Error " + response.getStatus());
return null; return null;
} }
} catch (UnirestException e) { } catch (UnirestException e) {
@ -508,4 +453,15 @@ public class FmNetwork {
return null; return null;
} }
protected class RequestParam {
int intparam;
String strparam;
public RequestParam(int param) {
}
}
} }

View File

@ -3,6 +3,7 @@ package sarsoo.fmframework.fm;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
@ -27,7 +28,7 @@ public class FmUserNetwork extends FmNetwork {
/** /**
* FmNetwork for user specific Last.FM information * FmNetwork for user specific Last.FM information
* *
* @param key Last.FM API Key * @param key Last.FM API Key
* @param userName Last.FM username * @param userName Last.FM username
*/ */
public FmUserNetwork(String key, String userName) { public FmUserNetwork(String key, String userName) {
@ -70,33 +71,16 @@ public class FmUserNetwork extends FmNetwork {
if (ConsoleHandler.isVerbose()) if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getUser"); ConsoleHandler.getConsole().write(">>getUser");
try { HashMap<String, String> parameters = new HashMap<String, String>();
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/")
.header("Accept", "application/json").header("User-Agent", "fmframework")
.queryString("method", "user.getinfo").queryString("user", userName).queryString("api_key", key)
.queryString("format", "json").asJson();
if (response.getStatus() == 200) { parameters.put("user", userName);
JSONObject obj = new JSONObject(response.getBody().toString()).getJSONObject("user"); JSONObject obj = makeGetRequest("user.getinfo", parameters).getJSONObject("user");
return new User(obj.getString("name"), obj.getString("realname"), obj.getString("url"), return new User(obj.getString("name"), obj.getString("realname"), obj.getString("url"),
obj.getString("country"), obj.getInt("age"), obj.getString("gender").charAt(0), obj.getString("country"), obj.getInt("age"), obj.getString("gender").charAt(0),
obj.getInt("playcount")); 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();
}
return null;
} }
/** /**
@ -132,38 +116,22 @@ public class FmUserNetwork extends FmNetwork {
if (ConsoleHandler.isVerbose()) if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getLastTrack"); ConsoleHandler.getConsole().write(">>getLastTrack");
try { HashMap<String, String> parameters = new HashMap<String, String>();
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("api_key", key).queryString("format", "json").queryString("limit", "1").asJson();
if (response.getStatus() == 200) { parameters.put("user", userName);
parameters.put("limit", "1");
JSONArray obj = new JSONObject(response.getBody().toString()).getJSONObject("recenttracks") JSONArray obj = makeGetRequest("user.getrecenttracks", parameters).getJSONObject("recenttracks")
.getJSONArray("track"); .getJSONArray("track");
JSONObject track = (JSONObject) obj.get(0); JSONObject track = (JSONObject) obj.get(0);
Track trackObj = getTrack(track.getString("name"), track.getJSONObject("artist").getString("#text")); Track trackObj = getTrack(track.getString("name"), track.getJSONObject("artist").getString("#text"));
trackObj.setAlbum(getAlbum(track.getJSONObject("album").getString("#text"), trackObj.setAlbum(getAlbum(track.getJSONObject("album").getString("#text"),
track.getJSONObject("artist").getString("#text"))); track.getJSONObject("artist").getString("#text")));
return trackObj; 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();
}
return null;
} }
/** /**
@ -180,41 +148,26 @@ public class FmUserNetwork extends FmNetwork {
ZoneId zoneId = ZoneId.systemDefault(); ZoneId zoneId = ZoneId.systemDefault();
long epoch = local.atStartOfDay(zoneId).toEpochSecond(); long epoch = local.atStartOfDay(zoneId).toEpochSecond();
try { HashMap<String, String> parameters = new HashMap<String, String>();
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();
if (response.getStatus() == 200) { parameters.put("user", userName);
parameters.put("from", Long.toString(epoch));
parameters.put("limit", "1");
int total = new JSONObject(response.getBody().toString()).getJSONObject("recenttracks") JSONObject obj = makeGetRequest("user.getrecenttracks", parameters);
.getJSONObject("@attr").getInt("total");
return total; int total = obj.getJSONObject("recenttracks").getJSONObject("@attr").getInt("total");
} else { return total;
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();
}
return 0;
} }
/** /**
* Return scrobble count by date * Return scrobble count by date
* *
* @param day Day int * @param day Day int
* @param month Month int * @param month Month int
* @param year Year int * @param year Year int
* @return Scrobble count * @return Scrobble count
*/ */
public int getScrobbleCountByDate(int day, int month, int year) { public int getScrobbleCountByDate(int day, int month, int year) {
@ -227,40 +180,23 @@ public class FmUserNetwork extends FmNetwork {
long epoch = startDate.atStartOfDay(zoneId).toEpochSecond(); long epoch = startDate.atStartOfDay(zoneId).toEpochSecond();
long endEpoch = epoch + (24 * 60 * 60); long endEpoch = epoch + (24 * 60 * 60);
try { HashMap<String, String> parameters = new HashMap<String, String>();
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();
if (response.getStatus() == 200) { parameters.put("user", userName);
parameters.put("from", Long.toString(epoch));
parameters.put("to", Long.toString(endEpoch));
parameters.put("limit", "1");
int total = new JSONObject(response.getBody().toString()).getJSONObject("recenttracks") JSONObject obj = makeGetRequest("user.getrecenttracks", parameters);
.getJSONObject("@attr").getInt("total");
return total; int total = obj.getJSONObject("recenttracks").getJSONObject("@attr").getInt("total");
} else { return total;
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();
}
return 0;
} }
/** /**
* Returns scrobble count of day * Returns scrobble count of day by today - {int day}
* by today - {int day}
* *
* @param day Negative day offset * @param day Negative day offset
* @return Scrobble count * @return Scrobble count
@ -276,34 +212,19 @@ public class FmUserNetwork extends FmNetwork {
epoch -= (day * (24 * 60 * 60)); epoch -= (day * (24 * 60 * 60));
long endEpoch = epoch + (24 * 60 * 60); long endEpoch = epoch + (24 * 60 * 60);
try { HashMap<String, String> parameters = new HashMap<String, String>();
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();
if (response.getStatus() == 200) { parameters.put("user", userName);
parameters.put("from", Long.toString(epoch));
parameters.put("to", Long.toString(endEpoch));
parameters.put("limit", "1");
int total = new JSONObject(response.getBody().toString()).getJSONObject("recenttracks") JSONObject obj = makeGetRequest("user.getrecenttracks", parameters);
.getJSONObject("@attr").getInt("total");
return total; int total = obj.getJSONObject("recenttracks").getJSONObject("@attr").getInt("total");
} else { return total;
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();
}
return 0;
} }
/** /**
@ -315,47 +236,30 @@ public class FmUserNetwork extends FmNetwork {
if (ConsoleHandler.isVerbose()) if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getTags"); ConsoleHandler.getConsole().write(">>getTags");
try { HashMap<String, String> parameters = new HashMap<String, String>();
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();
if (response.getStatus() == 200) { parameters.put("user", userName);
JSONArray tagJsonArray = new JSONObject(response.getBody().toString()).getJSONObject("toptags") JSONObject obj = makeGetRequest("user.gettoptags", parameters);
.getJSONArray("tag");
JSONObject tagJson; JSONArray tagJsonArray = obj.getJSONObject("toptags").getJSONArray("tag");
ArrayList<Tag> tags = new ArrayList<Tag>(); JSONObject tagJson;
int counter; ArrayList<Tag> tags = new ArrayList<Tag>();
for (counter = 0; counter < tagJsonArray.length(); counter++) {
tagJson = (JSONObject) tagJsonArray.get(counter); int counter;
for (counter = 0; counter < tagJsonArray.length(); counter++) {
Tag tag = new Tag(tagJson.getString("name"), tagJson.getString("url"), tagJson.getInt("count")); tagJson = (JSONObject) tagJsonArray.get(counter);
tags.add(tag); Tag tag = new Tag(tagJson.getString("name"), tagJson.getString("url"), tagJson.getInt("count"));
} tags.add(tag);
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();
} }
return null; return tags;
} }
@ -369,53 +273,39 @@ public class FmUserNetwork extends FmNetwork {
if (ConsoleHandler.isVerbose()) if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getTag: " + tagName); ConsoleHandler.getConsole().write(">>getTag: " + tagName);
try { HashMap<String, String> parameters = new HashMap<String, String>();
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();
if (response.getStatus() == 200) { parameters.put("user", userName);
parameters.put("tag", tagName);
parameters.put("taggingtype", "artist");
parameters.put("limit", "70");
JSONArray tagJsonArray = new JSONObject(response.getBody().toString()).getJSONObject("taggings") JSONObject obj = makeGetRequest("user.getpersonaltags", parameters);
.getJSONObject("artists").getJSONArray("artist");
JSONObject artistJson; JSONArray tagJsonArray = obj.getJSONObject("taggings")
.getJSONObject("artists").getJSONArray("artist");
FMObjList list = new FMObjList(); JSONObject artistJson;
list.setGroupName(tagName); FMObjList list = new FMObjList();
int counter; list.setGroupName(tagName);
for (counter = 0; counter < tagJsonArray.length(); counter++) {
artistJson = (JSONObject) tagJsonArray.get(counter); int counter;
for (counter = 0; counter < tagJsonArray.length(); counter++) {
Artist artist = getArtist(artistJson.getString("name")); artistJson = (JSONObject) tagJsonArray.get(counter);
if (ConsoleHandler.isVerbose()) Artist artist = getArtist(artistJson.getString("name"));
ConsoleHandler.getConsole().write(">Tag: " + tagName + ", " + artist.getName());
list.add(artist); if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">Tag: " + tagName + ", " + artist.getName());
} list.add(artist);
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();
} }
return null; return list;
} }

View File

@ -36,4 +36,13 @@ public class FmNetworkTest {
assertEquals(artist.getName(), "Kendrick Lamar"); assertEquals(artist.getName(), "Kendrick Lamar");
} }
@Test
public void testNullParameters() {
FmNetwork network = new FmNetwork(Key.getKey());
network.makeGetRequest("artist.getinfo", null);
assertTrue(true);
}
} }