Added Utils.encodeUrl()

This commit is contained in:
Erik C. Thauvin 2020-06-20 11:09:21 -07:00
parent 8d33ddc252
commit 875269f5a6
3 changed files with 15 additions and 15 deletions

View file

@ -42,6 +42,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
@ -116,6 +117,16 @@ public final class Utils {
return colorize(s, Colors.CYAN); 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 (<code>/</code>) to indicate a directory. * Ensures that the given location (File/URL) has a trailing slash (<code>/</code>) to indicate a directory.
* *

View file

@ -44,8 +44,6 @@ import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -98,15 +96,13 @@ public final class GoogleSearch extends ThreadedModule {
if (StringUtils.isNotBlank(query)) { if (StringUtils.isNotBlank(query)) {
final ArrayList<Message> results = new ArrayList<>(); final ArrayList<Message> results = new ArrayList<>();
try { try {
final String q = URLEncoder.encode(query, StandardCharsets.UTF_8.toString());
final URL url = final URL url =
new URL("https://www.googleapis.com/customsearch/v1?key=" new URL("https://www.googleapis.com/customsearch/v1?key="
+ apiKey + apiKey
+ "&cx=" + "&cx="
+ cseKey + cseKey
+ "&q=" + "&q="
+ q + Utils.encodeUrl(query)
+ "&filter=1&num=5&alt=json"); + "&filter=1&num=5&alt=json");
final JSONObject json = new JSONObject(Utils.urlReader(url)); final JSONObject json = new JSONObject(Utils.urlReader(url));

View file

@ -44,13 +44,11 @@ import net.thauvin.erik.mobibot.msg.ErrorMessage;
import net.thauvin.erik.mobibot.msg.Message; import net.thauvin.erik.mobibot.msg.Message;
import net.thauvin.erik.mobibot.msg.NoticeMessage; import net.thauvin.erik.mobibot.msg.NoticeMessage;
import net.thauvin.erik.mobibot.msg.PublicMessage; import net.thauvin.erik.mobibot.msg.PublicMessage;
import okhttp3.HttpUrl;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.jibble.pircbot.Colors; import org.jibble.pircbot.Colors;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import static net.thauvin.erik.mobibot.Utils.bold; 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(), messages.add(new NoticeMessage("https://openweathermap.org/city/" + cwd.getCityId(),
Colors.GREEN)); Colors.GREEN));
} else { } else {
final HttpUrl url = Objects.requireNonNull(HttpUrl.parse( final String url = "https://openweathermap.org/find?q=" +
"https://openweathermap.org/find")) Utils.encodeUrl(city + ',' + StringUtils.upperCase(country));
.newBuilder() messages.add(new NoticeMessage(url, Colors.GREEN));
.addQueryParameter("q",
city + ','
+ StringUtils.upperCase(country))
.build();
messages.add(new NoticeMessage(url.toString(), Colors.GREEN));
} }
} }
} }