completed abstraction of http requests
This commit is contained in:
parent
69627ffe96
commit
42d327c969
@ -52,113 +52,92 @@ public class FmNetwork {
|
||||
if (ConsoleHandler.isVerbose())
|
||||
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 {
|
||||
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)
|
||||
request.queryString("username", userName);
|
||||
JSONObject albumJson = obj.getJSONObject("album");
|
||||
|
||||
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());
|
||||
|
||||
String nameIn;
|
||||
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 {
|
||||
try {
|
||||
builder.setMbid(albumJson.getString("mbid"));
|
||||
} catch (JSONException e) {
|
||||
if (ConsoleHandler.isVerbose())
|
||||
ConsoleHandler.getConsole()
|
||||
.write("ERROR (getAlbum): " + name + " " + artist + " HTTP REQUEST ERROR");
|
||||
ConsoleHandler.getConsole().write("ERROR: No MBID for " + nameIn + " , " + e.getMessage());
|
||||
else
|
||||
System.err.println("ERROR (getAlbum): " + name + " " + artist + " HTTP REQUEST ERROR");
|
||||
return null;
|
||||
System.err.println("ERROR: No MBID for " + nameIn + " , " + e.getMessage());
|
||||
}
|
||||
|
||||
} catch (UnirestException e) {
|
||||
e.printStackTrace();
|
||||
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());
|
||||
}
|
||||
|
||||
return null;
|
||||
@ -175,121 +154,91 @@ public class FmNetwork {
|
||||
if (ConsoleHandler.isVerbose())
|
||||
ConsoleHandler.getConsole().write(">>getArtist: " + name);
|
||||
|
||||
//HttpRequest request;
|
||||
// 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>();
|
||||
|
||||
HashMap<String, String> parameters = new HashMap<String, String>();
|
||||
|
||||
parameters.put("artist", name);
|
||||
|
||||
if (userName != null)
|
||||
parameters.put("username", userName);
|
||||
//request.queryString("username", userName);
|
||||
parameters.put("artist", name);
|
||||
|
||||
// HttpResponse<JsonNode> response = request.asJson();
|
||||
if (userName != null)
|
||||
parameters.put("username", userName);
|
||||
|
||||
if (true) {
|
||||
// if (response.getStatus() == 200) {
|
||||
JSONObject obj = makeGetRequest("artist.getinfo", parameters);
|
||||
|
||||
// JSONObject obj = new JSONObject(response.getBody().toString());
|
||||
String artistName;
|
||||
|
||||
JSONObject obj = makeGetRequest("artist.getinfo", parameters);
|
||||
|
||||
String artistName;
|
||||
try {
|
||||
|
||||
try {
|
||||
JSONObject artistJson = obj.getJSONObject("artist");
|
||||
|
||||
JSONObject artistJson = obj.getJSONObject("artist");
|
||||
artistName = artistJson.getString("name");
|
||||
|
||||
artistName = artistJson.getString("name");
|
||||
ArtistBuilder builder = new ArtistBuilder(artistName);
|
||||
|
||||
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 {
|
||||
try {
|
||||
builder.setMbid(artistJson.getString("mbid"));
|
||||
} catch (JSONException e) {
|
||||
if (ConsoleHandler.isVerbose())
|
||||
ConsoleHandler.getConsole().write("ERROR (getArtist): " + name + " HTTP REQUEST ERROR");
|
||||
ConsoleHandler.getConsole().write("ERROR: No MBID for " + artistName + " , " + e.getMessage());
|
||||
else
|
||||
System.err.println("ERROR (getArtist): " + name + " HTTP REQUEST ERROR");
|
||||
return null;
|
||||
System.err.println("ERROR: No MBID for " + artistName + " , " + e.getMessage());
|
||||
}
|
||||
|
||||
// }
|
||||
// catch (UnirestException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
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());
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@ -306,113 +255,92 @@ public class FmNetwork {
|
||||
if (ConsoleHandler.isVerbose())
|
||||
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 {
|
||||
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)
|
||||
request.queryString("username", userName);
|
||||
JSONObject trackJson = obj.getJSONObject("track");
|
||||
|
||||
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());
|
||||
|
||||
String nameIn;
|
||||
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 {
|
||||
try {
|
||||
builder.setMbid(trackJson.getString("mbid"));
|
||||
} catch (JSONException e) {
|
||||
if (ConsoleHandler.isVerbose())
|
||||
ConsoleHandler.getConsole()
|
||||
.write("ERROR (getTrack): " + name + " " + artist + " HTTP REQUEST ERROR");
|
||||
ConsoleHandler.getConsole().write("ERROR: No MBID for " + nameIn + " , " + e.getMessage());
|
||||
else
|
||||
System.err.println("ERROR (getTrack): " + name + " " + artist + " HTTP REQUEST ERROR");
|
||||
return null;
|
||||
System.err.println("ERROR: No MBID for " + nameIn + " , " + e.getMessage());
|
||||
}
|
||||
|
||||
} catch (UnirestException e) {
|
||||
e.printStackTrace();
|
||||
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());
|
||||
}
|
||||
|
||||
return null;
|
||||
@ -479,27 +407,44 @@ public class FmNetwork {
|
||||
|
||||
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;
|
||||
try {
|
||||
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()) {
|
||||
request = request.queryString(key, parameters.get(key));
|
||||
System.out.println(key + " " + parameters.get(key));
|
||||
if (headers != null) {
|
||||
for (String key : headers.keySet()) {
|
||||
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();
|
||||
|
||||
if (response.getStatus() == 200) {
|
||||
|
||||
|
||||
return new JSONObject(response.getBody().toString());
|
||||
|
||||
|
||||
} else {
|
||||
System.out.println(response.getBody());
|
||||
if (ConsoleHandler.isVerbose())
|
||||
ConsoleHandler.getConsole().write("ERROR (getAlbum): HTTP REQUEST ERROR");
|
||||
ConsoleHandler.getConsole().write("ERROR : HTTP Request Error " + response.getStatus());
|
||||
else
|
||||
System.err.println("ERROR (getAlbum): HTTP REQUEST ERROR");
|
||||
System.err.println("ERROR : HTTP Request Error " + response.getStatus());
|
||||
return null;
|
||||
}
|
||||
} catch (UnirestException e) {
|
||||
@ -508,4 +453,15 @@ public class FmNetwork {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected class RequestParam {
|
||||
|
||||
int intparam;
|
||||
String strparam;
|
||||
|
||||
public RequestParam(int param) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package sarsoo.fmframework.fm;
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
@ -27,7 +28,7 @@ public class FmUserNetwork extends FmNetwork {
|
||||
/**
|
||||
* FmNetwork for user specific Last.FM information
|
||||
*
|
||||
* @param key Last.FM API Key
|
||||
* @param key Last.FM API Key
|
||||
* @param userName Last.FM username
|
||||
*/
|
||||
public FmUserNetwork(String key, String userName) {
|
||||
@ -35,7 +36,7 @@ public class FmUserNetwork extends FmNetwork {
|
||||
super(key);
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set network's Last.FM username
|
||||
*
|
||||
@ -70,33 +71,16 @@ public class FmUserNetwork extends FmNetwork {
|
||||
if (ConsoleHandler.isVerbose())
|
||||
ConsoleHandler.getConsole().write(">>getUser");
|
||||
|
||||
try {
|
||||
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();
|
||||
HashMap<String, String> parameters = new HashMap<String, String>();
|
||||
|
||||
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"),
|
||||
obj.getString("country"), obj.getInt("age"), obj.getString("gender").charAt(0),
|
||||
obj.getInt("playcount"));
|
||||
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();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -132,38 +116,22 @@ public class FmUserNetwork extends FmNetwork {
|
||||
if (ConsoleHandler.isVerbose())
|
||||
ConsoleHandler.getConsole().write(">>getLastTrack");
|
||||
|
||||
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("api_key", key).queryString("format", "json").queryString("limit", "1").asJson();
|
||||
HashMap<String, String> parameters = new HashMap<String, String>();
|
||||
|
||||
if (response.getStatus() == 200) {
|
||||
parameters.put("user", userName);
|
||||
parameters.put("limit", "1");
|
||||
|
||||
JSONArray obj = new JSONObject(response.getBody().toString()).getJSONObject("recenttracks")
|
||||
.getJSONArray("track");
|
||||
JSONArray obj = makeGetRequest("user.getrecenttracks", parameters).getJSONObject("recenttracks")
|
||||
.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"));
|
||||
trackObj.setAlbum(getAlbum(track.getJSONObject("album").getString("#text"),
|
||||
track.getJSONObject("artist").getString("#text")));
|
||||
Track trackObj = getTrack(track.getString("name"), track.getJSONObject("artist").getString("#text"));
|
||||
trackObj.setAlbum(getAlbum(track.getJSONObject("album").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();
|
||||
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();
|
||||
HashMap<String, String> parameters = new HashMap<String, String>();
|
||||
|
||||
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")
|
||||
.getJSONObject("@attr").getInt("total");
|
||||
JSONObject obj = makeGetRequest("user.getrecenttracks", parameters);
|
||||
|
||||
return total;
|
||||
int total = obj.getJSONObject("recenttracks").getJSONObject("@attr").getInt("total");
|
||||
|
||||
} else {
|
||||
if (ConsoleHandler.isVerbose())
|
||||
ConsoleHandler.getConsole().write("ERROR (getScrobblesToday): HTTP REQUEST ERROR");
|
||||
else
|
||||
System.err.println("ERROR (getScrobblesToday): HTTP REQUEST ERROR");
|
||||
return 0;
|
||||
}
|
||||
return total;
|
||||
|
||||
} catch (UnirestException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return scrobble count by date
|
||||
*
|
||||
* @param day Day int
|
||||
* @param day Day int
|
||||
* @param month Month int
|
||||
* @param year Year int
|
||||
* @param year Year int
|
||||
* @return Scrobble count
|
||||
*/
|
||||
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 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();
|
||||
HashMap<String, String> parameters = new HashMap<String, String>();
|
||||
|
||||
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")
|
||||
.getJSONObject("@attr").getInt("total");
|
||||
JSONObject obj = makeGetRequest("user.getrecenttracks", parameters);
|
||||
|
||||
return total;
|
||||
int total = obj.getJSONObject("recenttracks").getJSONObject("@attr").getInt("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;
|
||||
}
|
||||
return total;
|
||||
|
||||
} catch (UnirestException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns scrobble count of day
|
||||
* by today - {int day}
|
||||
* Returns scrobble count of day by today - {int day}
|
||||
*
|
||||
* @param day Negative day offset
|
||||
* @return Scrobble count
|
||||
@ -276,34 +212,19 @@ public class FmUserNetwork extends FmNetwork {
|
||||
epoch -= (day * (24 * 60 * 60));
|
||||
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();
|
||||
HashMap<String, String> parameters = new HashMap<String, String>();
|
||||
|
||||
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")
|
||||
.getJSONObject("@attr").getInt("total");
|
||||
JSONObject obj = makeGetRequest("user.getrecenttracks", parameters);
|
||||
|
||||
return total;
|
||||
int total = obj.getJSONObject("recenttracks").getJSONObject("@attr").getInt("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;
|
||||
}
|
||||
return total;
|
||||
|
||||
} catch (UnirestException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -315,107 +236,76 @@ public class FmUserNetwork extends FmNetwork {
|
||||
if (ConsoleHandler.isVerbose())
|
||||
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();
|
||||
HashMap<String, String> parameters = new HashMap<String, String>();
|
||||
|
||||
if (response.getStatus() == 200) {
|
||||
parameters.put("user", userName);
|
||||
|
||||
JSONArray tagJsonArray = new JSONObject(response.getBody().toString()).getJSONObject("toptags")
|
||||
.getJSONArray("tag");
|
||||
JSONObject obj = makeGetRequest("user.gettoptags", parameters);
|
||||
|
||||
JSONObject tagJson;
|
||||
JSONArray tagJsonArray = obj.getJSONObject("toptags").getJSONArray("tag");
|
||||
|
||||
ArrayList<Tag> tags = new ArrayList<Tag>();
|
||||
JSONObject tagJson;
|
||||
|
||||
int counter;
|
||||
for (counter = 0; counter < tagJsonArray.length(); counter++) {
|
||||
ArrayList<Tag> tags = new ArrayList<Tag>();
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns FMObjList of tagged artists
|
||||
* Returns FMObjList of tagged artists
|
||||
*
|
||||
* @param tagName Tag to explore
|
||||
* @return FMObjList of artists
|
||||
* @return FMObjList of artists
|
||||
*/
|
||||
public FMObjList getTag(String tagName) {
|
||||
if (ConsoleHandler.isVerbose())
|
||||
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();
|
||||
HashMap<String, String> parameters = new HashMap<String, String>();
|
||||
|
||||
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")
|
||||
.getJSONObject("artists").getJSONArray("artist");
|
||||
JSONObject obj = makeGetRequest("user.getpersonaltags", parameters);
|
||||
|
||||
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;
|
||||
for (counter = 0; counter < tagJsonArray.length(); counter++) {
|
||||
list.setGroupName(tagName);
|
||||
|
||||
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())
|
||||
ConsoleHandler.getConsole().write(">Tag: " + tagName + ", " + artist.getName());
|
||||
Artist artist = getArtist(artistJson.getString("name"));
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
|
@ -35,5 +35,14 @@ public class FmNetworkTest {
|
||||
Artist artist= new FmNetwork(Key.getKey()).getArtist("Kendrick Lamar");
|
||||
assertEquals(artist.getName(), "Kendrick Lamar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullParameters() {
|
||||
FmNetwork network = new FmNetwork(Key.getKey());
|
||||
|
||||
network.makeGetRequest("artist.getinfo", null);
|
||||
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user