🔨 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
public class MainBot {
public static HashMap<String, Commande> commandes = new HashMap<>();
public static final HashMap<String, SlashCommand> slashCommands = new HashMap<>();
public static HashMap<String, Integer> mutualGuildCount = new HashMap<>();
public static boolean roleFlag = false;
public static JDA jda;
public static boolean ready = false;
@ -46,7 +44,6 @@ public class MainBot {
return 1;
}));
}
Init.polish(jda, config);
ready = true;
}

View File

@ -15,8 +15,12 @@ import org.apache.logging.log4j.Logger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
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.util.HashMap;
import java.util.List;
@ -30,23 +34,25 @@ public class Cat implements SlashCommand {
@Override
public void action(SlashCommandEvent event) {
try {
URL urlC = new URL("http://aws.random.cat/meo");
URLConnection yc = urlC.openConnection();
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();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://aws.random.cat/meow"))
.GET()
.build();
HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
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<>() {};
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();
} catch (IOException e) {
} catch (InterruptedException | IOException e) {
logger.catching(e);
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 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 imgClickLink = content.substring(content.indexOf("class=\"post-content"));
imgClickLink = imgClickLink.substring(imgClickLink.indexOf("<a"));
@ -48,7 +48,7 @@ public class Madame extends NumberedSlashCommand {
@Override
public String poll() throws IOException {
public String poll() throws IOException, InterruptedException {
boolean success = false;
String imgUrl = null;
while (!success) {

View File

@ -65,7 +65,7 @@ public abstract class NumberedSlashCommand implements SlashCommand {
try {
String result = poll();
event.getHook().sendMessage(event.getMember().getAsMention() + "\n" + result).queue();
} catch (IOException e) {
} catch (IOException | InterruptedException e) {
logger.catching(e);
MessageEmbed message = EmbedMessageUtils.getInternalError();
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();
int randomResult = randomQueue.poll();
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);
}
}

View File

@ -3,8 +3,12 @@ package net.Broken.Tools;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
public class FindContentOnWebPage {
@ -17,7 +21,7 @@ public class FindContentOnWebPage {
* @return Picture URL
* @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);
String source = getSourceUrl(url);
int divIndex = source.indexOf("class=\"" + divClass);
@ -39,18 +43,16 @@ public class FindContentOnWebPage {
* @return Web page source as String
* @throws IOException
*/
public static String getSourceUrl(String url) throws IOException {
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();
public static String getSourceUrl(String url) throws IOException, InterruptedException {
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();
}
}