Improvements to reqwest4j init, warm up extractor, init youtube country on new thread

This commit is contained in:
Kavin 2023-12-17 02:11:23 +00:00
parent 1bb0356c8b
commit 68bbf3c0f8
No known key found for this signature in database
GPG Key ID: 6E4598CA5C92C41F
3 changed files with 30 additions and 15 deletions

View File

@ -21,15 +21,18 @@ import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.extractor.localization.Localization;
import org.schabi.newpipe.extractor.services.youtube.YoutubeJavaScriptPlayerManager;
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor;
import org.schabi.newpipe.extractor.stream.StreamInfo;
import rocks.kavin.reqwest4j.ReqwestUtils;
import java.security.Security;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static me.kavin.piped.consts.Constants.MATRIX_SERVER;
import static me.kavin.piped.consts.Constants.*;
public class Main {
@ -38,10 +41,32 @@ public class Main {
Security.setProperty("crypto.policy", "unlimited");
Security.addProvider(new BouncyCastleProvider());
ReqwestUtils.init(REQWEST_PROXY_USER, REQWEST_PROXY, REQWEST_PROXY_PASS);
NewPipe.init(new DownloaderImpl(), new Localization("en", "US"), ContentCountry.DEFAULT, Multithreading.getCachedExecutor());
YoutubeStreamExtractor.forceFetchAndroidClient(true);
YoutubeStreamExtractor.forceFetchIosClient(true);
// Warm up the extractor
try {
StreamInfo.getInfo("https://www.youtube.com/watch?v=dQw4w9WgXcQ");
} catch (Exception ignored) {
}
// Find country code, used for georestricted videos
Thread.ofVirtual().start(() -> {
try {
var html = RequestUtils.sendGet("https://www.youtube.com/").get();
var regex = Pattern.compile("GL\":\"([A-Z]{2})\"", Pattern.MULTILINE);
var matcher = regex.matcher(html);
if (matcher.find()) {
YOUTUBE_COUNTRY = matcher.group(1);
}
} catch (Exception ignored) {
System.err.println("Failed to get country from YouTube!");
}
});
Sentry.init(options -> {
options.setDsn(Constants.SENTRY_DSN);
options.setRelease(Constants.VERSION);

View File

@ -103,7 +103,7 @@ public class Constants {
public static final String GEO_RESTRICTION_CHECKER_URL;
public static final String YOUTUBE_COUNTRY;
public static String YOUTUBE_COUNTRY;
public static final String VERSION;
@ -140,7 +140,6 @@ public class Constants {
REQWEST_PROXY = getProperty(prop, "REQWEST_PROXY");
REQWEST_PROXY_USER = getProperty(prop, "REQWEST_PROXY_USER");
REQWEST_PROXY_PASS = getProperty(prop, "REQWEST_PROXY_PASS");
ReqwestUtils.init(REQWEST_PROXY, REQWEST_PROXY_USER, REQWEST_PROXY_PASS);
FRONTEND_URL = getProperty(prop, "FRONTEND_URL", "https://piped.video");
COMPROMISED_PASSWORD_CHECK = Boolean.parseBoolean(getProperty(prop, "COMPROMISED_PASSWORD_CHECK", "true"));
DISABLE_REGISTRATION = Boolean.parseBoolean(getProperty(prop, "DISABLE_REGISTRATION", "false"));
@ -202,18 +201,6 @@ public class Constants {
.addInterceptor(BrotliInterceptor.INSTANCE);
h2client = builder.build();
h2_no_redir_client = builder_noredir.build();
String temp = null;
try {
var html = RequestUtils.sendGet("https://www.youtube.com/").get();
var regex = Pattern.compile("GL\":\"([A-Z]{2})\"", Pattern.MULTILINE);
var matcher = regex.matcher(html);
if (matcher.find()) {
temp = matcher.group(1);
}
} catch (Exception ignored) {
System.err.println("Failed to get country from YouTube!");
}
YOUTUBE_COUNTRY = temp;
VERSION = new File("VERSION").exists() ?
IOUtils.toString(new FileReader("VERSION")) :
"unknown";

View File

@ -132,6 +132,9 @@ public class SyncRunner implements Runnable {
if (!UNAUTHENTICATED && type.startsWith("video.piped.stream.bypass.")) {
switch (type) {
case "video.piped.stream.bypass.request" -> {
if (Constants.YOUTUBE_COUNTRY == null) {
continue;
}
FederatedGeoBypassRequest bypassRequest = mapper.treeToValue(content, FederatedGeoBypassRequest.class);
if (bypassRequest.getAllowedCountries().contains(Constants.YOUTUBE_COUNTRY)) {
// We're capable of helping another instance!