Added Utils.encodeUrl()
This commit is contained in:
parent
8d33ddc252
commit
875269f5a6
3 changed files with 15 additions and 15 deletions
|
@ -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 (<code>/</code>) to indicate a directory.
|
||||
*
|
||||
|
|
|
@ -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<Message> 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));
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue