Added OpenWeatherMap weather module.

This commit is contained in:
Erik C. Thauvin 2017-04-02 18:13:10 -07:00
parent cb24ebbfe2
commit 5e5f0d84d1
10 changed files with 220 additions and 38 deletions

View file

@ -60,18 +60,20 @@ dependencies {
compile 'oro:oro:2.0.8'
compile 'org.jsoup:jsoup:1.9.2'
compile 'com.rometools:rome:1.6.1'
compile 'org.jsoup:jsoup:1.10.1'
compile 'com.rometools:rome:1.7.0'
compile 'org.slf4j:slf4j-log4j12:1.7.21'
compile 'org.json:json:20160212'
compile 'org.json:json:20160810'
compile 'org.ostermiller:utils:1.07.00'
compile 'net.sourceforge.jweather:jweather:0.3.0@jar'
compile 'net.objecthunter:exp4j:0.4.7'
compile 'org.twitter4j:twitter4j-core:4.0.4'
compile 'org.twitter4j:twitter4j-core:4.0.5'
compile 'net.sf.delicious-java:delicious:1.14'
compile files('lib/owm-japis-2.5.0.5.jar')
compileOnly 'net.thauvin.erik:semver:0.9.6-beta'
}
@ -136,8 +138,10 @@ task deploy(dependsOn: ['build']) {
}
task release(dependsOn: ['wrapper', 'clean', 'deploy']) << {
task release(dependsOn: ['wrapper', 'clean', 'deploy']) {
doLast {
group = 'Publishing'
description = 'Releases new version.'
isRelease = true
}
}

BIN
lib/owm-japis-2.5.0.5.jar Normal file

Binary file not shown.

View file

@ -1,4 +1,4 @@
Copyright (c) 2004-2016, Erik C. Thauvin (erik@thauvin.net)
Copyright (c) 2004-2017, Erik C. Thauvin (erik@thauvin.net)
All rights reserved.
Redistribution and use in source and binary forms, with or without

View file

@ -0,0 +1,19 @@
Copyright (c) 2013-2015 Ashutosh Kumar Singh <me@aksingh.net>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View file

@ -38,26 +38,6 @@
<property name="caretWidth" class="java.lang.Integer" />
</properties>
</component>
<component name="CompilerConfiguration">
<option name="DEFAULT_COMPILER" value="Javac" />
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
<entry name="!?*.aj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
</annotationProcessing>
</component>
<component name="CopyrightManager" default="">
<copyright>
<option name="myName" value="Copyright" />
@ -81,9 +61,6 @@
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="GradleLocalSettings">
<option name="modificationStamps">
<map>
@ -175,10 +152,15 @@
<option name="SUGGEST_PRIVATE_FOR_INNERS" value="false" />
</inspection_tool>
</profile>
<option name="useProjectProfile" value="false" />
<option name="PROJECT_PROFILE" />
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</component>
<component name="KotlinCommonCompilerArguments">
<option name="languageVersion" value="1.1" />
<option name="apiVersion" value="1.1" />
</component>
<component name="MavenImportPreferences">
<option name="generalSettings">
<MavenGeneralSettings>
@ -227,7 +209,7 @@
<module fileurl="file://$PROJECT_DIR$/.idea/modules/mobibot.iml" filepath="$PROJECT_DIR$/.idea/modules/mobibot.iml" />
</modules>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" assert-keyword="true" jdk-15="true" project-jdk-name="1.8.x" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8.x" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="PropertiesComponent">

View file

@ -41,3 +41,8 @@ tell-max-size=50
#
#google-api=
#google-cse-cx=
#
# Get OpenWeatherMap API key from: https://openweathermap.org/
#
owm-api-key=

View file

@ -13,9 +13,9 @@ import java.time.*;
* Annotation Processor</a>
*/
public final class ReleaseInfo {
private final static String buildmeta = "009";
private final static String buildmeta = "010";
private final static LocalDateTime date =
LocalDateTime.ofInstant(Instant.ofEpochMilli(1468895453561L), ZoneId.systemDefault());
LocalDateTime.ofInstant(Instant.ofEpochMilli(1491181046873L), ZoneId.systemDefault());
private final static int major = 0;
private final static int minor = 7;
private final static int patch = 0;

View file

@ -1,7 +1,7 @@
/*
* Mobibot.java
*
* Copyright (c) 2004-2016, Erik C. Thauvin (erik@thauvin.net)
* Copyright (c) 2004-2017, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -254,7 +254,7 @@ public class Mobibot extends PircBot {
MODULES.add(new StockQuote());
MODULES.add(new Twitter());
MODULES.add(new War());
MODULES.add(new Weather());
MODULES.add(new Weather2());
MODULES.add(new WorldTime());
}

View file

@ -0,0 +1,172 @@
/*
* Weather2.java
*
* Copyright (c) 2004-2017, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.mobibot.modules;
import net.aksingh.owmjapis.CurrentWeather;
import net.aksingh.owmjapis.OpenWeatherMap;
import net.thauvin.erik.mobibot.Mobibot;
import net.thauvin.erik.mobibot.Utils;
import java.io.IOException;
/**
* The <code>Weather2</code> class.
*
* @author <a href="mailto:erik@thauvin.net" target="_blank">Erik C. Thauvin</a>
* @created 2017-04-02
* @since 1.0
*/
public class Weather2 extends AbstractModule {
/**
* The weather command.
*/
public static final String WEATHER_CMD = "weather";
// The OpenWeatherMap API Key property.
private static final String OWM_API_KEY_PROP = "owm-api-key";
/**
* Creates a new {@link Weather} instance.
*/
public Weather2() {
commands.add(WEATHER_CMD);
properties.put(OWM_API_KEY_PROP, "");
}
private String capitalize(final String s) {
return s.substring(0, 1).toUpperCase() + s.substring(1);
}
/**
* {@inheritDoc}
*/
@Override
public void commandResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) {
new Thread(() -> run(bot, sender, args.toUpperCase(), isPrivate)).start();
}
private String fAndC(final Float f) {
final Float c = (f - 32) * 5 / 9;
return Math.round(f) + " \u00B0F, " + Math.round(c) + " \u00B0C";
}
/**
* {@inheritDoc}
*/
@Override
public void helpResponse(final Mobibot bot, final String sender, final String args, final boolean isPrivate) {
bot.send(sender, "To display weather information:");
bot.send(sender, bot.helpIndent(bot.getNick() + ": " + WEATHER_CMD + " <city> [, <country code>]"));
}
/**
* {@inheritDoc}
*/
@Override
public boolean isEnabled() {
return isValidProperties();
}
/**
* Fetches the weather data from a specific country.
*/
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));
if (Utils.isValidString(args)) {
final String[] argv = args.split(",");
if (argv.length >= 1 && argv.length <= 2) {
final String country;
final String city = argv[0].trim();
if (argv.length > 1 && Utils.isValidString(argv[1])) {
country = argv[1].trim();
} else {
country = "US";
}
try {
final CurrentWeather cwd = owm.currentWeatherByCityName(city, country);
if (cwd.hasCityName()) {
bot.send(sender, "City: " + cwd.getCityName() + " [" + country + "]", isPrivate);
final CurrentWeather.Main main = cwd.getMainInstance();
if (main.hasTemperature()) {
bot.send(sender, "Temperature: " + fAndC(main.getTemperature()), isPrivate);
}
if (main.hasHumidity()) {
bot.send(sender, "Humidity: " + Math.round(main.getHumidity()) + "%", isPrivate);
}
if (cwd.hasWindInstance()) {
final CurrentWeather.Wind w = cwd.getWindInstance();
if (w.hasWindSpeed()) {
bot.send(sender, "Wind: " + wind(w.getWindSpeed()), isPrivate);
}
}
if (cwd.hasWeatherInstance()) {
CurrentWeather.Weather w;
final StringBuilder condition = new StringBuilder("Condition: ");
for (int i = 0; i < cwd.getWeatherCount(); i++) {
w = cwd.getWeatherInstance(i);
if (i != 0) {
condition.append(", ").append(w.getWeatherDescription());
} else {
condition.append(capitalize(w.getWeatherDescription()));
}
}
bot.send(sender, condition.toString(), isPrivate);
}
return;
}
} catch (IOException e) {
if (bot.getLogger().isDebugEnabled()) {
bot.getLogger().debug("Unable to perform weather lookup: " + args, e);
}
bot.send(bot.getChannel(), "Unable to perform weather lookup: " + e.getMessage());
}
}
}
helpResponse(bot, sender, args, isPrivate);
}
private String wind(final Float w) {
final double kmh = w * 1.60934;
return Math.round(kmh) + " km/h, " + Math.round(w) + " mph";
}
}

View file

@ -5,4 +5,4 @@ version.major=0
version.minor=7
version.patch=0
version.prerelease=beta
version.buildmeta=009
version.buildmeta=010