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,106 +10,84 @@ import java.net.http.HttpRequest;
|
|||||||
import java.net.http.HttpResponse;
|
import java.net.http.HttpResponse;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class Translator {
|
public class Translator {
|
||||||
|
Request request;
|
||||||
//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();
|
|
||||||
|
|
||||||
public Translator(String authKey) {
|
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";
|
String response = "Error processing request";
|
||||||
try {
|
try {
|
||||||
response = r.get(authKey, lang, sourceMessage);
|
response = request.get(langToTranslateTo, sourceMessage);
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException | InterruptedException e) {
|
||||||
System.out.println("Error processing request");
|
System.out.println("Error processing request");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
String message = "Error Processing Request";
|
String message = "Error Processing Request";
|
||||||
|
|
||||||
JSONObject jo = new JSONObject(response);
|
JSONObject jo = new JSONObject(response);
|
||||||
JSONArray ja = jo.getJSONArray("translations");
|
JSONArray ja = jo.getJSONArray("translations");
|
||||||
|
|
||||||
for (int i = 0; i < ja.length(); i++) {
|
for (int i = 0; i < ja.length(); i++) {
|
||||||
JSONObject joo = ja.getJSONObject(i);
|
JSONObject joo = ja.getJSONObject(i);
|
||||||
message = joo.getString("text");
|
message = joo.getString("text");
|
||||||
}
|
}
|
||||||
|
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
}
|
private static class Request {
|
||||||
|
|
||||||
private class Request {
|
private final String authKey;
|
||||||
public Request() {
|
public Request(String authKey) {
|
||||||
}
|
this.authKey = authKey;
|
||||||
|
|
||||||
public String get(String authKey, Enum<Languages> langEnum, String message) throws IOException, InterruptedException {
|
|
||||||
String encodedAuthKey = URLEncoder.encode(authKey, StandardCharsets.UTF_8);
|
|
||||||
|
|
||||||
String lang;
|
|
||||||
lang = langEnum.toString();
|
|
||||||
|
|
||||||
if (langEnum == Languages.ENUS) {
|
|
||||||
lang = "EN-US";
|
|
||||||
} else if (langEnum == Languages.ENGB) {
|
|
||||||
lang = "EN-GB";
|
|
||||||
} else if (langEnum == Languages.PTBR) {
|
|
||||||
lang = "PT-BR";
|
|
||||||
} else if (langEnum == Languages.PTPT) {
|
|
||||||
lang = "PT-PT";
|
|
||||||
}
|
}
|
||||||
|
public String get(Enum<Language> langEnum, String message) throws IOException, InterruptedException {
|
||||||
|
String encodedAuthKey = URLEncoder.encode(authKey, StandardCharsets.UTF_8);
|
||||||
|
String lang = langEnum.toString();
|
||||||
|
if (langEnum == Language.EN_US) {
|
||||||
|
lang = "EN-US";
|
||||||
|
} else if (langEnum == Language.EN_GB) {
|
||||||
|
lang = "EN-GB";
|
||||||
|
} else if (langEnum == Language.PT_BR) {
|
||||||
|
lang = "PT-BR";
|
||||||
|
} 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()
|
||||||
|
.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();
|
||||||
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
return response.body();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
String encodedMessage = URLEncoder.encode(message, StandardCharsets.UTF_8);
|
public enum Language {
|
||||||
|
BG,
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
CS,
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
DA,
|
||||||
.GET()
|
DE,
|
||||||
.header("Accept","*/*")
|
EL,
|
||||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
EN_GB,
|
||||||
.uri(URI.create("https://api-free.deepl.com/v2/translate?auth_key=" + encodedAuthKey + "&target_lang=" + lang + "&text=" + encodedMessage))
|
EN_US,
|
||||||
.build();
|
ES,
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
ET,
|
||||||
|
FI,
|
||||||
String data = response.body();
|
FR,
|
||||||
|
HU,
|
||||||
return data;
|
IT,
|
||||||
//wait for imports
|
JA,
|
||||||
|
LT,
|
||||||
|
LV,
|
||||||
|
NL,
|
||||||
|
PL,
|
||||||
|
PT_BR,
|
||||||
|
PT_PT,
|
||||||
|
RO,
|
||||||
|
RU,
|
||||||
|
SK,
|
||||||
|
SL,
|
||||||
|
SV,
|
||||||
|
ZH
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static enum Languages {
|
|
||||||
BG,
|
|
||||||
CS,
|
|
||||||
DA,
|
|
||||||
DE,
|
|
||||||
EL,
|
|
||||||
ENGB,
|
|
||||||
ENUS,
|
|
||||||
ES,
|
|
||||||
ET,
|
|
||||||
FI,
|
|
||||||
FR,
|
|
||||||
HU,
|
|
||||||
IT,
|
|
||||||
JA,
|
|
||||||
LT,
|
|
||||||
LV,
|
|
||||||
NL,
|
|
||||||
PL,
|
|
||||||
PTBR,
|
|
||||||
PTPT,
|
|
||||||
RO,
|
|
||||||
RU,
|
|
||||||
SK,
|
|
||||||
SL,
|
|
||||||
SV,
|
|
||||||
ZH
|
|
||||||
}
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user