add translate
This commit is contained in:
parent
8e306fbdeb
commit
28ed76e0ac
6 changed files with 131 additions and 5 deletions
src/main/java/land/chipmunk/chayapak/chomens_bot
|
@ -0,0 +1,91 @@
|
||||||
|
package land.chipmunk.chayapak.chomens_bot.commands;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import land.chipmunk.chayapak.chomens_bot.command.Command;
|
||||||
|
import land.chipmunk.chayapak.chomens_bot.command.CommandContext;
|
||||||
|
import land.chipmunk.chayapak.chomens_bot.util.HttpUtilities;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
|
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class TranslateCommand implements Command {
|
||||||
|
public String name() { return "translate"; }
|
||||||
|
|
||||||
|
public String description() {
|
||||||
|
return "Translate a message using Google Translate";
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> usage() {
|
||||||
|
final List<String> usages = new ArrayList<>();
|
||||||
|
usages.add("<from> <to> <{message}>");
|
||||||
|
|
||||||
|
return usages;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> alias() {
|
||||||
|
final List<String> aliases = new ArrayList<>();
|
||||||
|
aliases.add("");
|
||||||
|
|
||||||
|
return aliases;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int trustLevel() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Component execute(CommandContext context, String[] args, String[] fullArgs) {
|
||||||
|
final String from = args[0];
|
||||||
|
final String to = args[1];
|
||||||
|
|
||||||
|
final String message = String.join(" ", Arrays.copyOfRange(args, 2, args.length));
|
||||||
|
|
||||||
|
final Gson gson = new Gson();
|
||||||
|
|
||||||
|
try {
|
||||||
|
final URL url = new URL("https://translate.google.com/translate_a/single?client=at&dt=t&dt=rm&dj=1");
|
||||||
|
|
||||||
|
final String jsonOutput = HttpUtilities.postRequest(
|
||||||
|
url,
|
||||||
|
"application/x-www-form-urlencoded;charset=utf-8",
|
||||||
|
String.format(
|
||||||
|
"sl=%s&tl=%s&q=%s",
|
||||||
|
from,
|
||||||
|
to,
|
||||||
|
URLEncoder.encode(
|
||||||
|
message,
|
||||||
|
StandardCharsets.UTF_8
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
|
||||||
|
|
||||||
|
final JsonArray sentences = jsonObject.getAsJsonArray("sentences");
|
||||||
|
|
||||||
|
final JsonObject translation = sentences.get(0).getAsJsonObject();
|
||||||
|
|
||||||
|
final String output = translation.get("trans").getAsString();
|
||||||
|
|
||||||
|
context.sendOutput(
|
||||||
|
Component
|
||||||
|
.translatable(
|
||||||
|
"Result: %s",
|
||||||
|
Component.text(output).color(NamedTextColor.GREEN)
|
||||||
|
)
|
||||||
|
.color(NamedTextColor.GOLD)
|
||||||
|
);
|
||||||
|
} catch (Exception e) {
|
||||||
|
return Component.text(e.toString()).color(NamedTextColor.RED);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Component.text("success");
|
||||||
|
}
|
||||||
|
}
|
|
@ -52,7 +52,7 @@ public class UrbanCommand implements Command {
|
||||||
URLEncoder.encode(term, StandardCharsets.UTF_8)
|
URLEncoder.encode(term, StandardCharsets.UTF_8)
|
||||||
);
|
);
|
||||||
|
|
||||||
final String jsonOutput = HttpUtilities.request(url);
|
final String jsonOutput = HttpUtilities.getRequest(url);
|
||||||
|
|
||||||
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
|
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,6 @@ import org.joda.time.DateTimeZone;
|
||||||
import org.joda.time.format.DateTimeFormat;
|
import org.joda.time.format.DateTimeFormat;
|
||||||
import org.joda.time.format.DateTimeFormatter;
|
import org.joda.time.format.DateTimeFormatter;
|
||||||
|
|
||||||
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;
|
||||||
|
@ -62,7 +61,7 @@ public class WeatherCommand implements Command {
|
||||||
+ "&aqi=no"
|
+ "&aqi=no"
|
||||||
);
|
);
|
||||||
|
|
||||||
final String jsonOutput = HttpUtilities.request(url);
|
final String jsonOutput = HttpUtilities.getRequest(url);
|
||||||
|
|
||||||
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
|
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
|
||||||
|
|
||||||
|
|
|
@ -54,7 +54,7 @@ public class WikipediaCommand implements Command {
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
final String jsonOutput = HttpUtilities.request(url);
|
final String jsonOutput = HttpUtilities.getRequest(url);
|
||||||
|
|
||||||
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
|
final JsonObject jsonObject = gson.fromJson(jsonOutput, JsonObject.class);
|
||||||
|
|
||||||
|
|
|
@ -51,6 +51,7 @@ public class CommandHandlerPlugin {
|
||||||
registerCommand(new ServerInfoCommand());
|
registerCommand(new ServerInfoCommand());
|
||||||
registerCommand(new BotUserCommand());
|
registerCommand(new BotUserCommand());
|
||||||
registerCommand(new GenerateMazeCommand());
|
registerCommand(new GenerateMazeCommand());
|
||||||
|
registerCommand(new TranslateCommand());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerCommand (Command command) {
|
public void registerCommand (Command command) {
|
||||||
|
|
|
@ -6,6 +6,8 @@ import javax.net.ssl.TrustManager;
|
||||||
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.io.OutputStream;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
import java.security.KeyManagementException;
|
import java.security.KeyManagementException;
|
||||||
|
@ -13,10 +15,13 @@ import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
|
|
||||||
public class HttpUtilities {
|
public class HttpUtilities {
|
||||||
public static String request (URL url) throws NoSuchAlgorithmException, KeyManagementException, IOException {
|
// ig duplicate codes yup real
|
||||||
|
|
||||||
|
public static String getRequest (URL url) throws NoSuchAlgorithmException, KeyManagementException, IOException {
|
||||||
SSLContext ctx = SSLContext.getInstance("TLS");
|
SSLContext ctx = SSLContext.getInstance("TLS");
|
||||||
ctx.init(new KeyManager[0], new TrustManager[] {new DownloadUtilities.DefaultTrustManager()}, new SecureRandom());
|
ctx.init(new KeyManager[0], new TrustManager[] {new DownloadUtilities.DefaultTrustManager()}, new SecureRandom());
|
||||||
SSLContext.setDefault(ctx);
|
SSLContext.setDefault(ctx);
|
||||||
|
|
||||||
URLConnection conn = url.openConnection();
|
URLConnection conn = url.openConnection();
|
||||||
conn.setConnectTimeout(5000);
|
conn.setConnectTimeout(5000);
|
||||||
conn.setReadTimeout(10000);
|
conn.setReadTimeout(10000);
|
||||||
|
@ -33,4 +38,34 @@ public class HttpUtilities {
|
||||||
|
|
||||||
return content.toString();
|
return content.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String postRequest (URL url, String contentType, String requestBody) throws Exception {
|
||||||
|
SSLContext ctx = SSLContext.getInstance("TLS");
|
||||||
|
ctx.init(new KeyManager[0], new TrustManager[] {new DownloadUtilities.DefaultTrustManager()}, new SecureRandom());
|
||||||
|
SSLContext.setDefault(ctx);
|
||||||
|
|
||||||
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
|
conn.setRequestMethod("POST");
|
||||||
|
conn.setDoOutput(true);
|
||||||
|
conn.setRequestProperty("Content-Type", contentType);
|
||||||
|
|
||||||
|
conn.setConnectTimeout(5000);
|
||||||
|
conn.setReadTimeout(10000);
|
||||||
|
// https://www.whatismybrowser.com/guides/the-latest-user-agent/windows
|
||||||
|
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36");
|
||||||
|
|
||||||
|
OutputStream os = conn.getOutputStream();
|
||||||
|
os.write(requestBody.getBytes());
|
||||||
|
os.flush();
|
||||||
|
|
||||||
|
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||||
|
String inputLine;
|
||||||
|
StringBuilder content = new StringBuilder();
|
||||||
|
while ((inputLine = in.readLine()) != null) {
|
||||||
|
content.append(inputLine);
|
||||||
|
}
|
||||||
|
in.close();
|
||||||
|
|
||||||
|
return content.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue