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;
import io.gitlab.jfronny.commons.http.client.HttpClient;
import io.gitlab.jfronny.libjf.translate.api.TranslateException;
import io.gitlab.jfronny.libjf.translate.impl.AbstractTranslateService;
import org.apache.commons.lang3.StringEscapeUtils;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
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.file.Files;
import java.nio.file.Path;
@ -103,7 +97,7 @@ public class GoogleTranslateService extends AbstractTranslateService<GoogleTrans
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)
return null;
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);
}
private static String get2(String url) {
// 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
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) throws URISyntaxException, IOException {
return HttpClient.get(url).sendString();
}
private static String get(String url) {
// Technically, we should be using HttpClient, but Google Translate doesn't like it for some reason and this mess bypasses that
// based on https://github.com/jhy/jsoup/blob/master/src/main/java/org/jsoup/helper/HttpConnection.java
try {
HttpsURLConnection connection = (HttpsURLConnection) URI.create(url).toURL().openConnection();
connection.setRequestMethod("GET");
connection.setInstanceFollowRedirects(false);
connection.setConnectTimeout(5000);
connection.setReadTimeout(2500);
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();
if (connection.getResponseCode() / 100 != 2) {
String loc = connection.getHeaderField("Location");
if (loc == null) loc = "";
else loc = " (redirected to " + loc + ")";
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);
}
}
// Alternative methods to get the page source (kept for reference)
// The HttpClient here is not the same as the one in libjf-commons, so adjust imports if you want to use them again
// private static String get2(String url) {
// // 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
// try {
// HttpClient client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.ALWAYS).build();
// 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());
// 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 get3(String url) {
// // Technically, we should be using HttpClient, but Google Translate doesn't like it for some reason and this mess bypasses that
// // based on https://github.com/jhy/jsoup/blob/master/src/main/java/org/jsoup/helper/HttpConnection.java
// try {
// HttpsURLConnection connection = (HttpsURLConnection) URI.create(url).toURL().openConnection();
// connection.setRequestMethod("GET");
// connection.setInstanceFollowRedirects(false);
// connection.setConnectTimeout(5000);
// connection.setReadTimeout(2500);
// 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();
// if (connection.getResponseCode() / 100 != 2) {
// String loc = connection.getHeaderField("Location");
// if (loc == null) loc = "";
// else loc = " (redirected to " + loc + ")";
// 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);
// }
// }
}