80 lines
3.4 KiB
Java
80 lines
3.4 KiB
Java
package io.gitlab.jfronny.googlechat.api;
|
|
|
|
import io.gitlab.jfronny.googlechat.GoogleChat;
|
|
import org.apache.commons.lang3.StringEscapeUtils;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStreamReader;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.net.URLEncoder;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
public class GoogleService {
|
|
private static final Pattern TRANSLATION_RESULT = Pattern.compile("class=\"result-container\">([^<]*)</div>", Pattern.MULTILINE);
|
|
|
|
public static String translate(String textToTranslate, Language translateFrom, Language translateTo) {
|
|
if (textToTranslate == null)
|
|
return null;
|
|
String pageSource = "";
|
|
try {
|
|
pageSource = getPageSource(textToTranslate, translateFrom.value, translateTo.value);
|
|
Matcher matcher = TRANSLATION_RESULT.matcher(pageSource);
|
|
if (matcher.find()) {
|
|
String match = matcher.group(1);
|
|
if (match != null && !match.isEmpty()) {
|
|
return StringEscapeUtils.unescapeHtml4(match);
|
|
}
|
|
}
|
|
GoogleChat.LOGGER.error("Could not translate \"" + textToTranslate + "\": result page couldn't be parsed");
|
|
return null;
|
|
} catch (Exception e) {
|
|
try {
|
|
Path p = Files.createTempFile("translater-pagedump-", ".html").toAbsolutePath();
|
|
Files.writeString(p, pageSource);
|
|
GoogleChat.LOGGER.error("Could not translate string, see dumped page at " + p, e);
|
|
} catch (IOException ioe) {
|
|
GoogleChat.LOGGER.error("Could not translate string and the page could not be dumped", ioe);
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static String getPageSource(String textToTranslate, String translateFrom, String translateTo)
|
|
throws Exception {
|
|
if (textToTranslate == null)
|
|
return null;
|
|
String pageUrl = String.format("https://translate.google.com/m?hl=en&sl=%s&tl=%s&ie=UTF-8&prev=_m&q=%s",
|
|
translateFrom, translateTo, URLEncoder.encode(textToTranslate.trim(), StandardCharsets.UTF_8));
|
|
URL url = new URL(pageUrl);
|
|
HttpURLConnection connection = null;
|
|
BufferedReader bufferedReader = null;
|
|
StringBuilder pageSource = new StringBuilder();
|
|
try {
|
|
connection = (HttpURLConnection) url.openConnection();
|
|
connection.setConnectTimeout(5000);
|
|
connection.setRequestProperty("User-Agent",
|
|
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
|
|
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
|
|
String line;
|
|
while ((line = bufferedReader.readLine()) != null) {
|
|
pageSource.append(line).append('\n');
|
|
}
|
|
return pageSource.toString();
|
|
} catch (Exception e) {
|
|
GoogleChat.LOGGER.error("Could not load translation from google", e);
|
|
return null;
|
|
} finally {
|
|
if (connection != null)
|
|
connection.disconnect();
|
|
if (bufferedReader != null)
|
|
bufferedReader.close();
|
|
}
|
|
}
|
|
}
|