@@ -95,7 +95,7 @@ private static boolean isGoogleURL(String url) {
9595 final String host = u .getHost ();
9696 return host .startsWith ("google." ) || host .startsWith ("m.google." )
9797 || host .startsWith ("www.google." );
98- } catch (MalformedURLException e ) {
98+ } catch (final MalformedURLException e ) {
9999 return false ;
100100 }
101101 }
@@ -207,10 +207,10 @@ public static String getFeedUrlFrom(final String channelIdOrUser) {
207207 public static OffsetDateTime parseDateFrom (final String textualUploadDate ) throws ParsingException {
208208 try {
209209 return OffsetDateTime .parse (textualUploadDate );
210- } catch (DateTimeParseException e ) {
210+ } catch (final DateTimeParseException e ) {
211211 try {
212212 return LocalDate .parse (textualUploadDate ).atStartOfDay ().atOffset (ZoneOffset .UTC );
213- } catch (DateTimeParseException e1 ) {
213+ } catch (final DateTimeParseException e1 ) {
214214 throw new ParsingException ("Could not parse date: \" " + textualUploadDate + "\" " , e1 );
215215 }
216216 }
@@ -277,11 +277,11 @@ public static JsonObject getInitialData(final String html) throws ParsingExcepti
277277 try {
278278 final String initialData = Parser .matchGroup1 ("window\\ [\" ytInitialData\" \\ ]\\ s*=\\ s*(\\ {.*?\\ });" , html );
279279 return JsonParser .object ().from (initialData );
280- } catch (Parser .RegexException e ) {
280+ } catch (final Parser .RegexException e ) {
281281 final String initialData = Parser .matchGroup1 ("var\\ s*ytInitialData\\ s*=\\ s*(\\ {.*?\\ });" , html );
282282 return JsonParser .object ().from (initialData );
283283 }
284- } catch (JsonParserException | Parser .RegexException e ) {
284+ } catch (final JsonParserException | Parser .RegexException e ) {
285285 throw new ParsingException ("Could not get ytInitialData" , e );
286286 }
287287 }
@@ -342,7 +342,7 @@ private static void extractClientVersionAndKey() throws IOException, ExtractionE
342342 clientVersion = contextClientVersion ;
343343 break ;
344344 }
345- } catch (Parser .RegexException ignored ) {
345+ } catch (final Parser .RegexException ignored ) {
346346 }
347347 }
348348
@@ -352,10 +352,10 @@ private static void extractClientVersionAndKey() throws IOException, ExtractionE
352352
353353 try {
354354 key = Parser .matchGroup1 ("INNERTUBE_API_KEY\" :\" ([0-9a-zA-Z_-]+?)\" " , html );
355- } catch (Parser .RegexException e ) {
355+ } catch (final Parser .RegexException e ) {
356356 try {
357357 key = Parser .matchGroup1 ("innertubeApiKey\" :\" ([0-9a-zA-Z_-]+?)\" " , html );
358- } catch (Parser .RegexException ignored ) {
358+ } catch (final Parser .RegexException ignored ) {
359359 }
360360 }
361361 }
@@ -468,7 +468,7 @@ public static String[] getYoutubeMusicKeys() throws IOException, ReCaptchaExcept
468468 String key ;
469469 try {
470470 key = Parser .matchGroup1 ("INNERTUBE_API_KEY\" :\" ([0-9a-zA-Z_-]+?)\" " , html );
471- } catch (Parser .RegexException e ) {
471+ } catch (final Parser .RegexException e ) {
472472 key = Parser .matchGroup1 ("innertube_api_key\" :\" ([0-9a-zA-Z_-]+?)\" " , html );
473473 }
474474
@@ -477,10 +477,10 @@ public static String[] getYoutubeMusicKeys() throws IOException, ReCaptchaExcept
477477 String clientVersion ;
478478 try {
479479 clientVersion = Parser .matchGroup1 ("INNERTUBE_CONTEXT_CLIENT_VERSION\" :\" ([0-9\\ .]+?)\" " , html );
480- } catch (Parser .RegexException e ) {
480+ } catch (final Parser .RegexException e ) {
481481 try {
482482 clientVersion = Parser .matchGroup1 ("INNERTUBE_CLIENT_VERSION\" :\" ([0-9\\ .]+?)\" " , html );
483- } catch (Parser .RegexException ee ) {
483+ } catch (final Parser .RegexException ee ) {
484484 clientVersion = Parser .matchGroup1 ("innertube_context_client_version\" :\" ([0-9\\ .]+?)\" " , html );
485485 }
486486 }
@@ -491,7 +491,7 @@ public static String[] getYoutubeMusicKeys() throws IOException, ReCaptchaExcept
491491
492492
493493 @ Nullable
494- public static String getUrlFromNavigationEndpoint (JsonObject navigationEndpoint ) throws ParsingException {
494+ public static String getUrlFromNavigationEndpoint (final JsonObject navigationEndpoint ) throws ParsingException {
495495 if (navigationEndpoint .has ("urlEndpoint" )) {
496496 String internUrl = navigationEndpoint .getObject ("urlEndpoint" ).getString ("url" );
497497 if (internUrl .startsWith ("https://www.youtube.com/redirect?" )) {
@@ -508,7 +508,7 @@ public static String getUrlFromNavigationEndpoint(JsonObject navigationEndpoint)
508508 String url ;
509509 try {
510510 url = URLDecoder .decode (param .split ("=" )[1 ], UTF_8 );
511- } catch (UnsupportedEncodingException e ) {
511+ } catch (final UnsupportedEncodingException e ) {
512512 return null ;
513513 }
514514 return url ;
@@ -662,6 +662,31 @@ public static Response getResponse(final String url, final Localization localiza
662662 return response ;
663663 }
664664
665+ public static String extractCookieValue (final String cookieName , final Response response ) {
666+ final List <String > cookies = response .responseHeaders ().get ("set-cookie" );
667+ int startIndex ;
668+ String result = "" ;
669+ for (final String cookie : cookies ) {
670+ startIndex = cookie .indexOf (cookieName );
671+ if (startIndex != -1 ) {
672+ result = cookie .substring (startIndex + cookieName .length () + "=" .length (),
673+ cookie .indexOf (";" , startIndex ));
674+ }
675+ }
676+ return result ;
677+ }
678+
679+ public static JsonObject getJsonPostResponse (final String endpoint ,
680+ final byte [] body ,
681+ final Localization localization )
682+ throws IOException , ExtractionException {
683+
684+ final Response response = getDownloader ().post ("https://youtubei.googleapis.com/youtubei/v1/"
685+ + endpoint + "?key=" + getKey (), new HashMap <>(), body , localization );
686+
687+ return JsonUtils .toJsonObject (getValidJsonResponseBody (response ));
688+ }
689+
665690 public static JsonArray getJsonResponse (final String url , final Localization localization )
666691 throws IOException , ExtractionException {
667692 Map <String , List <String >> headers = new HashMap <>();
@@ -885,7 +910,7 @@ private static MetaInfo getClarificationRendererContent(final JsonObject clarifi
885910 metaInfo .addUrl (new URL (url ));
886911 final String description = getTextFromObject (clarificationRenderer .getObject ("secondarySource" ));
887912 metaInfo .addUrlText (description == null ? url : description );
888- } catch (MalformedURLException e ) {
913+ } catch (final MalformedURLException e ) {
889914 throw new ParsingException ("Could not get metadata info secondary URL" , e );
890915 }
891916 }
0 commit comments