Skip to content

Commit

Permalink
Added Baidu REST client and GUI frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
ringosham committed Feb 21, 2021
1 parent b8d226f commit 0319770
Show file tree
Hide file tree
Showing 20 changed files with 624 additions and 173 deletions.
6 changes: 6 additions & 0 deletions .idea/copyright/GPL_v3.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/copyright/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions .idea/modules/TranslationMod.main.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions .idea/modules/TranslationMod.test.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 25 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
minecraft_version=1.16.2
forge_version=1.16.2-33.0.5
#
# Copyright (C) 2021 Ringosham
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

minecraft_version=1.16.4
forge_version=1.16.4-35.1.4
mappings_channel=snapshot
mappings_version=20200723-1.16.1
mappings_version=20201028-1.16.3
mod_version=5.1.1
# suppress inspection "UnusedProperty"
mod_id=translationmod
mod_name=Real time translation mod
mod_vendor=Ringosham
group_name=com.ringosham.translationmod
# Gradle import fix
# suppress inspection "UnusedProperty"
org.gradle.daemon=false
org.gradle.daemon=false
# suppress inspection "UnusedProperty"
# Gradle needs more memory
org.gradle.jvmargs=-Xmx3G
19 changes: 18 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
#
# Copyright (C) 2021 Ringosham
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

#Sat Sep 14 01:19:13 BST 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-all.zip
19 changes: 18 additions & 1 deletion src/main/java/com/ringosham/translationmod/TranslationMod.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Copyright (C) 2021 Ringosham
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.ringosham.translationmod;

import com.ringosham.translationmod.client.LangManager;
Expand All @@ -14,7 +31,7 @@
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;

/**
* Real time translation project - Licensed under GPL v3
* Real time translation project
*
* @author Ringosham
* @since 9/11/2015
Expand Down
98 changes: 98 additions & 0 deletions src/main/java/com/ringosham/translationmod/client/BaiduClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (C) 2021 Ringosham
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.ringosham.translationmod.client;

import com.ringosham.translationmod.client.types.Language;
import com.ringosham.translationmod.client.types.RequestResult;
import com.ringosham.translationmod.common.ConfigManager;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

public class BaiduClient extends RESTClient {

private static final char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

public BaiduClient() {
super("https://fanyi-api.baidu.com/api/trans/vip/translate");
}

@Override
public RequestResult translate(String message, Language from, Language to) {
Map<String, String> queryParam = new HashMap<>();
String encodedMessage = null;
//Percent encode message
try {
encodedMessage = URLEncoder.encode(message, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException ignored) {
}
//Query message
queryParam.put("q", encodedMessage);
//From language
queryParam.put("to", to.getBaiduCode());
//To language
queryParam.put("from", from.getBaiduCode());
//App ID
queryParam.put("appid", ConfigManager.config.baiduAppId.get());
//Salt
String salt = String.valueOf(System.currentTimeMillis());
queryParam.put("salt", salt);
//Signature
//Must use unencoded message for signature
queryParam.put("sign", sign(ConfigManager.config.baiduAppId.get(), message, salt, ConfigManager.config.baiduKey.get()));
Response response = POST(queryParam, "application/x-www-form-urlencoded");
if (response.getResponseCode() == 200) {
//Baidu does not follow HTTP codes at all. It's 200 regardless of success or failure
//wrrrrryyyyyyyyyy
} else {
//Most likely internal server error
}
//TODO Process response
return null;
}

private String sign(String appid, String q, String salt, String key) {
//Credits to Baidu's example
String rawSignString = appid + q + salt + key;
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] rawSignBytes = rawSignString.getBytes(StandardCharsets.UTF_8);
byte[] signBytes = digest.digest(rawSignBytes);
//Turn it to a hex string
char[] signChars = new char[signBytes.length * 2];
int index = 0;
for (byte b : signBytes) {
//Turn MSB to hex
//Bytes in Java are signed. Unsigned shift by 4 to get MSB
//(Need to AND 0xf because the shift converted the byte back to int)
signChars[index++] = hexDigits[b >>> 4 & 0xf];
//Turn LSB to hex
//Bitwise AND to eliminate MSB
signChars[index++] = hexDigits[b & 0xf];
}
return new String(signChars);
} catch (NoSuchAlgorithmException ignored) {
}
return null;
}
}
Loading

0 comments on commit 0319770

Please sign in to comment.