Skip to content

Commit ff60e05

Browse files
Use Collections.singletonMap().
1 parent 8c5f014 commit ff60e05

File tree

5 files changed

+48
-57
lines changed

5 files changed

+48
-57
lines changed

extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Request.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public Request(final String httpMethod,
4949
actualHeaders.putAll(headers);
5050
}
5151
if (automaticLocalizationHeader && localization != null) {
52-
actualHeaders.putAll(headersFromLocalization(localization));
52+
actualHeaders.putAll(getHeadersFromLocalization(localization));
5353
}
5454

5555
this.headers = Collections.unmodifiableMap(actualHeaders);
@@ -98,7 +98,7 @@ public byte[] dataToSend() {
9898
* A localization object that should be used when executing a request.<br>
9999
* <br>
100100
* Usually the {@code Accept-Language} will be set to this value (a helper
101-
* method to do this easily: {@link Request#headersFromLocalization(Localization)}).
101+
* method to do this easily: {@link Request#getHeadersFromLocalization(Localization)}).
102102
*/
103103
@Nullable
104104
public Localization localization() {
@@ -165,7 +165,7 @@ public Builder dataToSend(final byte[] dataToSendToSet) {
165165
* A localization object that should be used when executing a request.<br>
166166
* <br>
167167
* Usually the {@code Accept-Language} will be set to this value (a helper
168-
* method to do this easily: {@link Request#headersFromLocalization(Localization)}).
168+
* method to do this easily: {@link Request#getHeadersFromLocalization(Localization)}).
169169
*/
170170
public Builder localization(final Localization localizationToSet) {
171171
this.localization = localizationToSet;
@@ -245,23 +245,17 @@ public Builder addHeader(final String headerName, final String headerValue) {
245245

246246
@SuppressWarnings("WeakerAccess")
247247
@Nonnull
248-
public static Map<String, List<String>> headersFromLocalization(
248+
public static Map<String, List<String>> getHeadersFromLocalization(
249249
@Nullable final Localization localization) {
250250
if (localization == null) {
251251
return Collections.emptyMap();
252252
}
253253

254-
final Map<String, List<String>> headers = new LinkedHashMap<>();
255-
if (!localization.getCountryCode().isEmpty()) {
256-
headers.put("Accept-Language",
257-
Collections.singletonList(localization.getLocalizationCode()
258-
+ ", " + localization.getLanguageCode() + ";q=0.9"));
259-
} else {
260-
headers.put("Accept-Language",
261-
Collections.singletonList(localization.getLanguageCode()));
262-
}
263-
264-
return headers;
254+
final String languageCode = localization.getLanguageCode();
255+
final List<String> languageCodeList = Collections.singletonList(
256+
localization.getCountryCode().isEmpty() ? languageCode
257+
: localization.getLocalizationCode() + ", " + languageCode + ";q=0.9");
258+
return Collections.singletonMap("Accept-Language", languageCodeList);
265259
}
266260

267261
/*//////////////////////////////////////////////////////////////////////////

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
66
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
77
import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps;
8-
import static java.util.Collections.singletonList;
98

109
import com.grack.nanojson.JsonArray;
1110
import com.grack.nanojson.JsonObject;
@@ -39,8 +38,8 @@
3938
import java.time.format.DateTimeFormatter;
4039
import java.time.format.DateTimeParseException;
4140
import java.util.Collections;
42-
import java.util.HashMap;
4341
import java.util.List;
42+
import java.util.Map;
4443

4544
import javax.annotation.Nonnull;
4645

@@ -68,8 +67,8 @@ public static synchronized String clientId() throws ExtractionException, IOExcep
6867
// The one containing the client id will likely be the last one
6968
Collections.reverse(possibleScripts);
7069

71-
final HashMap<String, List<String>> headers = new HashMap<>();
72-
headers.put("Range", singletonList("bytes=0-50000"));
70+
final Map<String, List<String>> headers = Collections.singletonMap("Range",
71+
Collections.singletonList("bytes=0-50000"));
7372

7473
for (final Element element : possibleScripts) {
7574
final String srcUrl = element.attr("src");

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import static org.schabi.newpipe.extractor.utils.Utils.getStringResultFromRegexArray;
2929
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
3030

31+
import static java.util.Collections.singletonList;
32+
3133
import com.grack.nanojson.JsonArray;
3234
import com.grack.nanojson.JsonBuilder;
3335
import 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

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/dashmanifestcreators/YoutubeDashManifestCreatorsUtils.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -584,10 +584,9 @@ public static Response getInitializationResponse(@Nonnull String baseStreamingUr
584584
}
585585
} else if (isAndroidStreamingUrl || isIosStreamingUrl) {
586586
try {
587-
final Map<String, List<String>> headers = new HashMap<>();
588-
headers.put("User-Agent", Collections.singletonList(
589-
isAndroidStreamingUrl ? getAndroidUserAgent(null)
590-
: getIosUserAgent(null)));
587+
final Map<String, List<String>> headers = Collections.singletonMap("User-Agent",
588+
Collections.singletonList(isAndroidStreamingUrl
589+
? getAndroidUserAgent(null) : getIosUserAgent(null)));
591590
final byte[] emptyBody = "".getBytes(StandardCharsets.UTF_8);
592591
return downloader.post(baseStreamingUrl, headers, emptyBody);
593592
} catch (final IOException | ExtractionException e) {

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSuggestionExtractor.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.schabi.newpipe.extractor.services.youtube.extractors;
22

3-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.addCookieHeader;
3+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getCookieHeader;
44
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
55

66
import com.grack.nanojson.JsonArray;
@@ -17,9 +17,7 @@
1717
import java.io.IOException;
1818
import java.net.URLEncoder;
1919
import java.util.ArrayList;
20-
import java.util.HashMap;
2120
import java.util.List;
22-
import java.util.Map;
2321

2422
/*
2523
* Created by Christian Schabesberger on 28.09.16.
@@ -59,10 +57,7 @@ public List<String> suggestionList(final String query) throws IOException, Extra
5957
+ "&gl=" + URLEncoder.encode(getExtractorContentCountry().getCountryCode(), UTF_8)
6058
+ "&q=" + URLEncoder.encode(query, UTF_8);
6159

62-
final Map<String, List<String>> headers = new HashMap<>();
63-
addCookieHeader(headers);
64-
65-
String response = dl.get(url, headers, getExtractorLocalization()).responseBody();
60+
String response = dl.get(url, getCookieHeader(), getExtractorLocalization()).responseBody();
6661
// trim JSONP part "JP(...)"
6762
response = response.substring(3, response.length() - 1);
6863
try {

0 commit comments

Comments
 (0)