Skip to content

Commit fc8b5eb

Browse files
authored
Merge pull request TeamNewPipe#878 from Isira-Seneviratne/Use_Collections
Use Collections methods.
2 parents 4a4939d + 1af6b8e commit fc8b5eb

File tree

6 files changed

+50
-64
lines changed

6 files changed

+50
-64
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
@@ -42,7 +42,7 @@ public Request(final String httpMethod,
4242
actualHeaders.putAll(headers);
4343
}
4444
if (automaticLocalizationHeader && localization != null) {
45-
actualHeaders.putAll(headersFromLocalization(localization));
45+
actualHeaders.putAll(getHeadersFromLocalization(localization));
4646
}
4747

4848
this.headers = Collections.unmodifiableMap(actualHeaders);
@@ -91,7 +91,7 @@ public byte[] dataToSend() {
9191
* A localization object that should be used when executing a request.<br>
9292
* <br>
9393
* Usually the {@code Accept-Language} will be set to this value (a helper
94-
* method to do this easily: {@link Request#headersFromLocalization(Localization)}).
94+
* method to do this easily: {@link Request#getHeadersFromLocalization(Localization)}).
9595
*/
9696
@Nullable
9797
public Localization localization() {
@@ -158,7 +158,7 @@ public Builder dataToSend(final byte[] dataToSendToSet) {
158158
* A localization object that should be used when executing a request.<br>
159159
* <br>
160160
* Usually the {@code Accept-Language} will be set to this value (a helper
161-
* method to do this easily: {@link Request#headersFromLocalization(Localization)}).
161+
* method to do this easily: {@link Request#getHeadersFromLocalization(Localization)}).
162162
*/
163163
public Builder localization(final Localization localizationToSet) {
164164
this.localization = localizationToSet;
@@ -238,23 +238,17 @@ public Builder addHeader(final String headerName, final String headerValue) {
238238

239239
@SuppressWarnings("WeakerAccess")
240240
@Nonnull
241-
public static Map<String, List<String>> headersFromLocalization(
241+
public static Map<String, List<String>> getHeadersFromLocalization(
242242
@Nullable final Localization localization) {
243243
if (localization == null) {
244244
return Collections.emptyMap();
245245
}
246246

247-
final Map<String, List<String>> headers = new LinkedHashMap<>();
248-
if (!localization.getCountryCode().isEmpty()) {
249-
headers.put("Accept-Language",
250-
Collections.singletonList(localization.getLocalizationCode()
251-
+ ", " + localization.getLanguageCode() + ";q=0.9"));
252-
} else {
253-
headers.put("Accept-Language",
254-
Collections.singletonList(localization.getLanguageCode()));
255-
}
256-
257-
return headers;
247+
final String languageCode = localization.getLanguageCode();
248+
final List<String> languageCodeList = Collections.singletonList(
249+
localization.getCountryCode().isEmpty() ? languageCode
250+
: localization.getLocalizationCode() + ", " + languageCode + ";q=0.9");
251+
return Collections.singletonMap("Accept-Language", languageCodeList);
258252
}
259253

260254
/*//////////////////////////////////////////////////////////////////////////

extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampStreamExtractor.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import javax.annotation.Nonnull;
3232
import javax.annotation.Nullable;
3333
import java.io.IOException;
34-
import java.util.ArrayList;
3534
import java.util.Collections;
3635
import java.util.List;
3736
import java.util.stream.Collectors;
@@ -149,8 +148,7 @@ public Description getDescription() {
149148

150149
@Override
151150
public List<AudioStream> getAudioStreams() {
152-
final List<AudioStream> audioStreams = new ArrayList<>();
153-
audioStreams.add(new AudioStream.Builder()
151+
return Collections.singletonList(new AudioStream.Builder()
154152
.setId("mp3-128")
155153
.setContent(albumJson.getArray("trackinfo")
156154
.getObject(0)
@@ -159,7 +157,6 @@ public List<AudioStream> getAudioStreams() {
159157
.setMediaFormat(MediaFormat.MP3)
160158
.setAverageBitrate(128)
161159
.build());
162-
return audioStreams;
163160
}
164161

165162
@Override

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: 32 additions & 30 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;
@@ -63,7 +65,6 @@
6365
import java.time.ZoneOffset;
6466
import java.time.format.DateTimeParseException;
6567
import java.util.ArrayList;
66-
import java.util.Arrays;
6768
import java.util.Collections;
6869
import java.util.HashMap;
6970
import 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
}

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)