Added urlReader() method.

This commit is contained in:
Erik C. Thauvin 2020-04-29 11:52:03 -07:00
parent 6787b0dcbe
commit 1a5ae72d6e
4 changed files with 45 additions and 44 deletions

View file

@ -32,16 +32,23 @@
package net.thauvin.erik.mobibot; package net.thauvin.erik.mobibot;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.jibble.pircbot.Colors; import org.jibble.pircbot.Colors;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import java.io.BufferedReader;
import java.io.File; 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.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Date; import java.util.Date;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/** /**
* Miscellaneous utilities class. * Miscellaneous utilities class.
@ -332,6 +339,21 @@ public final class Utils {
return info.toString(); 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>. * Returns the specified date formatted as <code>yyyy-MM-dd HH:mm</code>.
* *

View file

@ -42,11 +42,8 @@ import org.jibble.pircbot.Colors;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
@ -111,25 +108,15 @@ public final class GoogleSearch extends ThreadedModule {
+ "&q=" + "&q="
+ q + q
+ "&filter=1&num=5&alt=json"); + "&filter=1&num=5&alt=json");
final URLConnection conn = url.openConnection();
final StringBuilder sb = new StringBuilder(); final JSONObject json = new JSONObject(Utils.urlReader(url));
try (final BufferedReader reader = final JSONArray ja = json.getJSONArray("items");
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()); for (int i = 0; i < ja.length(); i++) {
final JSONArray ja = json.getJSONArray("items"); final JSONObject j = ja.getJSONObject(i);
results.add(new NoticeMessage(Utils.unescapeXml(j.getString("title"))));
for (int i = 0; i < ja.length(); i++) { results.add(
final JSONObject j = ja.getJSONObject(i); new NoticeMessage(Utils.helpIndent(j.getString("link"), false), Colors.DARK_GREEN));
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) { } catch (IOException e) {
throw new ModuleException("searchGoogle(" + query + ')', "An error has occurred searching Google.", e); throw new ModuleException("searchGoogle(" + query + ')', "An error has occurred searching Google.", e);

View file

@ -38,11 +38,7 @@ import net.thauvin.erik.mobibot.msg.Message;
import net.thauvin.erik.mobibot.msg.PublicMessage; import net.thauvin.erik.mobibot.msg.PublicMessage;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
/** /**
* The Joke module. * The Joke module.
@ -79,22 +75,10 @@ public final class Joke extends ThreadedModule {
static Message randomJoke() throws ModuleException { static Message randomJoke() throws ModuleException {
try { try {
final URL url = new URL(JOKE_URL); final URL url = new URL(JOKE_URL);
final URLConnection conn = url.openConnection(); final JSONObject json = new JSONObject(Utils.urlReader(url));
return new PublicMessage(
final StringBuilder sb = new StringBuilder(); json.getJSONObject("value").get("joke").toString().replace("\\'", "'")
try (final BufferedReader reader = .replace("\\\"", "\""));
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("\\\"", "\""));
}
} catch (Exception e) { } catch (Exception e) {
throw new ModuleException("randomJoke()", "An error has occurred retrieving a random joke.", e); throw new ModuleException("randomJoke()", "An error has occurred retrieving a random joke.", e);
} }

View file

@ -38,6 +38,8 @@ import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Calendar; import java.util.Calendar;
@ -52,7 +54,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class UtilsTest { public class UtilsTest {
private static final String ASCII = private static final String ASCII =
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
private final Calendar cal = Calendar.getInstance(); private final Calendar cal = Calendar.getInstance();
private final LocalDateTime localDateTime = LocalDateTime.of(1952, 2, 17, 12, 30, 0); private final LocalDateTime localDateTime = LocalDateTime.of(1952, 2, 17, 12, 30, 0);
@ -71,7 +73,7 @@ public class UtilsTest {
@Test @Test
public void testColorize() { public void testColorize() {
assertThat(Utils.colorize(ASCII, Colors.REVERSE)).as("colorize(reverse)").isEqualTo( 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(ASCII, Colors.RED)).as("colorize(red)").isEqualTo(Colors.RED + ASCII + Colors.NORMAL);
assertThat(Utils.colorize(null, Colors.RED)).as("colorize(null)").isEqualTo(Colors.NORMAL); assertThat(Utils.colorize(null, Colors.RED)).as("colorize(null)").isEqualTo(Colors.NORMAL);
} }
@ -85,7 +87,7 @@ public class UtilsTest {
public void testEnsureDir() { public void testEnsureDir() {
assertThat(Utils.ensureDir("dir", false)).as("ensureDir(dir, false)").isEqualTo("dir" + File.separatorChar); 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( assertThat(Utils.ensureDir("https://erik.thauvin.net", true)).as("ensureDir(erik.thauvin.net, true)").isEqualTo(
"https://erik.thauvin.net/"); "https://erik.thauvin.net/");
} }
@Test @Test
@ -136,7 +138,7 @@ public class UtilsTest {
@Test @Test
public void testUnescapeXml() { public void testUnescapeXml() {
assertThat(Utils.unescapeXml("&lt;a name=&quot;test &amp; &apos;&#39;&quot;&gt;")).isEqualTo( assertThat(Utils.unescapeXml("&lt;a name=&quot;test &amp; &apos;&#39;&quot;&gt;")).isEqualTo(
"<a name=\"test & ''\">"); "<a name=\"test & ''\">");
} }
@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)); 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 @Test
public void testUtcDateTime() { public void testUtcDateTime() {
assertThat(Utils.utcDateTime(cal.getTime())).as("utcDateTime(date)").isEqualTo("1952-02-17 12:30"); assertThat(Utils.utcDateTime(cal.getTime())).as("utcDateTime(date)").isEqualTo("1952-02-17 12:30");