forked from Steven/DeepJ
Merged Classes
Fixed repetitive auth key usage
This commit is contained in:
parent
f5b9bc787b
commit
50d01058aa
7
.idea/discord.xml
Normal file
7
.idea/discord.xml
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DiscordProjectSettings">
|
||||
<option name="show" value="ASK" />
|
||||
<option name="description" value="" />
|
||||
</component>
|
||||
</project>
|
@ -10,88 +10,65 @@ import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class Translator {
|
||||
|
||||
//TODO Once I get home I gotta see if this works
|
||||
//NOTE: I'm stupid and did this during ELA
|
||||
|
||||
private final String authKey;
|
||||
Request r = new Request();
|
||||
|
||||
Request request;
|
||||
public Translator(String authKey) {
|
||||
this.authKey = authKey;
|
||||
request = new Request(authKey);
|
||||
}
|
||||
public String translate(Enum<Languages> lang, String sourceMessage) {
|
||||
public String translate(Enum<Language> langToTranslateTo, String sourceMessage) {
|
||||
String response = "Error processing request";
|
||||
try {
|
||||
response = r.get(authKey, lang, sourceMessage);
|
||||
response = request.get(langToTranslateTo, sourceMessage);
|
||||
} catch (IOException | InterruptedException e) {
|
||||
System.out.println("Error processing request");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
String message = "Error Processing Request";
|
||||
|
||||
JSONObject jo = new JSONObject(response);
|
||||
JSONArray ja = jo.getJSONArray("translations");
|
||||
|
||||
for (int i = 0; i < ja.length(); i++) {
|
||||
JSONObject joo = ja.getJSONObject(i);
|
||||
message = joo.getString("text");
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
}
|
||||
private static class Request {
|
||||
|
||||
private class Request {
|
||||
public Request() {
|
||||
private final String authKey;
|
||||
public Request(String authKey) {
|
||||
this.authKey = authKey;
|
||||
}
|
||||
|
||||
public String get(String authKey, Enum<Languages> langEnum, String message) throws IOException, InterruptedException {
|
||||
public String get(Enum<Language> langEnum, String message) throws IOException, InterruptedException {
|
||||
String encodedAuthKey = URLEncoder.encode(authKey, StandardCharsets.UTF_8);
|
||||
|
||||
String lang;
|
||||
lang = langEnum.toString();
|
||||
|
||||
if (langEnum == Languages.ENUS) {
|
||||
String lang = langEnum.toString();
|
||||
if (langEnum == Language.EN_US) {
|
||||
lang = "EN-US";
|
||||
} else if (langEnum == Languages.ENGB) {
|
||||
} else if (langEnum == Language.EN_GB) {
|
||||
lang = "EN-GB";
|
||||
} else if (langEnum == Languages.PTBR) {
|
||||
} else if (langEnum == Language.PT_BR) {
|
||||
lang = "PT-BR";
|
||||
} else if (langEnum == Languages.PTPT) {
|
||||
} else if (langEnum == Language.PT_PT) {
|
||||
lang = "PT-PT";
|
||||
}
|
||||
|
||||
String encodedMessage = URLEncoder.encode(message, StandardCharsets.UTF_8);
|
||||
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.GET()
|
||||
HttpRequest request = HttpRequest.newBuilder().GET()
|
||||
.header("Accept","*/*")
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
.uri(URI.create("https://api-free.deepl.com/v2/translate?auth_key=" + encodedAuthKey + "&target_lang=" + lang + "&text=" + encodedMessage))
|
||||
.build();
|
||||
.uri(URI.create("https://api-free.deepl.com/v2/translate?auth_key=" + encodedAuthKey + "&target_lang=" + lang + "&text=" + encodedMessage)).build();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
String data = response.body();
|
||||
|
||||
return data;
|
||||
//wait for imports
|
||||
return response.body();
|
||||
}
|
||||
}
|
||||
|
||||
public static enum Languages {
|
||||
public enum Language {
|
||||
BG,
|
||||
CS,
|
||||
DA,
|
||||
DE,
|
||||
EL,
|
||||
ENGB,
|
||||
ENUS,
|
||||
EN_GB,
|
||||
EN_US,
|
||||
ES,
|
||||
ET,
|
||||
FI,
|
||||
@ -103,8 +80,8 @@ public static enum Languages {
|
||||
LV,
|
||||
NL,
|
||||
PL,
|
||||
PTBR,
|
||||
PTPT,
|
||||
PT_BR,
|
||||
PT_PT,
|
||||
RO,
|
||||
RU,
|
||||
SK,
|
||||
@ -112,4 +89,5 @@ public static enum Languages {
|
||||
SV,
|
||||
ZH
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user