Changed isValidJsonObjec()t to validateJsonResponse()
This commit is contained in:
parent
2166e166e8
commit
4ccdcb95c3
1 changed files with 63 additions and 65 deletions
|
@ -107,53 +107,53 @@ public final class StockQuote extends ThreadedModule {
|
||||||
|
|
||||||
if (response.body() != null) {
|
if (response.body() != null) {
|
||||||
JSONObject json = new JSONObject(Objects.requireNonNull(response.body()).string());
|
JSONObject json = new JSONObject(Objects.requireNonNull(response.body()).string());
|
||||||
if (isValidJsonObject(json, debugMessage)) {
|
validateJsonResponse(json, debugMessage);
|
||||||
final JSONArray symbols = json.getJSONArray("bestMatches");
|
|
||||||
if (symbols.isEmpty()) {
|
final JSONArray symbols = json.getJSONArray("bestMatches");
|
||||||
|
if (symbols.isEmpty()) {
|
||||||
|
messages.add(new ErrorMessage(INVALID_SYMBOL));
|
||||||
|
return messages;
|
||||||
|
}
|
||||||
|
|
||||||
|
final JSONObject symbolInfo = symbols.getJSONObject(0);
|
||||||
|
|
||||||
|
// Get quote for symbol
|
||||||
|
request = new Request.Builder().url(
|
||||||
|
ALAPHADVANTAGE_URL + "GLOBAL_QUOTE&symbol=" + symbolInfo.getString("1. symbol") + "&apikey="
|
||||||
|
+ apiKey).build();
|
||||||
|
response = client.newCall(request).execute();
|
||||||
|
if (response.body() != null) {
|
||||||
|
json = new JSONObject(Objects.requireNonNull(response.body()).string());
|
||||||
|
validateJsonResponse(json, debugMessage);
|
||||||
|
|
||||||
|
final JSONObject quote = json.getJSONObject("Global Quote");
|
||||||
|
|
||||||
|
if (quote.isEmpty()) {
|
||||||
messages.add(new ErrorMessage(INVALID_SYMBOL));
|
messages.add(new ErrorMessage(INVALID_SYMBOL));
|
||||||
return messages;
|
return messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
final JSONObject symbolInfo = symbols.getJSONObject(0);
|
messages.add(new PublicMessage(
|
||||||
|
"Symbol: " + Utils.unescapeXml(quote.getString("01. symbol")) + " [" + Utils
|
||||||
|
.unescapeXml(symbolInfo.getString("2. name") + ']')));
|
||||||
|
messages.add(
|
||||||
|
new PublicMessage(" Price: " + Utils.unescapeXml(quote.getString("05. price"))));
|
||||||
|
messages.add(new PublicMessage(
|
||||||
|
" Previous: " + Utils.unescapeXml(quote.getString("08. previous close"))));
|
||||||
|
messages.add(
|
||||||
|
new NoticeMessage(" Open: " + Utils.unescapeXml(quote.getString("02. open"))));
|
||||||
|
messages.add(
|
||||||
|
new NoticeMessage(" High: " + Utils.unescapeXml(quote.getString("03. high"))));
|
||||||
|
messages.add(
|
||||||
|
new NoticeMessage(" Low: " + Utils.unescapeXml(quote.getString("04. low"))));
|
||||||
|
messages.add(
|
||||||
|
new NoticeMessage(" Volume: " + Utils.unescapeXml(quote.getString("06. volume"))));
|
||||||
|
messages.add(new NoticeMessage(
|
||||||
|
" Latest: " + Utils.unescapeXml(quote.getString("07. latest trading day"))));
|
||||||
|
messages.add(new NoticeMessage(
|
||||||
|
" Change: " + Utils.unescapeXml(quote.getString("09. change")) + " [" + Utils
|
||||||
|
.unescapeXml(quote.getString("10. change percent")) + ']'));
|
||||||
|
|
||||||
// Get quote for symbol
|
|
||||||
request = new Request.Builder().url(
|
|
||||||
ALAPHADVANTAGE_URL + "GLOBAL_QUOTE&symbol=" + symbolInfo.getString("1. symbol") + "&apikey="
|
|
||||||
+ apiKey).build();
|
|
||||||
response = client.newCall(request).execute();
|
|
||||||
if (response.body() != null) {
|
|
||||||
json = new JSONObject(Objects.requireNonNull(response.body()).string());
|
|
||||||
|
|
||||||
if (isValidJsonObject(json, debugMessage)) {
|
|
||||||
final JSONObject quote = json.getJSONObject("Global Quote");
|
|
||||||
|
|
||||||
if (quote.isEmpty()) {
|
|
||||||
messages.add(new ErrorMessage(INVALID_SYMBOL));
|
|
||||||
return messages;
|
|
||||||
}
|
|
||||||
|
|
||||||
messages.add(new PublicMessage(
|
|
||||||
"Symbol: " + Utils.unescapeXml(quote.getString("01. symbol")) + " [" + Utils
|
|
||||||
.unescapeXml(symbolInfo.getString("2. name") + ']')));
|
|
||||||
messages.add(new PublicMessage(
|
|
||||||
" Price: " + Utils.unescapeXml(quote.getString("05. price"))));
|
|
||||||
messages.add(new PublicMessage(
|
|
||||||
" Previous: " + Utils.unescapeXml(quote.getString("08. previous close"))));
|
|
||||||
messages.add(new NoticeMessage(
|
|
||||||
" Open: " + Utils.unescapeXml(quote.getString("02. open"))));
|
|
||||||
messages.add(new NoticeMessage(
|
|
||||||
" High: " + Utils.unescapeXml(quote.getString("03. high"))));
|
|
||||||
messages.add(new NoticeMessage(
|
|
||||||
" Low: " + Utils.unescapeXml(quote.getString("04. low"))));
|
|
||||||
messages.add(new NoticeMessage(
|
|
||||||
" Volume: " + Utils.unescapeXml(quote.getString("06. volume"))));
|
|
||||||
messages.add(new NoticeMessage(
|
|
||||||
" Latest: " + Utils.unescapeXml(quote.getString("07. latest trading day"))));
|
|
||||||
messages.add(new NoticeMessage(
|
|
||||||
" Change: " + Utils.unescapeXml(quote.getString("09. change")) + " [" + Utils
|
|
||||||
.unescapeXml(quote.getString("10. change percent")) + ']'));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -174,7 +174,27 @@ public final class StockQuote extends ThreadedModule {
|
||||||
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + STOCK_CMD + " <symbol|keywords>"));
|
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + STOCK_CMD + " <symbol|keywords>"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isValidJsonObject(final JSONObject json, final String debugMessage) throws ModuleException {
|
/**
|
||||||
|
* Returns the specified stock quote from Alpha Advantage.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
void run(final Mobibot bot, final String sender, final String symbol) {
|
||||||
|
if (Utils.isValidString(symbol)) {
|
||||||
|
try {
|
||||||
|
final List<Message> messages = getQuote(symbol, properties.get(ALPHAVANTAGE_API_KEY_PROP));
|
||||||
|
for (final Message msg : messages) {
|
||||||
|
bot.send(sender, msg);
|
||||||
|
}
|
||||||
|
} catch (ModuleException e) {
|
||||||
|
bot.getLogger().warn(e.getDebugMessage(), e);
|
||||||
|
bot.send(e.getMessage());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
helpResponse(bot, sender, symbol, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void validateJsonResponse(final JSONObject json, final String debugMessage) throws ModuleException {
|
||||||
try {
|
try {
|
||||||
final String info = json.getString("Information");
|
final String info = json.getString("Information");
|
||||||
if (!info.isEmpty()) {
|
if (!info.isEmpty()) {
|
||||||
|
@ -201,27 +221,5 @@ public final class StockQuote extends ThreadedModule {
|
||||||
} catch (JSONException ignore) {
|
} catch (JSONException ignore) {
|
||||||
// Do nothing.
|
// Do nothing.
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the specified stock quote from Alpha Advantage.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
void run(final Mobibot bot, final String sender, final String symbol) {
|
|
||||||
if (Utils.isValidString(symbol)) {
|
|
||||||
try {
|
|
||||||
final List<Message> messages = getQuote(symbol, properties.get(ALPHAVANTAGE_API_KEY_PROP));
|
|
||||||
for (final Message msg : messages) {
|
|
||||||
bot.send(sender, msg);
|
|
||||||
}
|
|
||||||
} catch (ModuleException e) {
|
|
||||||
bot.getLogger().warn(e.getDebugMessage(), e);
|
|
||||||
bot.send(e.getMessage());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
helpResponse(bot, sender, symbol, true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue