Update & Refactor WikipediaCommand to use query.pages.${pageID}.title|extract
This commit is contained in:
parent
f537fd4667
commit
6a3e5295d8
1 changed files with 73 additions and 13 deletions
|
@ -1,22 +1,29 @@
|
||||||
package me.chayapak1.chomens_bot.commands;
|
package me.chayapak1.chomens_bot.commands;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import me.chayapak1.chomens_bot.Bot;
|
import me.chayapak1.chomens_bot.Bot;
|
||||||
import me.chayapak1.chomens_bot.command.Command;
|
import me.chayapak1.chomens_bot.command.Command;
|
||||||
import me.chayapak1.chomens_bot.command.CommandContext;
|
import me.chayapak1.chomens_bot.command.CommandContext;
|
||||||
import me.chayapak1.chomens_bot.command.CommandException;
|
import me.chayapak1.chomens_bot.command.CommandException;
|
||||||
import me.chayapak1.chomens_bot.command.TrustLevel;
|
import me.chayapak1.chomens_bot.command.TrustLevel;
|
||||||
|
import me.chayapak1.chomens_bot.util.ColorUtilities;
|
||||||
import me.chayapak1.chomens_bot.util.HttpUtilities;
|
import me.chayapak1.chomens_bot.util.HttpUtilities;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import net.kyori.adventure.text.format.NamedTextColor;
|
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.URL;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
public class WikipediaCommand extends Command {
|
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 () {
|
public WikipediaCommand () {
|
||||||
super(
|
super(
|
||||||
"wikipedia",
|
"wikipedia",
|
||||||
|
@ -37,21 +44,74 @@ public class WikipediaCommand extends Command {
|
||||||
|
|
||||||
bot.executorService.submit(() -> {
|
bot.executorService.submit(() -> {
|
||||||
try {
|
try {
|
||||||
final URL url = new URL(
|
Component component = Component.empty();
|
||||||
"https://en.wikipedia.org/api/rest_v1/page/summary/" +
|
|
||||||
URLEncoder.encode(
|
final URL pageIDUrl = new URL(String.format(pageIDStringURL, URLEncoder.encode(page, StandardCharsets.UTF_8)));
|
||||||
page.replace(" ", "_"), // mabe.
|
|
||||||
StandardCharsets.UTF_8
|
final JsonObject pageIDJsonOutput = gson.fromJson(HttpUtilities.getRequest(pageIDUrl), JsonObject.class);
|
||||||
|
|
||||||
|
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 String jsonOutput = HttpUtilities.getRequest(url);
|
final JsonObject pages = query.getAsJsonObject("pages");
|
||||||
|
|
||||||
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
|
final int pageID = Integer.parseInt(pages.entrySet().iterator().next().getKey());
|
||||||
|
|
||||||
context.sendOutput(Component.text(jsonObject.get("extract").getAsString()).color(NamedTextColor.GREEN));
|
if (pageID == -1) {
|
||||||
} catch (FileNotFoundException ignored) {
|
|
||||||
context.sendOutput(Component.text("Cannot find page: " + page).color(NamedTextColor.RED));
|
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) {
|
} catch (Exception e) {
|
||||||
context.sendOutput(Component.text(e.toString()).color(NamedTextColor.RED));
|
context.sendOutput(Component.text(e.toString()).color(NamedTextColor.RED));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue