chore(translate): comment out workaround again since google might have just been flaky
All checks were successful
ci/woodpecker/push/docs Pipeline was successful
ci/woodpecker/push/jfmod Pipeline was successful

This commit is contained in:
Johannes Frohnmeyer 2024-05-11 21:08:00 +02:00
parent 5279dcb759
commit 03ffeb9eee
Signed by: Johannes
GPG Key ID: E76429612C2929F4

View File

@ -1,19 +1,13 @@
package io.gitlab.jfronny.libjf.translate.impl.google; package io.gitlab.jfronny.libjf.translate.impl.google;
import io.gitlab.jfronny.commons.http.client.HttpClient;
import io.gitlab.jfronny.libjf.translate.api.TranslateException; import io.gitlab.jfronny.libjf.translate.api.TranslateException;
import io.gitlab.jfronny.libjf.translate.impl.AbstractTranslateService; import io.gitlab.jfronny.libjf.translate.impl.AbstractTranslateService;
import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringEscapeUtils;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URLEncoder; 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.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -103,7 +97,7 @@ public class GoogleTranslateService extends AbstractTranslateService<GoogleTrans
return NAME; return NAME;
} }
private static String getPageSource(String textToTranslate, String translateFrom, String translateTo) { private static String getPageSource(String textToTranslate, String translateFrom, String translateTo) throws URISyntaxException, IOException {
if (textToTranslate == null) if (textToTranslate == null)
return 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", String pageUrl = String.format("https://translate.google.com/m?hl=en&sl=%s&tl=%s&ie=UTF-8&prev=_m&q=%s",
@ -111,52 +105,58 @@ public class GoogleTranslateService extends AbstractTranslateService<GoogleTrans
return get(pageUrl); return get(pageUrl);
} }
private static String get2(String url) { private static String get(String url) throws URISyntaxException, IOException {
// An attempt to use the stuff wrapped by HttpClient. Will need to test and (if it works) adjust commons so it can be used again return HttpClient.get(url).sendString();
try {
HttpClient client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.ALWAYS).build();
HttpResponse<String> response = client.send(HttpRequest.newBuilder().uri(new URI(url)).build(), HttpResponse.BodyHandlers.ofString());
if (response.statusCode() / 100 != 2) {
throw new IOException("Could not get page: " + response.statusCode());
}
return response.body();
} catch (URISyntaxException | IOException | InterruptedException e) {
throw new RuntimeException(e);
}
} }
private static String get(String url) { // Alternative methods to get the page source (kept for reference)
// Technically, we should be using HttpClient, but Google Translate doesn't like it for some reason and this mess bypasses that // The HttpClient here is not the same as the one in libjf-commons, so adjust imports if you want to use them again
// based on https://github.com/jhy/jsoup/blob/master/src/main/java/org/jsoup/helper/HttpConnection.java // private static String get2(String url) {
try { // // An attempt to use the stuff wrapped by HttpClient. Will need to test and (if it works) adjust commons, so it can be used again
HttpsURLConnection connection = (HttpsURLConnection) URI.create(url).toURL().openConnection(); // try {
connection.setRequestMethod("GET"); // HttpClient client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.ALWAYS).build();
connection.setInstanceFollowRedirects(false); // HttpResponse<String> response = client.send(HttpRequest.newBuilder().uri(new URI(url)).header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36").build(), HttpResponse.BodyHandlers.ofString());
connection.setConnectTimeout(5000); // if (response.statusCode() / 100 != 2) {
connection.setReadTimeout(2500); // throw new IOException("Could not get page: " + response.statusCode());
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"); // }
connection.connect(); // return response.body();
if (connection.getResponseCode() / 100 != 2) { // } catch (URISyntaxException | IOException | InterruptedException e) {
String loc = connection.getHeaderField("Location"); // throw new RuntimeException(e);
if (loc == null) loc = ""; // }
else loc = " (redirected to " + loc + ")"; // }
throw new IOException("Could not get page: " + connection.getResponseCode() + loc);
} // private static String get3(String url) {
connection.getRequestMethod(); // // Technically, we should be using HttpClient, but Google Translate doesn't like it for some reason and this mess bypasses that
connection.getURL(); // // based on https://github.com/jhy/jsoup/blob/master/src/main/java/org/jsoup/helper/HttpConnection.java
connection.getResponseCode(); // try {
connection.getResponseMessage(); // HttpsURLConnection connection = (HttpsURLConnection) URI.create(url).toURL().openConnection();
connection.getContentType(); // connection.setRequestMethod("GET");
try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) { // connection.setInstanceFollowRedirects(false);
StringBuilder sb = new StringBuilder(); // connection.setConnectTimeout(5000);
String line; // connection.setReadTimeout(2500);
while ((line = br.readLine()) != null) { // connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36");
sb.append(line).append('\n'); // connection.connect();
} // if (connection.getResponseCode() / 100 != 2) {
return sb.toString(); // String loc = connection.getHeaderField("Location");
} // if (loc == null) loc = "";
} catch (IOException e) { // else loc = " (redirected to " + loc + ")";
throw new RuntimeException(e); // throw new IOException("Could not get page: " + connection.getResponseCode() + loc);
} // }
} // connection.getRequestMethod();
// connection.getURL();
// connection.getResponseCode();
// connection.getResponseMessage();
// connection.getContentType();
// try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
// StringBuilder sb = new StringBuilder();
// String line;
// while ((line = br.readLine()) != null) {
// sb.append(line).append('\n');
// }
// return sb.toString();
// }
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
// }
} }