completed abstraction of http requests
This commit is contained in:
parent
69627ffe96
commit
42d327c969
@ -52,21 +52,15 @@ 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>();
|
||||||
try {
|
|
||||||
request = Unirest.get("http://ws.audioscrobbler.com/2.0/").header("Accept", "application/json")
|
parameters.put("album", name);
|
||||||
.header("User-Agent", "fmframework").queryString("method", "album.getinfo")
|
parameters.put("artist", artist);
|
||||||
.queryString("artist", artist).queryString("album", name).queryString("api_key", key)
|
|
||||||
.queryString("format", "json");
|
|
||||||
|
|
||||||
if (userName != null)
|
if (userName != null)
|
||||||
request.queryString("username", userName);
|
parameters.put("username", userName);
|
||||||
|
|
||||||
HttpResponse<JsonNode> response = request.asJson();
|
JSONObject obj = makeGetRequest("album.getinfo", parameters);
|
||||||
|
|
||||||
if (response.getStatus() == 200) {
|
|
||||||
|
|
||||||
JSONObject obj = new JSONObject(response.getBody().toString());
|
|
||||||
|
|
||||||
String nameIn;
|
String nameIn;
|
||||||
String artistIn;
|
String artistIn;
|
||||||
@ -102,8 +96,7 @@ public class FmNetwork {
|
|||||||
builder.setListeners(albumJson.getInt("listeners"));
|
builder.setListeners(albumJson.getInt("listeners"));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
if (ConsoleHandler.isVerbose())
|
if (ConsoleHandler.isVerbose())
|
||||||
ConsoleHandler.getConsole()
|
ConsoleHandler.getConsole().write("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
|
||||||
.write("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
|
|
||||||
else
|
else
|
||||||
System.err.println("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
|
System.err.println("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
|
||||||
}
|
}
|
||||||
@ -112,8 +105,7 @@ public class FmNetwork {
|
|||||||
builder.setPlayCount(albumJson.getInt("playcount"));
|
builder.setPlayCount(albumJson.getInt("playcount"));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
if (ConsoleHandler.isVerbose())
|
if (ConsoleHandler.isVerbose())
|
||||||
ConsoleHandler.getConsole()
|
ConsoleHandler.getConsole().write("ERROR: No play count for " + nameIn + " , " + e.getMessage());
|
||||||
.write("ERROR: No play count for " + nameIn + " , " + e.getMessage());
|
|
||||||
else
|
else
|
||||||
System.err.println("ERROR: No play count for " + nameIn + " , " + e.getMessage());
|
System.err.println("ERROR: No play count for " + nameIn + " , " + e.getMessage());
|
||||||
}
|
}
|
||||||
@ -148,19 +140,6 @@ public class FmNetwork {
|
|||||||
System.err.println("ERROR: Album Name Not Found, " + e.getMessage());
|
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -175,26 +154,12 @@ public class FmNetwork {
|
|||||||
if (ConsoleHandler.isVerbose())
|
if (ConsoleHandler.isVerbose())
|
||||||
ConsoleHandler.getConsole().write(">>getArtist: " + name);
|
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);
|
parameters.put("artist", name);
|
||||||
|
|
||||||
if (userName != null)
|
if (userName != null)
|
||||||
parameters.put("username", userName);
|
parameters.put("username", userName);
|
||||||
//request.queryString("username", userName);
|
|
||||||
|
|
||||||
// HttpResponse<JsonNode> response = request.asJson();
|
|
||||||
|
|
||||||
if (true) {
|
|
||||||
// if (response.getStatus() == 200) {
|
|
||||||
|
|
||||||
// JSONObject obj = new JSONObject(response.getBody().toString());
|
|
||||||
|
|
||||||
JSONObject obj = makeGetRequest("artist.getinfo", parameters);
|
JSONObject obj = makeGetRequest("artist.getinfo", parameters);
|
||||||
|
|
||||||
@ -212,8 +177,7 @@ public class FmNetwork {
|
|||||||
builder.setMbid(artistJson.getString("mbid"));
|
builder.setMbid(artistJson.getString("mbid"));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
if (ConsoleHandler.isVerbose())
|
if (ConsoleHandler.isVerbose())
|
||||||
ConsoleHandler.getConsole()
|
ConsoleHandler.getConsole().write("ERROR: No MBID for " + artistName + " , " + e.getMessage());
|
||||||
.write("ERROR: No MBID for " + artistName + " , " + e.getMessage());
|
|
||||||
else
|
else
|
||||||
System.err.println("ERROR: No MBID for " + artistName + " , " + e.getMessage());
|
System.err.println("ERROR: No MBID for " + artistName + " , " + e.getMessage());
|
||||||
}
|
}
|
||||||
@ -222,8 +186,7 @@ public class FmNetwork {
|
|||||||
builder.setUrl(artistJson.getString("url"));
|
builder.setUrl(artistJson.getString("url"));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
if (ConsoleHandler.isVerbose())
|
if (ConsoleHandler.isVerbose())
|
||||||
ConsoleHandler.getConsole()
|
ConsoleHandler.getConsole().write("ERROR: No Url for " + artistName + " , " + e.getMessage());
|
||||||
.write("ERROR: No Url for " + artistName + " , " + e.getMessage());
|
|
||||||
else
|
else
|
||||||
System.err.println("ERROR: No Url for " + artistName + " , " + e.getMessage());
|
System.err.println("ERROR: No Url for " + artistName + " , " + e.getMessage());
|
||||||
}
|
}
|
||||||
@ -232,8 +195,7 @@ public class FmNetwork {
|
|||||||
builder.setListeners(artistJson.getJSONObject("stats").getInt("listeners"));
|
builder.setListeners(artistJson.getJSONObject("stats").getInt("listeners"));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
if (ConsoleHandler.isVerbose())
|
if (ConsoleHandler.isVerbose())
|
||||||
ConsoleHandler.getConsole()
|
ConsoleHandler.getConsole().write("ERROR: No listeners for " + artistName + " , " + e.getMessage());
|
||||||
.write("ERROR: No listeners for " + artistName + " , " + e.getMessage());
|
|
||||||
else
|
else
|
||||||
System.err.println("ERROR: No listeners for " + artistName + " , " + e.getMessage());
|
System.err.println("ERROR: No listeners for " + artistName + " , " + e.getMessage());
|
||||||
}
|
}
|
||||||
@ -264,8 +226,7 @@ public class FmNetwork {
|
|||||||
|
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
if (ConsoleHandler.isVerbose())
|
if (ConsoleHandler.isVerbose())
|
||||||
ConsoleHandler.getConsole()
|
ConsoleHandler.getConsole().write("ERROR: No wiki for " + artistName + " , " + e.getMessage());
|
||||||
.write("ERROR: No wiki for " + artistName + " , " + e.getMessage());
|
|
||||||
else
|
else
|
||||||
System.err.println("ERROR: No wiki for " + artistName + " , " + e.getMessage());
|
System.err.println("ERROR: No wiki for " + artistName + " , " + e.getMessage());
|
||||||
}
|
}
|
||||||
@ -278,18 +239,6 @@ public class FmNetwork {
|
|||||||
else
|
else
|
||||||
System.err.println("ERROR: Arist Name Not Found, " + e.getMessage());
|
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();
|
|
||||||
// }
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
@ -306,21 +255,15 @@ 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>();
|
||||||
try {
|
|
||||||
request = Unirest.get("http://ws.audioscrobbler.com/2.0/").header("Accept", "application/json")
|
parameters.put("artist", artist);
|
||||||
.header("User-Agent", "fmframework").queryString("method", "track.getinfo")
|
parameters.put("track", name);
|
||||||
.queryString("artist", artist).queryString("track", name).queryString("api_key", key)
|
|
||||||
.queryString("format", "json");
|
|
||||||
|
|
||||||
if (userName != null)
|
if (userName != null)
|
||||||
request.queryString("username", userName);
|
parameters.put("username", userName);
|
||||||
|
|
||||||
HttpResponse<JsonNode> response = request.asJson();
|
JSONObject obj = makeGetRequest("track.getinfo", parameters);
|
||||||
|
|
||||||
if (response.getStatus() == 200) {
|
|
||||||
|
|
||||||
JSONObject obj = new JSONObject(response.getBody().toString());
|
|
||||||
|
|
||||||
String nameIn;
|
String nameIn;
|
||||||
String artistIn;
|
String artistIn;
|
||||||
@ -356,8 +299,7 @@ public class FmNetwork {
|
|||||||
builder.setListeners(trackJson.getInt("listeners"));
|
builder.setListeners(trackJson.getInt("listeners"));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
if (ConsoleHandler.isVerbose())
|
if (ConsoleHandler.isVerbose())
|
||||||
ConsoleHandler.getConsole()
|
ConsoleHandler.getConsole().write("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
|
||||||
.write("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
|
|
||||||
else
|
else
|
||||||
System.err.println("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
|
System.err.println("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
|
||||||
}
|
}
|
||||||
@ -366,8 +308,7 @@ public class FmNetwork {
|
|||||||
builder.setPlayCount(trackJson.getInt("playcount"));
|
builder.setPlayCount(trackJson.getInt("playcount"));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
if (ConsoleHandler.isVerbose())
|
if (ConsoleHandler.isVerbose())
|
||||||
ConsoleHandler.getConsole()
|
ConsoleHandler.getConsole().write("ERROR: No play count for " + nameIn + " , " + e.getMessage());
|
||||||
.write("ERROR: No play count for " + nameIn + " , " + e.getMessage());
|
|
||||||
else
|
else
|
||||||
System.err.println("ERROR: No play count for " + nameIn + " , " + e.getMessage());
|
System.err.println("ERROR: No play count for " + nameIn + " , " + e.getMessage());
|
||||||
}
|
}
|
||||||
@ -402,19 +343,6 @@ public class FmNetwork {
|
|||||||
System.err.println("ERROR: Album Name Not Found, " + e.getMessage());
|
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -479,15 +407,31 @@ 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);
|
||||||
|
|
||||||
|
if (headers != null) {
|
||||||
|
for (String key : headers.keySet()) {
|
||||||
|
request = request.header(key, headers.get(key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parameters != null) {
|
||||||
for (String key : parameters.keySet()) {
|
for (String key : parameters.keySet()) {
|
||||||
request = request.queryString(key, parameters.get(key));
|
request = request.queryString(key, parameters.get(key));
|
||||||
System.out.println(key + " " + parameters.get(key));
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
request = request.queryString("api_key", key).queryString("format", "json");
|
||||||
|
|
||||||
HttpResponse<JsonNode> response = request.asJson();
|
HttpResponse<JsonNode> response = request.asJson();
|
||||||
|
|
||||||
@ -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) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
@ -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,15 +116,12 @@ 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);
|
||||||
@ -151,19 +132,6 @@ public class FmUserNetwork extends FmNetwork {
|
|||||||
|
|
||||||
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,33 +148,18 @@ 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");
|
|
||||||
|
int total = obj.getJSONObject("recenttracks").getJSONObject("@attr").getInt("total");
|
||||||
|
|
||||||
return 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();
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -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");
|
|
||||||
|
int total = obj.getJSONObject("recenttracks").getJSONObject("@attr").getInt("total");
|
||||||
|
|
||||||
return 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();
|
|
||||||
}
|
|
||||||
|
|
||||||
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");
|
|
||||||
|
int total = obj.getJSONObject("recenttracks").getJSONObject("@attr").getInt("total");
|
||||||
|
|
||||||
return 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();
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -315,16 +236,13 @@ 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");
|
|
||||||
|
JSONArray tagJsonArray = obj.getJSONObject("toptags").getJSONArray("tag");
|
||||||
|
|
||||||
JSONObject tagJson;
|
JSONObject tagJson;
|
||||||
|
|
||||||
@ -343,20 +261,6 @@ public class FmUserNetwork extends FmNetwork {
|
|||||||
|
|
||||||
return tags;
|
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;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -369,16 +273,16 @@ 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);
|
||||||
|
|
||||||
|
JSONArray tagJsonArray = obj.getJSONObject("taggings")
|
||||||
.getJSONObject("artists").getJSONArray("artist");
|
.getJSONObject("artists").getJSONArray("artist");
|
||||||
|
|
||||||
JSONObject artistJson;
|
JSONObject artistJson;
|
||||||
@ -403,20 +307,6 @@ public class FmUserNetwork extends FmNetwork {
|
|||||||
|
|
||||||
return list;
|
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;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user