Update & Refactor WikipediaCommand to use query.pages.${pageID}.title|extract

This commit is contained in:
Chayapak 2024-10-18 14:25:05 +07:00
parent f537fd4667
commit 6a3e5295d8

View file

@ -1,22 +1,29 @@
package me.chayapak1.chomens_bot.commands;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.command.Command;
import me.chayapak1.chomens_bot.command.CommandContext;
import me.chayapak1.chomens_bot.command.CommandException;
import me.chayapak1.chomens_bot.command.TrustLevel;
import me.chayapak1.chomens_bot.util.ColorUtilities;
import me.chayapak1.chomens_bot.util.HttpUtilities;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextDecoration;
import java.io.FileNotFoundException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
public class WikipediaCommand extends Command {
public static final String pageIDStringURL = "https://en.wikipedia.org/w/api.php?prop=info%%7Cpageprops&inprop=url&ppprop=disambiguation&titles=%s&format=json&redirects=&action=query&origin=*&";
public static final String outputStringURL = "https://en.wikipedia.org/w/api.php?prop=extracts&explaintext=&exintro=&pageids=%d&format=json&redirects=&action=query&origin=*&";
public WikipediaCommand () {
super(
"wikipedia",
@ -37,21 +44,74 @@ public class WikipediaCommand extends Command {
bot.executorService.submit(() -> {
try {
final URL url = new URL(
"https://en.wikipedia.org/api/rest_v1/page/summary/" +
URLEncoder.encode(
page.replace(" ", "_"), // mabe.
StandardCharsets.UTF_8
)
);
Component component = Component.empty();
final String jsonOutput = HttpUtilities.getRequest(url);
final URL pageIDUrl = new URL(String.format(pageIDStringURL, URLEncoder.encode(page, StandardCharsets.UTF_8)));
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
final JsonObject pageIDJsonOutput = gson.fromJson(HttpUtilities.getRequest(pageIDUrl), JsonObject.class);
context.sendOutput(Component.text(jsonObject.get("extract").getAsString()).color(NamedTextColor.GREEN));
} catch (FileNotFoundException ignored) {
context.sendOutput(Component.text("Cannot find page: " + page).color(NamedTextColor.RED));
final JsonObject query = pageIDJsonOutput.getAsJsonObject("query");
final JsonElement normalizedElement = query.get("normalized");
if (normalizedElement != null) {
final JsonArray normalized = normalizedElement.getAsJsonArray();
for (JsonElement element : normalized) {
final JsonObject redirect = element.getAsJsonObject();
final String redirectedTo = redirect.get("to").getAsString();
component = component.append(
Component
.translatable("Redirected to %s")
.arguments(Component.text(redirectedTo))
.style(
Style.style()
.decorate(TextDecoration.ITALIC)
.color(NamedTextColor.GRAY)
)
).append(Component.newline());
}
}
final JsonObject pages = query.getAsJsonObject("pages");
final int pageID = Integer.parseInt(pages.entrySet().iterator().next().getKey());
if (pageID == -1) {
context.sendOutput(Component.text("Cannot find page: " + page).color(NamedTextColor.RED));
return;
}
final URL outputUrl = new URL(String.format(outputStringURL, pageID));
final JsonObject outputJsonOutput = gson.fromJson(HttpUtilities.getRequest(outputUrl), JsonObject.class);
final JsonObject pageOutput = outputJsonOutput
.getAsJsonObject("query")
.getAsJsonObject("pages")
.getAsJsonObject(String.valueOf(pageID));
final String title = pageOutput.get("title").getAsString();
final String extract = pageOutput.get("extract").getAsString();
component = component
.append(
Component
.text(title)
.style(
Style.style()
.decorate(TextDecoration.BOLD)
.color(ColorUtilities.getColorByString(bot.config.colorPalette.secondary))
)
)
.append(Component.newline())
.append(Component.text(extract).color(NamedTextColor.GREEN));
context.sendOutput(component);
} catch (NumberFormatException e) {
context.sendOutput(Component.text("Failed parsing page ID").color(NamedTextColor.RED));
e.printStackTrace();
} catch (Exception e) {
context.sendOutput(Component.text(e.toString()).color(NamedTextColor.RED));
}