15 Commits

Author SHA1 Message Date
5gi
e0f590b2bf fixed code from last commit 2022-11-04 15:31:37 -04:00
5gi
e25bff8f07 Update Text 2022-11-04 12:45:52 -04:00
5gi
5abf319465 Update 'src/main/java/tech/nevets/deepjTranslator.java' 2022-11-04 12:03:06 -04:00
5gi
b9fb097836 Update 'src/main/java/tech/nevets/deepj/Translator.java' 2022-11-04 11:19:09 -04:00
5gi
8c39329d6b Update 'README.md' 2022-11-04 10:53:32 -04:00
5gi
416332936a Update 'README.md' 2022-11-04 09:26:36 -04:00
5gi
efe7a737b2 Update 'README.md' 2022-11-04 08:34:06 -04:00
5gi
7958399f0c Fixed repetitive unreadable enum key.
Made code less if statementy
2022-11-03 19:01:38 -04:00
5gi
2c276acaf9 Merged Classes
Fixed repetitive auth key usage
2022-11-03 17:46:40 -04:00
5gi
50d01058aa Merged Classes
Fixed repetitive auth key usage
2022-11-03 17:31:18 -04:00
5gi
f5b9bc787b Update 'src/main/java/tech/nevets/deepj/Translator.java' 2022-11-02 14:05:34 -04:00
5gi
cf2e34e0d0 Delete 'src/main/java/tech/nevets/deepj/Languages.java' 2022-11-02 14:01:28 -04:00
5gi
8379a510c8 Update 'src/main/java/tech/nevets/deepj/Translator.java' 2022-11-02 12:55:50 -04:00
5gi
61724ad1bc Update 'src/main/java/tech/nevets/deepj/Translator.java' 2022-11-02 12:54:16 -04:00
5gi
ecae124cef Delete 'src/main/java/tech/nevets/deepj/Request.java' 2022-11-02 12:53:22 -04:00
6 changed files with 144 additions and 113 deletions

7
.idea/discord.xml generated Normal file
View 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>

View File

@@ -1,3 +1,24 @@
# DeepJ
█▀█ █▀█ █ █▀▀ █ █▄ █ ▄▀█ █
█▄█ █▀▄ █ █▄█ █ █ ▀█ █▀█ █▄▄
Java Wrapper for DeepL API
█▄▄ █▄█
█▄█‎ ‎█
█▀‎ ▀█▀‎ █▀▀‎ █‎ █‎ █▀▀‎ █▄‎ █
▄█ ██▄ ▀▄▀ ██▄ █ ▀█
________________________________________
█‎ ‎█ █▀█ █▀█ ▄▀█ ▀█▀ █▀▀ █▀█
█▄█ █▀▀‎ █▄█ █▀█‎ █‎ ‎██▄ █▄█
█▄▄ █▄█
█▄█
█▀ █▀▀ ▀█▀
▄█ █▄█ ▄█▄
________________________________________
DeepJ is a Java Wrapper for the DeepL Free Public API. This was originally made by Steven (Forked From) and Updated by 5gi

View File

@@ -0,0 +1,115 @@
package tech.nevets.DeepJ;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
public class Translator {
//TODO Test class to make sure multiple contructors work.
public static String globalAuthKey = "null";
public static void setGlobalAuthKey(String key) {
globalAuthKey = key;
}
private Request request;
public Translator() {
if (globalAuthKey == null) {
System.out.println("\n[DeepJ] You are currently using the Global Auth Key Translator Constructor." +
"\n[DeepJ] If you are trying to use global auth keys, Call the method \"Translator.setGlobalAuthKey(String key)\" and then re-use this contructor." +
"\n[DeepJ] If you want to define an different authkey every time do: \"new Translator(String authKey)\"\n");
} else {
request = new Request(globalAuthKey);
}
}
public Translator(String authKey) {
if (!Objects.equals(globalAuthKey, "null")) {
System.out.println("[DeepJ] Looks like you have defined a global auth key already! You can use if by just typing \"new Traslator()\"" +
"\n instead of \"new Translator(String authKey)\". NOTE: This will work but just some advice :)");
}
request = new Request(authKey);
}
public Translator(String authKey, boolean silence) {
if (!Objects.equals(globalAuthKey, "null") && !silence) {
System.out.println("[DeepJ] Looks like you have defined a global auth key already! You can use if by just typing \"new Traslator()\"" +
"\n instead of \"new Translator(String authKey)\". NOTE: This will work but this is just some advice :)");
}
request = new Request(authKey);
}
public String translate(Language langToTranslateTo, String sourceMessage) {
String response = "Error processing request";
try {
response = request.queryAPI(langToTranslateTo, sourceMessage);
} catch (IOException | InterruptedException e) {
System.out.println(response);
e.printStackTrace();
}
String message = "Error Processing Request (Note: This could be due to the api updating or your api being taxed to often)";
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("translations");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
message = jsonObject1.getString("text");
}
return message;
}
protected static class Request {
private final String authKey;
public Request(String authKey) {
this.authKey = authKey;
}
public String queryAPI(Language langEnum, String message) throws IOException, InterruptedException {
String encodedAuthKey = URLEncoder.encode(authKey, StandardCharsets.UTF_8);
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=" + langEnum.getApiString() + "&text=" + encodedMessage)).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.body();
}
}
public enum Language {
Bulgarian("BG"),
Czech("CS"),
Danish("DA"),
German("DE"),
Greek("EL"),
EnglishUK("EN-GB"),
EnglishUS("EN-US"),
Spanish("ES"),
Estonian("ET"),
Finish("FI"),
French("FR"),
Hungarian("HU"),
Italian("IT"),
Japanese("JA"),
Lithuanian("LT"),
Latvian("LV"),
Dutch("NL"),
Polish("PL"),
PortugueseBrazil("PT-BR"),
PortuguesePortugal("PT-PT"),
Romanian("RO"),
Russian("RU"),
Slovak("SK"),
Slovenian("SL"),
Swedish("SV"),
Chinese("ZH");
private final String apiString;
Language(String apiString) {
this.apiString = apiString;
}
public String getApiString() {
return this.apiString;
}
}
}

View File

@@ -1,30 +0,0 @@
package tech.nevets.deepj;
public 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
}

View File

@@ -1,46 +0,0 @@
package tech.nevets.deepj;
import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
public class Request {
public Request() {
}
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";
}
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());
String data = response.body();
return data;
}
}

View File

@@ -1,36 +0,0 @@
package tech.nevets.deepj;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.IOException;
public class Translator {
private final String authKey;
Request r = new Request();
public Translator(String authKey) {
this.authKey = authKey;
}
public String translate(Enum<Languages> lang, String sourceMessage) {
String response = "Error processing request";
try {
response = r.get(authKey, lang, 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;
}
}