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 ;
6365import java .time .ZoneOffset ;
6466import java .time .format .DateTimeParseException ;
6567import java .util .ArrayList ;
66- import java .util .Arrays ;
6768import java .util .Collections ;
6869import java .util .HashMap ;
6970import java .util .List ;
@@ -595,9 +596,9 @@ public static boolean areHardcodedClientVersionAndKeyValid()
595596 // @formatter:on
596597
597598 final Map <String , List <String >> headers = new HashMap <>();
598- headers .put ("X-YouTube-Client-Name" , Collections . singletonList ("1" ));
599+ headers .put ("X-YouTube-Client-Name" , singletonList ("1" ));
599600 headers .put ("X-YouTube-Client-Version" ,
600- Collections . singletonList (HARDCODED_CLIENT_VERSION ));
601+ singletonList (HARDCODED_CLIENT_VERSION ));
601602
602603 // This endpoint is fetched by the YouTube website to get the items of its main menu and is
603604 // pretty lightweight (around 30kB)
@@ -619,8 +620,8 @@ private static void extractClientVersionAndKeyFromSwJs()
619620 }
620621 final String url = "https://www.youtube.com/sw.js" ;
621622 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" ));
623+ headers .put ("Origin" , singletonList ("https://www.youtube.com" ));
624+ headers .put ("Referer" , singletonList ("https://www.youtube.com" ));
624625 final String response = getDownloader ().get (url , headers ).responseBody ();
625626 try {
626627 clientVersion = getStringResultFromRegexArray (response ,
@@ -641,9 +642,7 @@ private static void extractClientVersionAndKeyFromHtmlSearchResultsPage()
641642 }
642643 // Don't provide a search term in order to have a smaller response
643644 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 ();
645+ final String html = getDownloader ().get (url , getCookieHeader ()).responseBody ();
647646 final JsonObject initialData = getInitialData (html );
648647 final JsonArray serviceTrackingParams = initialData .getObject ("responseContext" )
649648 .getArray ("serviceTrackingParams" );
@@ -821,13 +820,13 @@ public static boolean isHardcodedYoutubeMusicKeyValid() throws IOException,
821820 // @formatter:on
822821
823822 final Map <String , List <String >> headers = new HashMap <>();
824- headers .put ("X-YouTube-Client-Name" , Collections . singletonList (
823+ headers .put ("X-YouTube-Client-Name" , singletonList (
825824 HARDCODED_YOUTUBE_MUSIC_KEY [1 ]));
826- headers .put ("X-YouTube-Client-Version" , Collections . singletonList (
825+ headers .put ("X-YouTube-Client-Version" , singletonList (
827826 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" ));
827+ headers .put ("Origin" , singletonList ("https://music.youtube.com" ));
828+ headers .put ("Referer" , singletonList ("music.youtube.com" ));
829+ headers .put ("Content-Type" , singletonList ("application/json" ));
831830
832831 final Response response = getDownloader ().post (url , headers , json );
833832 // Ensure to have a valid response
@@ -851,18 +850,16 @@ public static String[] getYoutubeMusicKey()
851850 try {
852851 final String url = "https://music.youtube.com/sw.js" ;
853852 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" ));
853+ headers .put ("Origin" , singletonList ("https://music.youtube.com" ));
854+ headers .put ("Referer" , singletonList ("https://music.youtube.com" ));
856855 final String response = getDownloader ().get (url , headers ).responseBody ();
857856 musicClientVersion = getStringResultFromRegexArray (response ,
858857 INNERTUBE_CONTEXT_CLIENT_VERSION_REGEXES , 1 );
859858 musicKey = getStringResultFromRegexArray (response , INNERTUBE_API_KEY_REGEXES , 1 );
860859 musicClientName = Parser .matchGroup1 (INNERTUBE_CLIENT_NAME_REGEX , response );
861860 } catch (final Exception e ) {
862861 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 ();
862+ final String html = getDownloader ().get (url , getCookieHeader ()).responseBody ();
866863
867864 musicKey = getStringResultFromRegexArray (html , INNERTUBE_API_KEY_REGEXES , 1 );
868865 musicClientVersion = getStringResultFromRegexArray (html ,
@@ -1066,7 +1063,7 @@ public static JsonObject getJsonPostResponse(final String endpoint,
10661063 throws IOException , ExtractionException {
10671064 final Map <String , List <String >> headers = new HashMap <>();
10681065 addClientInfoHeaders (headers );
1069- headers .put ("Content-Type" , Collections . singletonList ("application/json" ));
1066+ headers .put ("Content-Type" , singletonList ("application/json" ));
10701067
10711068 final Response response = getDownloader ().post (YOUTUBEI_V1_URL + endpoint + "?key="
10721069 + getKey () + DISABLE_PRETTY_PRINT_PARAMETER , headers , body , localization );
@@ -1100,9 +1097,9 @@ private static JsonObject getMobilePostResponse(
11001097 @ Nonnull final String innerTubeApiKey ,
11011098 @ Nullable final String endPartOfUrlRequest ) throws IOException , ExtractionException {
11021099 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" ));
1100+ headers .put ("Content-Type" , singletonList ("application/json" ));
1101+ headers .put ("User-Agent" , singletonList (userAgent ));
1102+ headers .put ("X-Goog-Api-Format-Version" , singletonList ("2" ));
11061103
11071104 final String baseEndpointUrl = YOUTUBEI_V1_GAPIS_URL + endpoint + "?key=" + innerTubeApiKey
11081105 + DISABLE_PRETTY_PRINT_PARAMETER ;
@@ -1306,25 +1303,30 @@ public static void addYouTubeHeaders(final Map<String, List<String>> headers)
13061303 */
13071304 public static void addClientInfoHeaders (@ Nonnull final Map <String , List <String >> headers )
13081305 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" ));
1306+ headers .computeIfAbsent ("Origin" , k -> singletonList ("https://www.youtube.com" ));
1307+ headers .computeIfAbsent ("Referer" , k -> singletonList ("https://www.youtube.com" ));
1308+ headers .computeIfAbsent ("X-YouTube-Client-Name" , k -> singletonList ("1" ));
13141309 if (headers .get ("X-YouTube-Client-Version" ) == null ) {
1315- headers .put ("X-YouTube-Client-Version" , Collections . singletonList (getClientVersion ()));
1310+ headers .put ("X-YouTube-Client-Version" , singletonList (getClientVersion ()));
13161311 }
13171312 }
13181313
1314+ /**
1315+ * Create a map with the required cookie header.
1316+ * @return A singleton map containing the header.
1317+ */
1318+ public static Map <String , List <String >> getCookieHeader () {
1319+ return Collections .singletonMap ("Cookie" , singletonList (generateConsentCookie ()));
1320+ }
1321+
13191322 /**
13201323 * Add the <code>CONSENT</code> cookie to prevent redirect to <code>consent.youtube.com</code>
13211324 * @see #CONSENT_COOKIE
13221325 * @param headers the headers which should be completed
13231326 */
1324- @ SuppressWarnings ("ArraysAsListWithZeroOrOneArgument" )
13251327 public static void addCookieHeader (@ Nonnull final Map <String , List <String >> headers ) {
13261328 if (headers .get ("Cookie" ) == null ) {
1327- headers .put ("Cookie" , Arrays . asList (generateConsentCookie ()));
1329+ headers .put ("Cookie" , Collections . singletonList (generateConsentCookie ()));
13281330 } else {
13291331 headers .get ("Cookie" ).add (generateConsentCookie ());
13301332 }
0 commit comments