Code cleanup.
This commit is contained in:
parent
34028479e8
commit
56b54912b3
4 changed files with 57 additions and 73 deletions
|
@ -54,15 +54,14 @@ import java.util.ArrayList;
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
public final class StockQuote extends AbstractModule {
|
public final class StockQuote extends AbstractModule {
|
||||||
|
/**
|
||||||
|
* The Alpha Advantage property key.
|
||||||
|
*/
|
||||||
static final String ALPHAVANTAGE_API_KEY_PROP = "alphavantage-api-key";
|
static final String ALPHAVANTAGE_API_KEY_PROP = "alphavantage-api-key";
|
||||||
|
|
||||||
// The Alpha Advantage URL.
|
// The Alpha Advantage URL.
|
||||||
private static final String ALAPHADVANTAGE_URL = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE";
|
private static final String ALAPHADVANTAGE_URL = "https://www.alphavantage.co/query?function=GLOBAL_QUOTE";
|
||||||
|
// The quote command.
|
||||||
/**
|
|
||||||
* The quote command.
|
|
||||||
*/
|
|
||||||
private static final String STOCK_CMD = "stock";
|
private static final String STOCK_CMD = "stock";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -73,7 +72,6 @@ public final class StockQuote extends AbstractModule {
|
||||||
properties.put(ALPHAVANTAGE_API_KEY_PROP, "");
|
properties.put(ALPHAVANTAGE_API_KEY_PROP, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a stock quote.
|
* Get a stock quote.
|
||||||
*
|
*
|
||||||
|
@ -90,46 +88,56 @@ public final class StockQuote extends AbstractModule {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final Response response = client.newCall(request).execute();
|
final Response response = client.newCall(request).execute();
|
||||||
final JSONObject json = new JSONObject(response.body().string());
|
if (response.body() != null) {
|
||||||
|
final JSONObject json = new JSONObject(response.body().string());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final String info = json.getString("Information");
|
final String info = json.getString("Information");
|
||||||
if (!info.isEmpty()) {
|
if (!info.isEmpty()) {
|
||||||
throw new ModuleException(debugMessage, Utils.unescapeXml(info));
|
throw new ModuleException(debugMessage, Utils.unescapeXml(info));
|
||||||
|
}
|
||||||
|
} catch (JSONException ignore) {
|
||||||
|
// Do nothing.
|
||||||
}
|
}
|
||||||
} catch (JSONException ignore) {
|
|
||||||
// Do nothing.
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final String error = json.getString("Error Message");
|
final String error = json.getString("Error Message");
|
||||||
if (!error.isEmpty()) {
|
if (!error.isEmpty()) {
|
||||||
throw new ModuleException(debugMessage, Utils.unescapeXml(error));
|
throw new ModuleException(debugMessage, Utils.unescapeXml(error));
|
||||||
|
}
|
||||||
|
} catch (JSONException ignore) {
|
||||||
|
// Do nothing.
|
||||||
}
|
}
|
||||||
} catch (JSONException ignore) {
|
|
||||||
// Do nothing.
|
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"))));
|
||||||
|
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")) + ']'));
|
||||||
}
|
}
|
||||||
|
|
||||||
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"))));
|
|
||||||
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) {
|
||||||
throw new ModuleException(debugMessage, "An error has occurred retrieving a stock quote.", e);
|
throw new ModuleException(debugMessage, "An error has occurred retrieving a stock quote.", e);
|
||||||
}
|
}
|
||||||
|
@ -165,7 +173,7 @@ public final class StockQuote extends AbstractModule {
|
||||||
final ArrayList<Message> messages =
|
final ArrayList<Message> messages =
|
||||||
getQuote(symbol, properties.get(ALPHAVANTAGE_API_KEY_PROP));
|
getQuote(symbol, properties.get(ALPHAVANTAGE_API_KEY_PROP));
|
||||||
for (Message msg : messages) {
|
for (Message msg : messages) {
|
||||||
bot.send(msg.isNoticeOrError() ? sender : bot.getChannel(), msg.getMessage());
|
bot.send(msg.isNotice() ? sender : bot.getChannel(), msg.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (ModuleException e) {
|
} catch (ModuleException e) {
|
||||||
|
|
|
@ -54,15 +54,7 @@ public abstract class ThreadedModule extends AbstractModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* Runs the thread.
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean isEnabled() {
|
|
||||||
return isValidProperties();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Run the thread.
|
|
||||||
*/
|
*/
|
||||||
abstract void run(Mobibot bot, String sender, String args);
|
abstract void run(Mobibot bot, String sender, String args);
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,15 +44,11 @@ import java.security.SecureRandom;
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
public final class War extends AbstractModule {
|
public final class War extends AbstractModule {
|
||||||
/**
|
// The war command
|
||||||
* The war command
|
private static final String WAR_CMD = "war";
|
||||||
*/
|
|
||||||
public static final String WAR_CMD = "war";
|
|
||||||
|
|
||||||
// The deck of card.
|
// The deck of card.
|
||||||
private static final String[] WAR_DECK =
|
private static final String[] WAR_DECK =
|
||||||
new String[]{"Ace", "King", "Queen", "Jack", "10", "9", "8", "7", "6", "5", "4", "3", "2"};
|
new String[]{"Ace", "King", "Queen", "Jack", "10", "9", "8", "7", "6", "5", "4", "3", "2"};
|
||||||
|
|
||||||
// The suits for the deck of card.
|
// The suits for the deck of card.
|
||||||
private static final String[] WAR_SUITS = new String[]{"Hearts", "Spades", "Diamonds", "Clubs"};
|
private static final String[] WAR_SUITS = new String[]{"Hearts", "Spades", "Diamonds", "Clubs"};
|
||||||
|
|
||||||
|
|
|
@ -50,14 +50,10 @@ import java.util.List;
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
*/
|
*/
|
||||||
public class Weather2 extends AbstractModule {
|
public class Weather2 extends AbstractModule {
|
||||||
|
|
||||||
/**
|
|
||||||
* The weather command.
|
|
||||||
*/
|
|
||||||
public static final String WEATHER_CMD = "weather";
|
|
||||||
|
|
||||||
// The OpenWeatherMap API Key property.
|
// The OpenWeatherMap API Key property.
|
||||||
private static final String OWM_API_KEY_PROP = "owm-api-key";
|
private static final String OWM_API_KEY_PROP = "owm-api-key";
|
||||||
|
// The weather command.
|
||||||
|
private static final String WEATHER_CMD = "weather";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new {@link Weather2} instance.
|
* Creates a new {@link Weather2} instance.
|
||||||
|
@ -76,7 +72,7 @@ public class Weather2 extends AbstractModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
private String fAndC(final Double d) {
|
private String fAndC(final Double d) {
|
||||||
final Double c = (d - 32) * 5 / 9;
|
final double c = (d - 32) * 5 / 9;
|
||||||
return Math.round(d) + " \u00B0F, " + Math.round(c) + " \u00B0C";
|
return Math.round(d) + " \u00B0F, " + Math.round(c) + " \u00B0C";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,14 +100,6 @@ public class Weather2 extends AbstractModule {
|
||||||
+ ". Zip codes are supported in most countries.");
|
+ ". Zip codes are supported in most countries.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritDoc}
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean isEnabled() {
|
|
||||||
return isValidProperties();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches the weather data from a specific city.
|
* Fetches the weather data from a specific city.
|
||||||
*/
|
*/
|
||||||
|
@ -146,7 +134,7 @@ public class Weather2 extends AbstractModule {
|
||||||
bot.send(sender, "Temperature: " + fAndC(main.getTemp()), isPrivate);
|
bot.send(sender, "Temperature: " + fAndC(main.getTemp()), isPrivate);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (main.hasHumidity()) {
|
if (main.hasHumidity() && (main.getHumidity() != null)) {
|
||||||
bot.send(sender, "Humidity: " + Math.round(main.getHumidity()) + "%", isPrivate);
|
bot.send(sender, "Humidity: " + Math.round(main.getHumidity()) + "%", isPrivate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue