🔨 Rebuild http request with new client

This commit is contained in:
SebClem 2022-06-23 15:43:49 +02:00
parent da976ff541
commit 4db157d1d1
Signed by: sebclem
GPG Key ID: 5A4308F6A359EA50
6 changed files with 38 additions and 33 deletions

View File

@ -17,10 +17,8 @@ import java.util.HashMap;
@ConfigurationPropertiesScan @ConfigurationPropertiesScan
public class MainBot { public class MainBot {
public static HashMap<String, Commande> commandes = new HashMap<>();
public static final HashMap<String, SlashCommand> slashCommands = new HashMap<>(); public static final HashMap<String, SlashCommand> slashCommands = new HashMap<>();
public static HashMap<String, Integer> mutualGuildCount = new HashMap<>(); public static HashMap<String, Integer> mutualGuildCount = new HashMap<>();
public static boolean roleFlag = false;
public static JDA jda; public static JDA jda;
public static boolean ready = false; public static boolean ready = false;
@ -46,7 +44,6 @@ public class MainBot {
return 1; return 1;
})); }));
} }
Init.polish(jda, config); Init.polish(jda, config);
ready = true; ready = true;
} }

View File

@ -15,8 +15,12 @@ import org.apache.logging.log4j.Logger;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -30,23 +34,25 @@ public class Cat implements SlashCommand {
@Override @Override
public void action(SlashCommandEvent event) { public void action(SlashCommandEvent event) {
try { try {
URL urlC = new URL("http://aws.random.cat/meo"); HttpRequest request = HttpRequest.newBuilder()
URLConnection yc = urlC.openConnection(); .uri(URI.create("https://aws.random.cat/meow"))
BufferedReader in = new BufferedReader(new InputStreamReader( .GET()
yc.getInputStream(), StandardCharsets.UTF_8)); .build();
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null) HttpClient client = HttpClient.newHttpClient();
a.append(inputLine); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
in.close(); if (response.statusCode() != 200) {
logger.warn("[CAT] Fail to fetch cat: Status Code: " + response.statusCode() + " Body:" + response.body());
throw new IOException();
}
TypeReference<HashMap<String, String>> typeRef = new TypeReference<>() {}; TypeReference<HashMap<String, String>> typeRef = new TypeReference<>() {};
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
HashMap<String, String> json = mapper.readValue(a.toString(), typeRef); HashMap<String, String> json = mapper.readValue(response.body(), typeRef);
event.reply(json.get("file")).queue(); event.reply(json.get("file")).queue();
} catch (IOException e) { } catch (InterruptedException | IOException e) {
logger.catching(e); logger.catching(e);
event.reply(new MessageBuilder().setEmbeds(EmbedMessageUtils.getInternalError()).build()).setEphemeral(true).queue(); event.reply(new MessageBuilder().setEmbeds(EmbedMessageUtils.getInternalError()).build()).setEphemeral(true).queue();
} }

View File

@ -23,7 +23,7 @@ public class Madame extends NumberedSlashCommand {
* @throws StringIndexOutOfBoundsException * @throws StringIndexOutOfBoundsException
* @throws IOException * @throws IOException
*/ */
private boolean scanPageForTipeee(String url, Logger logger) throws StringIndexOutOfBoundsException, IOException { private boolean scanPageForTipeee(String url, Logger logger) throws StringIndexOutOfBoundsException, IOException, InterruptedException {
String content = FindContentOnWebPage.getSourceUrl(url); String content = FindContentOnWebPage.getSourceUrl(url);
String imgClickLink = content.substring(content.indexOf("class=\"post-content")); String imgClickLink = content.substring(content.indexOf("class=\"post-content"));
imgClickLink = imgClickLink.substring(imgClickLink.indexOf("<a")); imgClickLink = imgClickLink.substring(imgClickLink.indexOf("<a"));
@ -48,7 +48,7 @@ public class Madame extends NumberedSlashCommand {
@Override @Override
public String poll() throws IOException { public String poll() throws IOException, InterruptedException {
boolean success = false; boolean success = false;
String imgUrl = null; String imgUrl = null;
while (!success) { while (!success) {

View File

@ -65,7 +65,7 @@ public abstract class NumberedSlashCommand implements SlashCommand {
try { try {
String result = poll(); String result = poll();
event.getHook().sendMessage(event.getMember().getAsMention() + "\n" + result).queue(); event.getHook().sendMessage(event.getMember().getAsMention() + "\n" + result).queue();
} catch (IOException e) { } catch (IOException | InterruptedException e) {
logger.catching(e); logger.catching(e);
MessageEmbed message = EmbedMessageUtils.getInternalError(); MessageEmbed message = EmbedMessageUtils.getInternalError();
event.getHook().setEphemeral(true).sendMessageEmbeds(message).queue(); event.getHook().setEphemeral(true).sendMessageEmbeds(message).queue();
@ -89,7 +89,7 @@ public abstract class NumberedSlashCommand implements SlashCommand {
} }
} }
public String poll() throws IOException { public String poll() throws IOException, InterruptedException {
checkRandom(); checkRandom();
int randomResult = randomQueue.poll(); int randomResult = randomQueue.poll();
return FindContentOnWebPage.doYourJob(baseURL + randomResult + urlSuffix, divClass, htmlType); return FindContentOnWebPage.doYourJob(baseURL + randomResult + urlSuffix, divClass, htmlType);

View File

@ -78,7 +78,7 @@ public class DailyMadame implements NewDayListener {
} }
} }
} }
} catch (IOException e) { } catch (IOException |InterruptedException e) {
logger.catching(e); logger.catching(e);
} }
} }

View File

@ -3,8 +3,12 @@ package net.Broken.Tools;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
public class FindContentOnWebPage { public class FindContentOnWebPage {
@ -17,7 +21,7 @@ public class FindContentOnWebPage {
* @return Picture URL * @return Picture URL
* @throws IOException * @throws IOException
*/ */
public static String doYourJob(String url, String divClass, String htmlType) throws IOException { public static String doYourJob(String url, String divClass, String htmlType) throws IOException, InterruptedException {
// System.out.println(url); // System.out.println(url);
String source = getSourceUrl(url); String source = getSourceUrl(url);
int divIndex = source.indexOf("class=\"" + divClass); int divIndex = source.indexOf("class=\"" + divClass);
@ -39,18 +43,16 @@ public class FindContentOnWebPage {
* @return Web page source as String * @return Web page source as String
* @throws IOException * @throws IOException
*/ */
public static String getSourceUrl(String url) throws IOException { public static String getSourceUrl(String url) throws IOException, InterruptedException {
URL urlC = new URL(url);
URLConnection yc = urlC.openConnection();
yc.setRequestProperty("User-Agent", "Googlebot/2.1 (+http://www.googlebot.com/bot.html)");
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream(), StandardCharsets.UTF_8));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)
a.append(inputLine);
in.close();
return a.toString(); HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("User-Agent", "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)")
.GET()
.build();
HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.body();
} }
} }