Skip to content

Commit

Permalink
Parsing response from Baidu
Browse files Browse the repository at this point in the history
  • Loading branch information
ringosham committed Feb 21, 2021
1 parent d6f6874 commit 5b0d9f6
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .idea/modules/TranslationMod.main.iml

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

2 changes: 1 addition & 1 deletion .idea/modules/TranslationMod.test.iml

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

2 changes: 1 addition & 1 deletion TranslationMod.iml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="TranslationMod" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="com.ringosham.translationmod" external.system.module.version="5.1.1" type="JAVA_MODULE" version="4">
<module external.linked.project.id="TranslationMod" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="com.ringosham.translationmod" external.system.module.version="6.0" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/com/ringosham/translationmod/client/BaiduClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@

package com.ringosham.translationmod.client;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.ringosham.translationmod.client.types.Language;
import com.ringosham.translationmod.client.types.RequestResult;
import com.ringosham.translationmod.client.types.baidu.TranslateError;
import com.ringosham.translationmod.client.types.baidu.TranslateSuccess;
import com.ringosham.translationmod.common.ConfigManager;

import java.io.UnsupportedEncodingException;
Expand Down Expand Up @@ -64,12 +68,24 @@ public RequestResult translate(String message, Language from, Language to) {
Response response = POST(queryParam, "application/x-www-form-urlencoded");
if (response.getResponseCode() == 200) {
//Baidu does not follow standard REST response codes at all. It's 200 regardless of success or failure
//This is utterly retarded
//wrrrrryyyyyyyyyy
Gson gson = new Gson();
JsonObject parse = gson.fromJson(response.getEntity(), JsonObject.class);
if (!parse.has("error_code")) {
//Success
TranslateSuccess success = gson.fromJson(response.getEntity(), TranslateSuccess.class);
Language sourceLang = LangManager.getInstance().findLanguageFromBaidu(success.getFrom());
return new RequestResult(response.getResponseCode(), success.getTrans_result()[0].getDstDecoded(), sourceLang, to);
} else {
//Error
TranslateError error = gson.fromJson(response.getEntity(), TranslateError.class);
return new RequestResult(error.getError_code(), null, null, null);
}
} else {
//Most likely internal server error
//Most likely some arbitrary internal server error
return new RequestResult(response.getResponseCode(), null, null, null);
}
//TODO Process response
return null;
}

private String sign(String appid, String q, String salt, String key) {
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/ringosham/translationmod/client/LangManager.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.client;

import com.google.gson.Gson;
Expand Down Expand Up @@ -54,6 +71,13 @@ public Language findLanguageFromGoogle(String googleCode) {
return null;
}

public Language findLanguageFromBaidu(String baiduCode) {
for (Language lang : languages)
if (lang.getBaiduCode().equals(baiduCode))
return lang;
return null;
}

public List<Language> getAllLanguages() {
return new ArrayList<>(languages);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.types.baidu;

public class TranslateError {
private int error_code;
private String error_msg;

public int getError_code() {
return error_code;
}

public String getError_msg() {
return error_msg;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.types.baidu;

import org.apache.commons.lang3.StringEscapeUtils;

public class TranslateResult {
private String src;
private String dst;

public String getSrc() {
return src;
}

public String getDst() {
return dst;
}

//Uses deprecated class but it should work for our case
//Importing a library just for one method is stupid
@SuppressWarnings("deprecation")
public String getSrcDecoded() {
return StringEscapeUtils.unescapeJson(src);
}

@SuppressWarnings("deprecation")
public String getDstDecoded() {
return StringEscapeUtils.unescapeJson(dst);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.types.baidu;

public class TranslateSuccess {
private String from;
private String to;
private TranslateResult[] trans_result;

public String getFrom() {
return from;
}

public String getTo() {
return to;
}

public TranslateResult[] getTrans_result() {
return trans_result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class Translator extends Thread {
//Cache about 100 messages
private static final int CACHE_SIZE = 100;

private static boolean warnLimit = false;

public Translator(String message, Language from, Language to) {
this.message = message;
this.from = from;
Expand Down Expand Up @@ -140,20 +142,58 @@ private void logException(RequestResult transRequest) {
ChatUtil.printChatMessage(true, "Google translate has stopped responding. Pausing translations", TextFormatting.YELLOW);
break;
case 403:
Log.logger.error("Exceeded API quota");
Log.logger.error("Google API >> Exceeded API quota");
ChatUtil.printChatMessage(true, "You have exceeded your quota. Please check your quota settings", TextFormatting.RED);
ChatUtil.printChatMessage(true, "Falling back to free version until you restart the game", TextFormatting.RED);
GooglePaidClient.setDisable();
break;
case 400:
Log.logger.error("API key invalid");
Log.logger.error("Google API >> API key invalid");
ChatUtil.printChatMessage(true, "API key invalid. If you do not wish to use a key, please remove it from the settings", TextFormatting.RED);
break;
case 500:
Log.logger.error("Failed to determine source language: " + transRequest.getMessage());
Log.logger.error("Google API >> Failed to determine source language: " + transRequest.getMessage());
break;
case 52001:
Log.logger.error("Baidu API >> Connection timeout");
break;
case 52002:
Log.logger.error("Baidu API >> Server side failure");
ChatUtil.printChatMessage(true, "Server side failure. Cannot translate", TextFormatting.RED);
break;
case 52003:
Log.logger.error("Baidu API >> Unauthorized request");
ChatUtil.printChatMessage(true, "Authentication failure. Please check your App ID and API key", TextFormatting.RED);
break;
case 54003:
Log.logger.warn("Baidu API >> Restricted request per second limit");
if (!warnLimit) {
ChatUtil.printChatMessage(true, "Request restricted due to tier limits. Consider upgrading your plan", TextFormatting.YELLOW);
warnLimit = true;
}
break;
case 54004:
Log.logger.error("Baidu API >> Not enough balance");
ChatUtil.printChatMessage(true, "Balance insufficient. Please go to Baidu's control panel to top up", TextFormatting.RED);
break;
case 54005:
Log.logger.error("Baidu API >> Request too large");
ChatUtil.printChatMessage(true, "Request denied due to size too large", TextFormatting.YELLOW);
break;
case 58001:
Log.logger.error("Baidu API >> Translation direction not support");
ChatUtil.printChatMessage(true, "Cannot translate from " + transRequest.getFrom().getName() + " to " + transRequest.getTo().getName(), TextFormatting.RED);
break;
case 58002:
Log.logger.error("Baidu API >> Translation service is not enabled");
ChatUtil.printChatMessage(true, "Translation service is not enabled. Please turn it on in Baidu's control panel", TextFormatting.RED);
break;
case 90107:
Log.logger.error("Baidu API >> Verification failed");
ChatUtil.printChatMessage(true, "Verification failed. Please check your verification status", TextFormatting.RED);
break;
default:
Log.logger.error("Unknown error: " + transRequest.getMessage());
Log.logger.error("Unknown error/Server side failure: " + transRequest.getMessage());
break;
}
}
Expand Down

0 comments on commit 5b0d9f6

Please sign in to comment.