Implemented new version of OWM.
This commit is contained in:
parent
290bf5cf93
commit
9c8a2a61dd
1 changed files with 56 additions and 32 deletions
|
@ -31,12 +31,16 @@
|
||||||
*/
|
*/
|
||||||
package net.thauvin.erik.mobibot.modules;
|
package net.thauvin.erik.mobibot.modules;
|
||||||
|
|
||||||
import net.aksingh.owmjapis.CurrentWeather;
|
import net.aksingh.owmjapis.api.APIException;
|
||||||
import net.aksingh.owmjapis.OpenWeatherMap;
|
import net.aksingh.owmjapis.core.OWM;
|
||||||
|
import net.aksingh.owmjapis.model.CurrentWeather;
|
||||||
|
import net.aksingh.owmjapis.model.param.Main;
|
||||||
|
import net.aksingh.owmjapis.model.param.Weather;
|
||||||
|
import net.aksingh.owmjapis.model.param.Wind;
|
||||||
import net.thauvin.erik.mobibot.Mobibot;
|
import net.thauvin.erik.mobibot.Mobibot;
|
||||||
import net.thauvin.erik.mobibot.Utils;
|
import net.thauvin.erik.mobibot.Utils;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <code>Weather2</code> module.
|
* The <code>Weather2</code> module.
|
||||||
|
@ -71,9 +75,9 @@ public class Weather2 extends AbstractModule {
|
||||||
new Thread(() -> run(bot, sender, args.toUpperCase(), isPrivate)).start();
|
new Thread(() -> run(bot, sender, args.toUpperCase(), isPrivate)).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String fAndC(final Float f) {
|
private String fAndC(final Double d) {
|
||||||
final Float c = (f - 32) * 5 / 9;
|
final Double c = (d - 32) * 5 / 9;
|
||||||
return Math.round(f) + " \u00B0F, " + Math.round(c) + " \u00B0C";
|
return Math.round(d) + " \u00B0F, " + Math.round(c) + " \u00B0C";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -101,7 +105,8 @@ public class Weather2 extends AbstractModule {
|
||||||
* Fetches the weather data from a specific city.
|
* Fetches the weather data from a specific city.
|
||||||
*/
|
*/
|
||||||
private void run(final Mobibot bot, final String sender, final String args, final boolean isPrivate) {
|
private void run(final Mobibot bot, final String sender, final String args, final boolean isPrivate) {
|
||||||
final OpenWeatherMap owm = new OpenWeatherMap(properties.get(OWM_API_KEY_PROP));
|
final OWM owm = new OWM(properties.get(OWM_API_KEY_PROP));
|
||||||
|
owm.setUnit(OWM.Unit.IMPERIAL);
|
||||||
if (Utils.isValidString(args)) {
|
if (Utils.isValidString(args)) {
|
||||||
final String[] argv = args.split(",");
|
final String[] argv = args.split(",");
|
||||||
|
|
||||||
|
@ -115,46 +120,54 @@ public class Weather2 extends AbstractModule {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final CurrentWeather cwd = owm.currentWeatherByCityName(city, country);
|
final CurrentWeather cwd;
|
||||||
|
if (city.matches("\\d+")) {
|
||||||
|
cwd = owm.currentWeatherByZipCode(Integer.parseInt(city), OWM.Country.UNITED_STATES);
|
||||||
|
} else {
|
||||||
|
cwd = owm.currentWeatherByCityName(city, getCountry(country));
|
||||||
|
}
|
||||||
if (cwd.hasCityName()) {
|
if (cwd.hasCityName()) {
|
||||||
bot.send(sender, "City: " + cwd.getCityName() + " [" + country + "]", isPrivate);
|
bot.send(sender, "City: " + cwd.getCityName() + " [" + country + "]", isPrivate);
|
||||||
|
|
||||||
final CurrentWeather.Main main = cwd.getMainInstance();
|
final Main main = cwd.getMainData();
|
||||||
if (main.hasTemperature()) {
|
if (main != null) {
|
||||||
bot.send(sender, "Temperature: " + fAndC(main.getTemperature()), isPrivate);
|
if (main.hasTemp()) {
|
||||||
|
bot.send(sender, "Temperature: " + fAndC(main.getTemp()), isPrivate);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (main.hasHumidity()) {
|
if (main.hasHumidity()) {
|
||||||
bot.send(sender, "Humidity: " + Math.round(main.getHumidity()) + "%", isPrivate);
|
bot.send(sender, "Humidity: " + Math.round(main.getHumidity()) + "%", isPrivate);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (cwd.hasWindInstance()) {
|
if (cwd.hasWindData()) {
|
||||||
final CurrentWeather.Wind w = cwd.getWindInstance();
|
final Wind w = cwd.getWindData();
|
||||||
if (w.hasWindSpeed()) {
|
if (w != null && w.hasSpeed()) {
|
||||||
bot.send(sender, "Wind: " + wind(w.getWindSpeed()), isPrivate);
|
bot.send(sender, "Wind: " + wind(w.getSpeed()), isPrivate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cwd.hasWeatherInstance()) {
|
if (cwd.hasWeatherList()) {
|
||||||
CurrentWeather.Weather w;
|
|
||||||
final StringBuilder condition = new StringBuilder("Condition: ");
|
final StringBuilder condition = new StringBuilder("Condition: ");
|
||||||
for (int i = 0; i < cwd.getWeatherCount(); i++) {
|
final List<Weather> list = cwd.getWeatherList();
|
||||||
w = cwd.getWeatherInstance(i);
|
if (list != null) {
|
||||||
if (i != 0) {
|
for (Weather w : list) {
|
||||||
condition.append(", ").append(w.getWeatherDescription());
|
if (condition.indexOf(",") == -1) {
|
||||||
|
condition.append(Utils.capitalize(w.getDescription()));
|
||||||
} else {
|
} else {
|
||||||
condition.append(Utils.capitalize(w.getWeatherDescription()));
|
condition.append(", ").append(w.getDescription());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bot.send(sender, condition.toString(), isPrivate);
|
bot.send(sender, condition.toString(), isPrivate);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bot.send(sender, Utils.green("https://openweathermap.org/city/" + cwd.getCityCode()), isPrivate);
|
bot.send(sender, Utils.green("https://openweathermap.org/city/" + cwd.getCityId()), isPrivate);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (APIException | NullPointerException e) {
|
||||||
if (bot.getLogger().isDebugEnabled()) {
|
if (bot.getLogger().isDebugEnabled()) {
|
||||||
bot.getLogger().debug("Unable to perform weather lookup: " + args, e);
|
bot.getLogger().debug("Unable to perform weather lookup: " + args, e);
|
||||||
}
|
}
|
||||||
|
@ -167,8 +180,19 @@ public class Weather2 extends AbstractModule {
|
||||||
helpResponse(bot, sender, args, isPrivate);
|
helpResponse(bot, sender, args, isPrivate);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String wind(final Float w) {
|
private String wind(final Double w) {
|
||||||
final double kmh = w * 1.60934;
|
final double kmh = w * 1.60934;
|
||||||
return Math.round(w) + " mph, " + Math.round(kmh) + " km/h";
|
return Math.round(w) + " mph, " + Math.round(kmh) + " km/h";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private OWM.Country getCountry(String countryCode) {
|
||||||
|
for (OWM.Country c : OWM.Country.values()) {
|
||||||
|
if (c.name().equalsIgnoreCase(countryCode)) {
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return OWM.Country.UNITED_STATES;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue