Search in sources :

Example 1 with OpenWeatherMap

use of net.aksingh.owmjapis.OpenWeatherMap in project Shadbot by Shadorc.

the class WeatherCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException {
    if (!context.hasArg()) {
        throw new MissingArgumentException();
    }
    LoadingMessage loadingMsg = new LoadingMessage("Loading weather information...", context.getChannel());
    loadingMsg.send();
    try {
        OpenWeatherMap owm = new OpenWeatherMap(Units.METRIC, APIKeys.get(APIKey.OPENWEATHERMAP_API_KEY));
        CurrentWeather weather = owm.currentWeatherByCityName(context.getArg());
        if (!weather.isValid()) {
            loadingMsg.edit(TextUtils.noResult(context.getArg()));
            return;
        }
        String clouds = StringUtils.capitalize(weather.getWeatherInstance(0).getWeatherDescription());
        float windSpeed = weather.getWindInstance().getWindSpeed() * 3.6f;
        String windDesc = this.getWindDesc(windSpeed);
        String rain = weather.hasRainInstance() ? String.format("%.1f mm/h", weather.getRainInstance().getRain3h()) : "None";
        float humidity = weather.getMainInstance().getHumidity();
        float temperature = weather.getMainInstance().getTemperature();
        EmbedBuilder embed = EmbedUtils.getDefaultEmbed().withAuthorName("Weather for: " + weather.getCityName()).withThumbnail("https://image.flaticon.com/icons/svg/494/494472.svg").withAuthorUrl("http://openweathermap.org/city/" + weather.getCityCode()).appendDescription("Last updated " + dateFormatter.format(weather.getDateTime())).appendField(Emoji.CLOUD + " Clouds", clouds, true).appendField(Emoji.WIND + " Wind", String.format("%s%n%.1f km/h", windDesc, windSpeed), true).appendField(Emoji.RAIN + " Rain", rain, true).appendField(Emoji.DROPLET + " Humidity", String.format("%.1f%%", humidity), true).appendField(Emoji.THERMOMETER + " Temperature", String.format("%.1f°C", temperature), true);
        loadingMsg.edit(embed.build());
    } catch (IOException err) {
        loadingMsg.delete();
        Utils.handle("getting weather information", context, err);
    }
}
Also used : EmbedBuilder(sx.blah.discord.util.EmbedBuilder) CurrentWeather(net.aksingh.owmjapis.CurrentWeather) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) OpenWeatherMap(net.aksingh.owmjapis.OpenWeatherMap) LoadingMessage(me.shadorc.shadbot.utils.object.LoadingMessage) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 MissingArgumentException (me.shadorc.shadbot.exception.MissingArgumentException)1 LoadingMessage (me.shadorc.shadbot.utils.object.LoadingMessage)1 CurrentWeather (net.aksingh.owmjapis.CurrentWeather)1 OpenWeatherMap (net.aksingh.owmjapis.OpenWeatherMap)1 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)1