forked from Steven/DeepJ
Compare commits
59 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
7af2b6e7cb | ||
|
07c30ccf8e | ||
|
3af5d6da63 | ||
|
c135da4115 | ||
|
af64960abf | ||
|
f058a27be7 | ||
|
c0f3cb5e95 | ||
|
9781caea90 | ||
|
524a19dada | ||
|
4cceed428e | ||
|
dbf2af3c64 | ||
|
6f754483a5 | ||
|
fcb20efbd8 | ||
|
c1438bff2a | ||
|
b12e497afa | ||
|
d8606a0587 | ||
|
0aa351bebd | ||
|
0f1fab61f8 | ||
|
deae28702d | ||
|
92bc6ae4c7 | ||
|
bb1aaf7cee | ||
|
e1017d8311 | ||
aab9802e56 | |||
15368ab222 | |||
fab65fde3a | |||
b3b37d0ac5 | |||
ac4ce2192f | |||
3345009f64 | |||
b72a411859 | |||
8a599d51f7 | |||
6e31919236 | |||
cf6d1e7128 | |||
dba816a33e | |||
a31d12bbb5 | |||
29442d1f9a | |||
117b59d5e7 | |||
08b7665518 | |||
7d5efc7b00 | |||
5d78663c87 | |||
f47c69715c | |||
732fc3882d | |||
0e35a2f4c8 | |||
c00f4b1a2d | |||
|
4c84fd5a98 | ||
cc5a1b9df6 | |||
787664bb38 | |||
|
d1419d48e0 | ||
|
27cd9ec212 | ||
|
b96202a905 | ||
|
96c950ccfe | ||
|
9677f95d60 | ||
|
78c8146fed | ||
|
13155c9112 | ||
|
ff8b5ea1aa | ||
|
dd495dc277 | ||
|
31268450e7 | ||
04bfc0fe22 | |||
|
0b634901ff | ||
|
1b2f247a62 |
@ -16,5 +16,10 @@
|
||||
<option name="name" value="MavenRepo" />
|
||||
<option name="url" value="https://repo.maven.apache.org/maven2/" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="maven" />
|
||||
<option name="name" value="maven" />
|
||||
<option name="url" value="https://git.nevets.tech/api/packages/5gi/maven" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
@ -4,7 +4,7 @@
|
||||
<component name="FrameworkDetectionExcludesConfiguration">
|
||||
<file type="web" url="file://$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="temurin-11" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="corretto-17" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
60
README.md
60
README.md
@ -1,24 +1,58 @@
|
||||
# DeepJ
|
||||
█▀█ █▀█ █ █▀▀ █ █▄ █ ▄▀█ █
|
||||
█▄█ █▀▄ █ █▄█ █ █ ▀█ █▀█ █▄▄
|
||||
[](https://ci.nevets.tech/view/Alec/job/DeepJ%20Improved/)
|
||||
[](https://ci.nevets.tech/view/Alec/job/DeepJ%20Improved/)
|
||||
> Quick Setup
|
||||
<details><summary>Gradle</summary>
|
||||
<p>
|
||||
|
||||
█▄▄ █▄█
|
||||
█▄█ █
|
||||
Go to your build.gradle file and type:
|
||||
|
||||
█▀ ▀█▀ █▀▀ █ █ █▀▀ █▄ █
|
||||
▄█ █ ██▄ ▀▄▀ ██▄ █ ▀█
|
||||
```
|
||||
repositories {
|
||||
maven {
|
||||
url "https://git.nevets.tech/api/packages/5gi/maven"
|
||||
}
|
||||
}
|
||||
```
|
||||
and
|
||||
|
||||
________________________________________
|
||||
```
|
||||
dependencies {
|
||||
implementation group: 'com.the5gi.deepj', name: 'DeepJ', version: 'VERSION'
|
||||
}
|
||||
```
|
||||
</p>
|
||||
</details>
|
||||
|
||||
█ █ █▀█ █▀█ ▄▀█ ▀█▀ █▀▀ █▀█
|
||||
█▄█ █▀▀ █▄█ █▀█ █ ██▄ █▄█
|
||||
<details><summary>Maven</summary>
|
||||
<p>
|
||||
|
||||
█▄▄ █▄█
|
||||
█▄█ █
|
||||
Go to your pom.xml file and type:
|
||||
|
||||
█▀ █▀▀ ▀█▀
|
||||
▄█ █▄█ ▄█▄
|
||||
```
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>gitea</id>
|
||||
<url>https://git.nevets.tech/api/packages/5gi/maven</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
```
|
||||
and
|
||||
```
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.the5gi.deepj</groupId>
|
||||
<artifactId>DeepJ</artifactId>
|
||||
<version>1.2.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
```
|
||||
</p>
|
||||
</details>
|
||||
|
||||
> Info
|
||||
|
||||
[To see more details, see the wiki](https://git.nevets.tech/5gi/DeepJ/wiki/Home#user-content-usage-setup).
|
||||
________________________________________
|
||||
|
||||
DeepJ is a Java Wrapper for the DeepL Free Public API. This was originally made by Steven (Forked From) and Updated by 5gi
|
26
build.gradle
26
build.gradle
@ -3,38 +3,40 @@ plugins {
|
||||
id 'maven-publish'
|
||||
id 'com.github.johnrengelman.shadow' version '5.2.0'
|
||||
}
|
||||
|
||||
group 'tech.nevets.deepj'
|
||||
version '1.1.0'
|
||||
apply plugin: 'maven-publish'
|
||||
group 'com.the5gi.deepj'
|
||||
version '1.4.3'
|
||||
|
||||
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_11
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
maven {
|
||||
url "https://git.nevets.tech/api/packages/5gi/maven"
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation group: 'org.json', name: 'json', version: '20210307'
|
||||
}
|
||||
|
||||
apply plugin: 'maven-publish'
|
||||
|
||||
publishing {
|
||||
publications{
|
||||
publish(MavenPublication) {
|
||||
artifact("target/DeepJ-$version" + "-all.jar") {
|
||||
artifact("nexus/DeepJ-$version" + ".jar") {
|
||||
extension 'jar'
|
||||
}
|
||||
}
|
||||
}
|
||||
repositories {
|
||||
maven {
|
||||
name 'nexus'
|
||||
url "https://repo.nevets.tech/repository/maven-releases/"
|
||||
credentials {
|
||||
username System.getenv('nexusUser')
|
||||
password System.getenv('nexusPass')
|
||||
}
|
||||
name 'gitea'
|
||||
url "https://git.nevets.tech/api/packages/5gi/maven/"
|
||||
credentials.username System.getenv('fivegiUserGit')
|
||||
credentials.password System.getenv('fivegiPassGit')
|
||||
}
|
||||
}
|
||||
}
|
||||
configurations.all {
|
||||
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
|
||||
}
|
207
src/main/java/com/the5gi/DeepJ/Translator.java
Normal file
207
src/main/java/com/the5gi/DeepJ/Translator.java
Normal file
@ -0,0 +1,207 @@
|
||||
package com.the5gi.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 {
|
||||
|
||||
//REQUEST INSIDE OF TRANSLATOR
|
||||
protected Request request;
|
||||
|
||||
//GLOBAL AUTH KEY (GAK)
|
||||
public static String globalAPIKey = "null";
|
||||
|
||||
//CONSTRUCTORS
|
||||
|
||||
public Translator() {
|
||||
//GAK
|
||||
if (globalAPIKey == null) {
|
||||
System.out.println("\n[DeepJ] You are currently using the Global API Key Translator Constructor." +"\n[DeepJ] If you are trying to use global API keys, Call the method \"Translator.setGlobalAuthKey(String key)\" and then re-use this constructor." + "\n[DeepJ] If you want to define an different authkey every time do: \"new Translator(String authKey)\"\n");
|
||||
} else {
|
||||
request = new Request(globalAPIKey);
|
||||
}
|
||||
}
|
||||
public Translator(boolean silent) {
|
||||
//GAK (SILENCABLE)
|
||||
if (globalAPIKey == null && !silent) {
|
||||
System.out.println("\n[DeepJ] You are currently using the Global API Key Translator Constructor." +"\n[DeepJ] If you are trying to use global API keys, Call the method \"Translator.setGlobalAuthKey(String key)\" and then re-use this constructor." + "\n[DeepJ] If you want to define an different authkey every time do: \"new Translator(String authKey)\"\n");
|
||||
} else {
|
||||
request = new Request(globalAPIKey);
|
||||
}
|
||||
}
|
||||
public Translator(String authKey) {
|
||||
//NORMAL REQUEST (GAK Notice) (NO SILENCE)
|
||||
if (!Objects.equals(globalAPIKey, "null")) {
|
||||
System.out.println("[DeepJ] Looks like you have defined a global API key already! You can use if by just typing \"new Translator()\"" + "\n instead of \"new Translator(String authKey)\". NOTE: This will work but just some advice :)");
|
||||
}
|
||||
request = new Request(authKey);
|
||||
}
|
||||
public Translator(String apiKey, boolean silence) {
|
||||
//NORMAL REQUEST (GAK SILENCEABLE)
|
||||
if (!Objects.equals(globalAPIKey, "null") && !silence) {
|
||||
System.out.println("[DeepJ] Looks like you have defined a global API key already! You can use if by just typing \"new Translator()\"" + "\n instead of \"new Translator(String authKey)\". NOTE: This will work but this is just some advice :)");
|
||||
}
|
||||
request = new Request(apiKey);
|
||||
}
|
||||
public static void setGlobalAPIKey(String key) {
|
||||
//SET GLOBAL API KEY (STATIC)
|
||||
globalAPIKey = key;
|
||||
}
|
||||
public static Translator newTranslator(String apiKey) {
|
||||
//NORMAL REQUEST (NO GAK) (STATIC)
|
||||
return new Translator(apiKey);
|
||||
}
|
||||
public static TranslatorBuilder newBuilder() {
|
||||
return new TranslatorBuilder();
|
||||
}
|
||||
public static Translator newTranslator() {
|
||||
//GAK (STATIC)
|
||||
if (globalAPIKey == null) {
|
||||
System.out.println("\n[DeepJ] You are currently using the Global API Key \".of()\" Translator." +
|
||||
"\n[DeepJ] If you are trying to use global API keys, Call the method \"Translator.setGlobalAPIKey(String key)\" and then re-use this method." +
|
||||
"\n[DeepJ] If you want to define an different authkey every time do: \"Translator.of(String apiKey)\"\n");
|
||||
return new Translator("null");
|
||||
} else {
|
||||
return new Translator();
|
||||
}
|
||||
}
|
||||
//END CONSTRUCTORS
|
||||
|
||||
//API KEYS
|
||||
public void setAPIKey(String key) {
|
||||
request.setAuthKey(key);
|
||||
}
|
||||
public String getAPIKey() {
|
||||
return request.authKey;
|
||||
}
|
||||
public void close() {
|
||||
request = new Request("null");
|
||||
}
|
||||
//END API KEYS
|
||||
|
||||
|
||||
//TRANSLATE
|
||||
public String translate(Language langToTranslateTo, String sourceMessage) {
|
||||
if (request.authKey == "null") {
|
||||
System.out.println("[DeepJ] This translator is closed! Please re-create!");
|
||||
return "null";
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
//API REQUEST CLASS
|
||||
protected static class Request {
|
||||
public String authKey;
|
||||
public Request(String authKey) {
|
||||
this.authKey = authKey;
|
||||
}
|
||||
public void setAuthKey(String key) {
|
||||
authKey = key;
|
||||
}
|
||||
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();
|
||||
URI deepLURI = URI.create("https://api-free.deepl.com/v2/translate?auth_key="+encodedAuthKey+"&target_lang="+langEnum.getApiString()+"&text="+encodedMessage);
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.GET()
|
||||
.header("Accept","*/*")
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
.uri(deepLURI)
|
||||
.build();
|
||||
HttpResponse<String> response = client.send(request,HttpResponse.BodyHandlers.ofString());
|
||||
return response.body();
|
||||
}
|
||||
}
|
||||
|
||||
public static class TranslatorBuilder {
|
||||
private boolean silent = false;
|
||||
private String authKey = null;
|
||||
private boolean GAK = false;
|
||||
|
||||
public Translator build() {
|
||||
if (GAK) {
|
||||
return new Translator(silent);
|
||||
} else {
|
||||
return new Translator(authKey, silent);
|
||||
}
|
||||
}
|
||||
|
||||
public TranslatorBuilder useGAK(boolean bool) {
|
||||
this.GAK = bool;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TranslatorBuilder setAuthKey(String authKey) {
|
||||
this.authKey = authKey;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TranslatorBuilder setSilent(boolean silent) {
|
||||
this.silent = silent;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
//DEEPL LANGUAGES
|
||||
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");
|
||||
|
||||
//API STRING IDENTIFIER
|
||||
private final String apiString;
|
||||
Language(String apiString) {
|
||||
this.apiString = apiString;
|
||||
}
|
||||
|
||||
//API STRING IDENTIFIER RETURN
|
||||
public String getApiString() {
|
||||
return this.apiString;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,115 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user