From 875269f5a60cecb4d07b345e0e5ed4b4b6f6af00 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sat, 20 Jun 2020 11:09:21 -0700 Subject: [PATCH] Added Utils.encodeUrl() --- src/main/java/net/thauvin/erik/mobibot/Utils.java | 11 +++++++++++ .../thauvin/erik/mobibot/modules/GoogleSearch.java | 6 +----- .../net/thauvin/erik/mobibot/modules/Weather2.java | 13 +++---------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main/java/net/thauvin/erik/mobibot/Utils.java b/src/main/java/net/thauvin/erik/mobibot/Utils.java index 3d0632a..69bd6a9 100644 --- a/src/main/java/net/thauvin/erik/mobibot/Utils.java +++ b/src/main/java/net/thauvin/erik/mobibot/Utils.java @@ -42,6 +42,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; +import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.time.ZoneId; @@ -116,6 +117,16 @@ public final class Utils { return colorize(s, Colors.CYAN); } + /** + * URL encodes the given string. + * + * @param s The string to encode. + * @return The encoded string. + */ + public static String encodeUrl(final String s) { + return URLEncoder.encode(s, StandardCharsets.UTF_8); + } + /** * Ensures that the given location (File/URL) has a trailing slash (/) to indicate a directory. * diff --git a/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java b/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java index a36789f..478a64e 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/GoogleSearch.java @@ -44,8 +44,6 @@ import org.json.JSONObject; import java.io.IOException; import java.net.URL; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -98,15 +96,13 @@ public final class GoogleSearch extends ThreadedModule { if (StringUtils.isNotBlank(query)) { final ArrayList results = new ArrayList<>(); try { - final String q = URLEncoder.encode(query, StandardCharsets.UTF_8.toString()); - final URL url = new URL("https://www.googleapis.com/customsearch/v1?key=" + apiKey + "&cx=" + cseKey + "&q=" - + q + + Utils.encodeUrl(query) + "&filter=1&num=5&alt=json"); final JSONObject json = new JSONObject(Utils.urlReader(url)); 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 a0894d0..9ef1426 100644 --- a/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java +++ b/src/main/java/net/thauvin/erik/mobibot/modules/Weather2.java @@ -44,13 +44,11 @@ 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.apache.commons.lang3.StringUtils; import org.jibble.pircbot.Colors; import java.util.ArrayList; import java.util.List; -import java.util.Objects; import static net.thauvin.erik.mobibot.Utils.bold; @@ -185,14 +183,9 @@ public class Weather2 extends ThreadedModule { messages.add(new NoticeMessage("https://openweathermap.org/city/" + cwd.getCityId(), Colors.GREEN)); } else { - final HttpUrl url = Objects.requireNonNull(HttpUrl.parse( - "https://openweathermap.org/find")) - .newBuilder() - .addQueryParameter("q", - city + ',' - + StringUtils.upperCase(country)) - .build(); - messages.add(new NoticeMessage(url.toString(), Colors.GREEN)); + final String url = "https://openweathermap.org/find?q=" + + Utils.encodeUrl(city + ',' + StringUtils.upperCase(country)); + messages.add(new NoticeMessage(url, Colors.GREEN)); } } }