diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java b/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java index 4f2adac..6899ae6 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java @@ -44,6 +44,7 @@ import net.thauvin.erik.mobibot.msg.ErrorMessage; import net.thauvin.erik.mobibot.msg.Message; import net.thauvin.erik.mobibot.msg.NoticeMessage; import net.thauvin.erik.mobibot.msg.PublicMessage; +import okhttp3.HttpUrl; import org.jibble.pircbot.Colors; import java.util.ArrayList; @@ -167,10 +168,20 @@ public class Weather2 extends ThreadedModule { messages.add(new NoticeMessage(condition.toString())); } } - messages.add(new NoticeMessage("https://openweathermap.org/city/" - + cwd.getCityId(), Colors.GREEN)); - } + if (cwd.getCityId() != null) { + if (cwd.getCityId() > 0) { + messages.add(new NoticeMessage("https://openweathermap.org/city/" + cwd.getCityId(), + Colors.GREEN)); + } else { + final HttpUrl url = + HttpUrl.parse("https://openweathermap.org/find").newBuilder().addQueryParameter( + "q", city).build(); + messages.add( + new NoticeMessage(url.toString(), Colors.GREEN)); + } + } + } } catch (APIException | NullPointerException e) { throw new ModuleException("getWeather(" + query + ')', "Unable to perform weather lookup.", e); } @@ -194,7 +205,7 @@ public class Weather2 extends ThreadedModule { bot.send(sender, "For example:"); bot.send(sender, bot.helpIndent(bot.getNick() + ": " + WEATHER_CMD + " paris, fr")); bot.send(sender, "The default ISO 3166 country code is " + Utils.bold("US") - + ". Zip codes are supported in most countries."); + + ". Zip codes are supported in most countries."); } /** diff --git a/src/test/java/net/thauvin/erik/mobibot/modules/Weather2Test.java b/src/test/java/net/thauvin/erik/mobibot/modules/Weather2Test.java index 6f549dc..faf75bc 100644 --- a/src/test/java/net/thauvin/erik/mobibot/modules/Weather2Test.java +++ b/src/test/java/net/thauvin/erik/mobibot/modules/Weather2Test.java @@ -51,11 +51,13 @@ public class Weather2Test extends LocalProperties { @SuppressFBWarnings("PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS") @Test public void testWeather() throws ModuleException { - List messages = Weather2.getWeather("98204", - LocalProperties.getProperty(Weather2.OWM_API_KEY_PROP)); + List messages = Weather2.getWeather("98204", LocalProperties.getProperty(Weather2.OWM_API_KEY_PROP)); assertThat(messages.get(0).getMessage()).as("is Everett").contains("Everett"); + assertThat(messages.get(messages.size() - 1).getMessage()).as("is City Search").endsWith("98204"); + messages = Weather2.getWeather("London, UK", LocalProperties.getProperty(Weather2.OWM_API_KEY_PROP)); assertThat(messages.get(0).getMessage()).as("is UK").contains("UK"); + assertThat(messages.get(messages.size() - 1).getMessage()).as("is City Code").endsWith("4298960"); try { Weather2.getWeather("test", "");