2828import static org .schabi .newpipe .extractor .utils .Utils .getStringResultFromRegexArray ;
2929import static org .schabi .newpipe .extractor .utils .Utils .isNullOrEmpty ;
3030
31+ import static java .util .Collections .singletonList ;
32+
3133import com .grack .nanojson .JsonArray ;
3234import com .grack .nanojson .JsonBuilder ;
3335import com .grack .nanojson .JsonObject ;
@@ -595,9 +597,9 @@ public static boolean areHardcodedClientVersionAndKeyValid()
595597 // @formatter:on
596598
597599 final Map <String , List <String >> headers = new HashMap <>();
598- headers .put ("X-YouTube-Client-Name" , Collections . singletonList ("1" ));
600+ headers .put ("X-YouTube-Client-Name" , singletonList ("1" ));
599601 headers .put ("X-YouTube-Client-Version" ,
600- Collections . singletonList (HARDCODED_CLIENT_VERSION ));
602+ singletonList (HARDCODED_CLIENT_VERSION ));
601603
602604 // This endpoint is fetched by the YouTube website to get the items of its main menu and is
603605 // pretty lightweight (around 30kB)
@@ -619,8 +621,8 @@ private static void extractClientVersionAndKeyFromSwJs()
619621 }
620622 final String url = "https://www.youtube.com/sw.js" ;
621623 final Map <String , List <String >> headers = new HashMap <>();
622- headers .put ("Origin" , Collections . singletonList ("https://www.youtube.com" ));
623- headers .put ("Referer" , Collections . singletonList ("https://www.youtube.com" ));
624+ headers .put ("Origin" , singletonList ("https://www.youtube.com" ));
625+ headers .put ("Referer" , singletonList ("https://www.youtube.com" ));
624626 final String response = getDownloader ().get (url , headers ).responseBody ();
625627 try {
626628 clientVersion = getStringResultFromRegexArray (response ,
@@ -641,9 +643,7 @@ private static void extractClientVersionAndKeyFromHtmlSearchResultsPage()
641643 }
642644 // Don't provide a search term in order to have a smaller response
643645 final String url = "https://www.youtube.com/results?search_query=&ucbcb=1" ;
644- final Map <String , List <String >> headers = new HashMap <>();
645- addCookieHeader (headers );
646- final String html = getDownloader ().get (url , headers ).responseBody ();
646+ final String html = getDownloader ().get (url , getCookieHeader ()).responseBody ();
647647 final JsonObject initialData = getInitialData (html );
648648 final JsonArray serviceTrackingParams = initialData .getObject ("responseContext" )
649649 .getArray ("serviceTrackingParams" );
@@ -821,13 +821,13 @@ public static boolean isHardcodedYoutubeMusicKeyValid() throws IOException,
821821 // @formatter:on
822822
823823 final Map <String , List <String >> headers = new HashMap <>();
824- headers .put ("X-YouTube-Client-Name" , Collections . singletonList (
824+ headers .put ("X-YouTube-Client-Name" , singletonList (
825825 HARDCODED_YOUTUBE_MUSIC_KEY [1 ]));
826- headers .put ("X-YouTube-Client-Version" , Collections . singletonList (
826+ headers .put ("X-YouTube-Client-Version" , singletonList (
827827 HARDCODED_YOUTUBE_MUSIC_KEY [2 ]));
828- headers .put ("Origin" , Collections . singletonList ("https://music.youtube.com" ));
829- headers .put ("Referer" , Collections . singletonList ("music.youtube.com" ));
830- headers .put ("Content-Type" , Collections . singletonList ("application/json" ));
828+ headers .put ("Origin" , singletonList ("https://music.youtube.com" ));
829+ headers .put ("Referer" , singletonList ("music.youtube.com" ));
830+ headers .put ("Content-Type" , singletonList ("application/json" ));
831831
832832 final Response response = getDownloader ().post (url , headers , json );
833833 // Ensure to have a valid response
@@ -851,18 +851,16 @@ public static String[] getYoutubeMusicKey()
851851 try {
852852 final String url = "https://music.youtube.com/sw.js" ;
853853 final Map <String , List <String >> headers = new HashMap <>();
854- headers .put ("Origin" , Collections . singletonList ("https://music.youtube.com" ));
855- headers .put ("Referer" , Collections . singletonList ("https://music.youtube.com" ));
854+ headers .put ("Origin" , singletonList ("https://music.youtube.com" ));
855+ headers .put ("Referer" , singletonList ("https://music.youtube.com" ));
856856 final String response = getDownloader ().get (url , headers ).responseBody ();
857857 musicClientVersion = getStringResultFromRegexArray (response ,
858858 INNERTUBE_CONTEXT_CLIENT_VERSION_REGEXES , 1 );
859859 musicKey = getStringResultFromRegexArray (response , INNERTUBE_API_KEY_REGEXES , 1 );
860860 musicClientName = Parser .matchGroup1 (INNERTUBE_CLIENT_NAME_REGEX , response );
861861 } catch (final Exception e ) {
862862 final String url = "https://music.youtube.com/?ucbcb=1" ;
863- final Map <String , List <String >> headers = new HashMap <>();
864- addCookieHeader (headers );
865- final String html = getDownloader ().get (url , headers ).responseBody ();
863+ final String html = getDownloader ().get (url , getCookieHeader ()).responseBody ();
866864
867865 musicKey = getStringResultFromRegexArray (html , INNERTUBE_API_KEY_REGEXES , 1 );
868866 musicClientVersion = getStringResultFromRegexArray (html ,
@@ -1066,7 +1064,7 @@ public static JsonObject getJsonPostResponse(final String endpoint,
10661064 throws IOException , ExtractionException {
10671065 final Map <String , List <String >> headers = new HashMap <>();
10681066 addClientInfoHeaders (headers );
1069- headers .put ("Content-Type" , Collections . singletonList ("application/json" ));
1067+ headers .put ("Content-Type" , singletonList ("application/json" ));
10701068
10711069 final Response response = getDownloader ().post (YOUTUBEI_V1_URL + endpoint + "?key="
10721070 + getKey () + DISABLE_PRETTY_PRINT_PARAMETER , headers , body , localization );
@@ -1100,9 +1098,9 @@ private static JsonObject getMobilePostResponse(
11001098 @ Nonnull final String innerTubeApiKey ,
11011099 @ Nullable final String endPartOfUrlRequest ) throws IOException , ExtractionException {
11021100 final Map <String , List <String >> headers = new HashMap <>();
1103- headers .put ("Content-Type" , Collections . singletonList ("application/json" ));
1104- headers .put ("User-Agent" , Collections . singletonList (userAgent ));
1105- headers .put ("X-Goog-Api-Format-Version" , Collections . singletonList ("2" ));
1101+ headers .put ("Content-Type" , singletonList ("application/json" ));
1102+ headers .put ("User-Agent" , singletonList (userAgent ));
1103+ headers .put ("X-Goog-Api-Format-Version" , singletonList ("2" ));
11061104
11071105 final String baseEndpointUrl = YOUTUBEI_V1_GAPIS_URL + endpoint + "?key=" + innerTubeApiKey
11081106 + DISABLE_PRETTY_PRINT_PARAMETER ;
@@ -1306,16 +1304,22 @@ public static void addYouTubeHeaders(final Map<String, List<String>> headers)
13061304 */
13071305 public static void addClientInfoHeaders (@ Nonnull final Map <String , List <String >> headers )
13081306 throws IOException , ExtractionException {
1309- headers .computeIfAbsent ("Origin" , k -> Collections .singletonList (
1310- "https://www.youtube.com" ));
1311- headers .computeIfAbsent ("Referer" , k -> Collections .singletonList (
1312- "https://www.youtube.com" ));
1313- headers .computeIfAbsent ("X-YouTube-Client-Name" , k -> Collections .singletonList ("1" ));
1307+ headers .computeIfAbsent ("Origin" , k -> singletonList ("https://www.youtube.com" ));
1308+ headers .computeIfAbsent ("Referer" , k -> singletonList ("https://www.youtube.com" ));
1309+ headers .computeIfAbsent ("X-YouTube-Client-Name" , k -> singletonList ("1" ));
13141310 if (headers .get ("X-YouTube-Client-Version" ) == null ) {
1315- headers .put ("X-YouTube-Client-Version" , Collections . singletonList (getClientVersion ()));
1311+ headers .put ("X-YouTube-Client-Version" , singletonList (getClientVersion ()));
13161312 }
13171313 }
13181314
1315+ /**
1316+ * Create a map with the required cookie header.
1317+ * @return A singleton map containing the header.
1318+ */
1319+ public static Map <String , List <String >> getCookieHeader () {
1320+ return Collections .singletonMap ("Cookie" , singletonList (generateConsentCookie ()));
1321+ }
1322+
13191323 /**
13201324 * Add the <code>CONSENT</code> cookie to prevent redirect to <code>consent.youtube.com</code>
13211325 * @see #CONSENT_COOKIE
0 commit comments