2121import org .schabi .newpipe .extractor .localization .Localization ;
2222import org .schabi .newpipe .extractor .services .youtube .YoutubeJavaScriptPlayerManager ;
2323import org .schabi .newpipe .extractor .services .youtube .extractors .YoutubeStreamExtractor ;
24+ import org .schabi .newpipe .extractor .stream .StreamInfo ;
25+ import rocks .kavin .reqwest4j .ReqwestUtils ;
2426
2527import java .security .Security ;
2628import java .util .*;
2729import java .util .concurrent .CompletableFuture ;
2830import java .util .concurrent .ConcurrentLinkedQueue ;
2931import java .util .concurrent .TimeUnit ;
32+ import java .util .regex .Pattern ;
3033import java .util .stream .Collectors ;
3134
32- import static me .kavin .piped .consts .Constants .MATRIX_SERVER ;
35+ import static me .kavin .piped .consts .Constants .* ;
3336
3437public 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 );
0 commit comments