chore(translate): more text for workaround
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 19:04:00 +02:00
parent b47bc6fd0a
commit 5279dcb759
Signed by: Johannes
GPG Key ID: E76429612C2929F4

View File

@ -11,6 +11,9 @@ 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;
@ -108,8 +111,22 @@ 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) {
// Technically, we should be using HttpClient, but Google Translate doesn't like it for some reason
// 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();
@ -120,7 +137,10 @@ public class GoogleTranslateService extends AbstractTranslateService<GoogleTrans
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) {
throw new IOException("Could not get page: " + connection.getResponseCode());
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();