Skip to content

Commit 68bbf3c

Browse files
committed
Improvements to reqwest4j init, warm up extractor, init youtube country on new thread
1 parent 1bb0356 commit 68bbf3c

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

src/main/java/me/kavin/piped/Main.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@
2121
import org.schabi.newpipe.extractor.localization.Localization;
2222
import org.schabi.newpipe.extractor.services.youtube.YoutubeJavaScriptPlayerManager;
2323
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor;
24+
import org.schabi.newpipe.extractor.stream.StreamInfo;
25+
import rocks.kavin.reqwest4j.ReqwestUtils;
2426

2527
import java.security.Security;
2628
import java.util.*;
2729
import java.util.concurrent.CompletableFuture;
2830
import java.util.concurrent.ConcurrentLinkedQueue;
2931
import java.util.concurrent.TimeUnit;
32+
import java.util.regex.Pattern;
3033
import java.util.stream.Collectors;
3134

32-
import static me.kavin.piped.consts.Constants.MATRIX_SERVER;
35+
import static me.kavin.piped.consts.Constants.*;
3336

3437
public class Main {
3538

@@ -38,10 +41,32 @@ public static void main(String[] args) throws Exception {
3841
Security.setProperty("crypto.policy", "unlimited");
3942
Security.addProvider(new BouncyCastleProvider());
4043

44+
ReqwestUtils.init(REQWEST_PROXY_USER, REQWEST_PROXY, REQWEST_PROXY_PASS);
45+
4146
NewPipe.init(new DownloaderImpl(), new Localization("en", "US"), ContentCountry.DEFAULT, Multithreading.getCachedExecutor());
4247
YoutubeStreamExtractor.forceFetchAndroidClient(true);
4348
YoutubeStreamExtractor.forceFetchIosClient(true);
4449

50+
// Warm up the extractor
51+
try {
52+
StreamInfo.getInfo("https://www.youtube.com/watch?v=dQw4w9WgXcQ");
53+
} catch (Exception ignored) {
54+
}
55+
56+
// Find country code, used for georestricted videos
57+
Thread.ofVirtual().start(() -> {
58+
try {
59+
var html = RequestUtils.sendGet("https://www.youtube.com/").get();
60+
var regex = Pattern.compile("GL\":\"([A-Z]{2})\"", Pattern.MULTILINE);
61+
var matcher = regex.matcher(html);
62+
if (matcher.find()) {
63+
YOUTUBE_COUNTRY = matcher.group(1);
64+
}
65+
} catch (Exception ignored) {
66+
System.err.println("Failed to get country from YouTube!");
67+
}
68+
});
69+
4570
Sentry.init(options -> {
4671
options.setDsn(Constants.SENTRY_DSN);
4772
options.setRelease(Constants.VERSION);

src/main/java/me/kavin/piped/consts/Constants.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public class Constants {
103103

104104
public static final String GEO_RESTRICTION_CHECKER_URL;
105105

106-
public static final String YOUTUBE_COUNTRY;
106+
public static String YOUTUBE_COUNTRY;
107107

108108
public static final String VERSION;
109109

@@ -140,7 +140,6 @@ public class Constants {
140140
REQWEST_PROXY = getProperty(prop, "REQWEST_PROXY");
141141
REQWEST_PROXY_USER = getProperty(prop, "REQWEST_PROXY_USER");
142142
REQWEST_PROXY_PASS = getProperty(prop, "REQWEST_PROXY_PASS");
143-
ReqwestUtils.init(REQWEST_PROXY, REQWEST_PROXY_USER, REQWEST_PROXY_PASS);
144143
FRONTEND_URL = getProperty(prop, "FRONTEND_URL", "https://piped.video");
145144
COMPROMISED_PASSWORD_CHECK = Boolean.parseBoolean(getProperty(prop, "COMPROMISED_PASSWORD_CHECK", "true"));
146145
DISABLE_REGISTRATION = Boolean.parseBoolean(getProperty(prop, "DISABLE_REGISTRATION", "false"));
@@ -202,18 +201,6 @@ else if (key.startsWith("frontend."))
202201
.addInterceptor(BrotliInterceptor.INSTANCE);
203202
h2client = builder.build();
204203
h2_no_redir_client = builder_noredir.build();
205-
String temp = null;
206-
try {
207-
var html = RequestUtils.sendGet("https://www.youtube.com/").get();
208-
var regex = Pattern.compile("GL\":\"([A-Z]{2})\"", Pattern.MULTILINE);
209-
var matcher = regex.matcher(html);
210-
if (matcher.find()) {
211-
temp = matcher.group(1);
212-
}
213-
} catch (Exception ignored) {
214-
System.err.println("Failed to get country from YouTube!");
215-
}
216-
YOUTUBE_COUNTRY = temp;
217204
VERSION = new File("VERSION").exists() ?
218205
IOUtils.toString(new FileReader("VERSION")) :
219206
"unknown";

src/main/java/me/kavin/piped/utils/matrix/SyncRunner.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ public void run() {
132132
if (!UNAUTHENTICATED && type.startsWith("video.piped.stream.bypass.")) {
133133
switch (type) {
134134
case "video.piped.stream.bypass.request" -> {
135+
if (Constants.YOUTUBE_COUNTRY == null) {
136+
continue;
137+
}
135138
FederatedGeoBypassRequest bypassRequest = mapper.treeToValue(content, FederatedGeoBypassRequest.class);
136139
if (bypassRequest.getAllowedCountries().contains(Constants.YOUTUBE_COUNTRY)) {
137140
// We're capable of helping another instance!

0 commit comments

Comments
 (0)