From 68bbf3c0f8a9a36116c5ccb9a26267ded1ad3840 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Sun, 17 Dec 2023 02:11:23 +0000 Subject: [PATCH] Improvements to reqwest4j init, warm up extractor, init youtube country on new thread --- src/main/java/me/kavin/piped/Main.java | 27 ++++++++++++++++++- .../java/me/kavin/piped/consts/Constants.java | 15 +---------- .../kavin/piped/utils/matrix/SyncRunner.java | 3 +++ 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/main/java/me/kavin/piped/Main.java b/src/main/java/me/kavin/piped/Main.java index 934dc8c..32295c3 100644 --- a/src/main/java/me/kavin/piped/Main.java +++ b/src/main/java/me/kavin/piped/Main.java @@ -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); diff --git a/src/main/java/me/kavin/piped/consts/Constants.java b/src/main/java/me/kavin/piped/consts/Constants.java index 840069a..cac0223 100644 --- a/src/main/java/me/kavin/piped/consts/Constants.java +++ b/src/main/java/me/kavin/piped/consts/Constants.java @@ -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"; diff --git a/src/main/java/me/kavin/piped/utils/matrix/SyncRunner.java b/src/main/java/me/kavin/piped/utils/matrix/SyncRunner.java index 091f41e..b4fab90 100644 --- a/src/main/java/me/kavin/piped/utils/matrix/SyncRunner.java +++ b/src/main/java/me/kavin/piped/utils/matrix/SyncRunner.java @@ -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!