Added urlReader() method.
This commit is contained in:
parent
6787b0dcbe
commit
1a5ae72d6e
4 changed files with 45 additions and 44 deletions
|
@ -32,16 +32,23 @@
|
|||
|
||||
package net.thauvin.erik.mobibot;
|
||||
|
||||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.jibble.pircbot.Colors;
|
||||
import org.jsoup.Jsoup;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Miscellaneous utilities class.
|
||||
|
@ -332,6 +339,21 @@ public final class Utils {
|
|||
return info.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads contents of a URL.
|
||||
*
|
||||
* @param url The URL to read.
|
||||
* @return The URL contents.
|
||||
* @throws IOException If an IO error occurs.
|
||||
*/
|
||||
@SuppressFBWarnings("SECSSSRFUC")
|
||||
public static String urlReader(final URL url) throws IOException {
|
||||
try (final BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(url.openStream(), StandardCharsets.UTF_8))) {
|
||||
return reader.lines().collect(Collectors.joining(System.lineSeparator()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the specified date formatted as <code>yyyy-MM-dd HH:mm</code>.
|
||||
*
|
||||
|
|
|
@ -42,11 +42,8 @@ import org.jibble.pircbot.Colors;
|
|||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
|
@ -111,25 +108,15 @@ public final class GoogleSearch extends ThreadedModule {
|
|||
+ "&q="
|
||||
+ q
|
||||
+ "&filter=1&num=5&alt=json");
|
||||
final URLConnection conn = url.openConnection();
|
||||
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
try (final BufferedReader reader =
|
||||
new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
sb.append(line);
|
||||
}
|
||||
final JSONObject json = new JSONObject(Utils.urlReader(url));
|
||||
final JSONArray ja = json.getJSONArray("items");
|
||||
|
||||
final JSONObject json = new JSONObject(sb.toString());
|
||||
final JSONArray ja = json.getJSONArray("items");
|
||||
|
||||
for (int i = 0; i < ja.length(); i++) {
|
||||
final JSONObject j = ja.getJSONObject(i);
|
||||
results.add(new NoticeMessage(Utils.unescapeXml(j.getString("title"))));
|
||||
results.add(
|
||||
new NoticeMessage(Utils.helpIndent(j.getString("link"), false), Colors.DARK_GREEN));
|
||||
}
|
||||
for (int i = 0; i < ja.length(); i++) {
|
||||
final JSONObject j = ja.getJSONObject(i);
|
||||
results.add(new NoticeMessage(Utils.unescapeXml(j.getString("title"))));
|
||||
results.add(
|
||||
new NoticeMessage(Utils.helpIndent(j.getString("link"), false), Colors.DARK_GREEN));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new ModuleException("searchGoogle(" + query + ')', "An error has occurred searching Google.", e);
|
||||
|
|
|
@ -38,11 +38,7 @@ import net.thauvin.erik.mobibot.msg.Message;
|
|||
import net.thauvin.erik.mobibot.msg.PublicMessage;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* The Joke module.
|
||||
|
@ -79,22 +75,10 @@ public final class Joke extends ThreadedModule {
|
|||
static Message randomJoke() throws ModuleException {
|
||||
try {
|
||||
final URL url = new URL(JOKE_URL);
|
||||
final URLConnection conn = url.openConnection();
|
||||
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
try (final BufferedReader reader =
|
||||
new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
sb.append(line);
|
||||
}
|
||||
|
||||
final JSONObject json = new JSONObject(sb.toString());
|
||||
|
||||
return new PublicMessage(
|
||||
json.getJSONObject("value").get("joke").toString().replace("\\'", "'")
|
||||
.replace("\\\"", "\""));
|
||||
}
|
||||
final JSONObject json = new JSONObject(Utils.urlReader(url));
|
||||
return new PublicMessage(
|
||||
json.getJSONObject("value").get("joke").toString().replace("\\'", "'")
|
||||
.replace("\\\"", "\""));
|
||||
} catch (Exception e) {
|
||||
throw new ModuleException("randomJoke()", "An error has occurred retrieving a random joke.", e);
|
||||
}
|
||||
|
|
|
@ -38,6 +38,8 @@ import org.testng.annotations.BeforeClass;
|
|||
import org.testng.annotations.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Calendar;
|
||||
|
||||
|
@ -52,7 +54,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
*/
|
||||
public class UtilsTest {
|
||||
private static final String ASCII =
|
||||
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
|
||||
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
|
||||
|
||||
private final Calendar cal = Calendar.getInstance();
|
||||
private final LocalDateTime localDateTime = LocalDateTime.of(1952, 2, 17, 12, 30, 0);
|
||||
|
@ -71,7 +73,7 @@ public class UtilsTest {
|
|||
@Test
|
||||
public void testColorize() {
|
||||
assertThat(Utils.colorize(ASCII, Colors.REVERSE)).as("colorize(reverse)").isEqualTo(
|
||||
Colors.REVERSE + ASCII + Colors.REVERSE);
|
||||
Colors.REVERSE + ASCII + Colors.REVERSE);
|
||||
assertThat(Utils.colorize(ASCII, Colors.RED)).as("colorize(red)").isEqualTo(Colors.RED + ASCII + Colors.NORMAL);
|
||||
assertThat(Utils.colorize(null, Colors.RED)).as("colorize(null)").isEqualTo(Colors.NORMAL);
|
||||
}
|
||||
|
@ -85,7 +87,7 @@ public class UtilsTest {
|
|||
public void testEnsureDir() {
|
||||
assertThat(Utils.ensureDir("dir", false)).as("ensureDir(dir, false)").isEqualTo("dir" + File.separatorChar);
|
||||
assertThat(Utils.ensureDir("https://erik.thauvin.net", true)).as("ensureDir(erik.thauvin.net, true)").isEqualTo(
|
||||
"https://erik.thauvin.net/");
|
||||
"https://erik.thauvin.net/");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -136,7 +138,7 @@ public class UtilsTest {
|
|||
@Test
|
||||
public void testUnescapeXml() {
|
||||
assertThat(Utils.unescapeXml("<a name="test & ''">")).isEqualTo(
|
||||
"<a name=\"test & ''\">");
|
||||
"<a name=\"test & ''\">");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -144,6 +146,12 @@ public class UtilsTest {
|
|||
assertThat("17 years 2 months 2 weeks 1 day 6 hours 45 minutes").isEqualTo(Utils.uptime(547800300076L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUrlReader() throws IOException {
|
||||
assertThat(Utils.urlReader(new URL("https://postman-echo.com/status/200"))).as("urlReader()").isEqualTo(
|
||||
"{\"status\":200}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUtcDateTime() {
|
||||
assertThat(Utils.utcDateTime(cal.getTime())).as("utcDateTime(date)").isEqualTo("1952-02-17 12:30");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue