mirror of
https://github.com/TeamNewPipe/NewPipeExtractor.git
synced 2025-04-28 16:00:33 +05:30
Use Collections.singletonMap().
This commit is contained in:
parent
8c5f014a6f
commit
ff60e05c76
@ -49,7 +49,7 @@ public class Request {
|
|||||||
actualHeaders.putAll(headers);
|
actualHeaders.putAll(headers);
|
||||||
}
|
}
|
||||||
if (automaticLocalizationHeader && localization != null) {
|
if (automaticLocalizationHeader && localization != null) {
|
||||||
actualHeaders.putAll(headersFromLocalization(localization));
|
actualHeaders.putAll(getHeadersFromLocalization(localization));
|
||||||
}
|
}
|
||||||
|
|
||||||
this.headers = Collections.unmodifiableMap(actualHeaders);
|
this.headers = Collections.unmodifiableMap(actualHeaders);
|
||||||
@ -98,7 +98,7 @@ public class Request {
|
|||||||
* A localization object that should be used when executing a request.<br>
|
* A localization object that should be used when executing a request.<br>
|
||||||
* <br>
|
* <br>
|
||||||
* Usually the {@code Accept-Language} will be set to this value (a helper
|
* Usually the {@code Accept-Language} will be set to this value (a helper
|
||||||
* method to do this easily: {@link Request#headersFromLocalization(Localization)}).
|
* method to do this easily: {@link Request#getHeadersFromLocalization(Localization)}).
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
public Localization localization() {
|
public Localization localization() {
|
||||||
@ -165,7 +165,7 @@ public class Request {
|
|||||||
* A localization object that should be used when executing a request.<br>
|
* A localization object that should be used when executing a request.<br>
|
||||||
* <br>
|
* <br>
|
||||||
* Usually the {@code Accept-Language} will be set to this value (a helper
|
* Usually the {@code Accept-Language} will be set to this value (a helper
|
||||||
* method to do this easily: {@link Request#headersFromLocalization(Localization)}).
|
* method to do this easily: {@link Request#getHeadersFromLocalization(Localization)}).
|
||||||
*/
|
*/
|
||||||
public Builder localization(final Localization localizationToSet) {
|
public Builder localization(final Localization localizationToSet) {
|
||||||
this.localization = localizationToSet;
|
this.localization = localizationToSet;
|
||||||
@ -245,23 +245,17 @@ public class Request {
|
|||||||
|
|
||||||
@SuppressWarnings("WeakerAccess")
|
@SuppressWarnings("WeakerAccess")
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static Map<String, List<String>> headersFromLocalization(
|
public static Map<String, List<String>> getHeadersFromLocalization(
|
||||||
@Nullable final Localization localization) {
|
@Nullable final Localization localization) {
|
||||||
if (localization == null) {
|
if (localization == null) {
|
||||||
return Collections.emptyMap();
|
return Collections.emptyMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
final Map<String, List<String>> headers = new LinkedHashMap<>();
|
final String languageCode = localization.getLanguageCode();
|
||||||
if (!localization.getCountryCode().isEmpty()) {
|
final List<String> languageCodeList = Collections.singletonList(
|
||||||
headers.put("Accept-Language",
|
localization.getCountryCode().isEmpty() ? languageCode
|
||||||
Collections.singletonList(localization.getLocalizationCode()
|
: localization.getLocalizationCode() + ", " + languageCode + ";q=0.9");
|
||||||
+ ", " + localization.getLanguageCode() + ";q=0.9"));
|
return Collections.singletonMap("Accept-Language", languageCodeList);
|
||||||
} else {
|
|
||||||
headers.put("Accept-Language",
|
|
||||||
Collections.singletonList(localization.getLanguageCode()));
|
|
||||||
}
|
|
||||||
|
|
||||||
return headers;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*//////////////////////////////////////////////////////////////////////////
|
/*//////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -5,7 +5,6 @@ import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;
|
|||||||
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
|
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
|
||||||
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
|
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
|
||||||
import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps;
|
import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps;
|
||||||
import static java.util.Collections.singletonList;
|
|
||||||
|
|
||||||
import com.grack.nanojson.JsonArray;
|
import com.grack.nanojson.JsonArray;
|
||||||
import com.grack.nanojson.JsonObject;
|
import com.grack.nanojson.JsonObject;
|
||||||
@ -39,8 +38,8 @@ import java.time.OffsetDateTime;
|
|||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.time.format.DateTimeParseException;
|
import java.time.format.DateTimeParseException;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
@ -68,8 +67,8 @@ public final class SoundcloudParsingHelper {
|
|||||||
// The one containing the client id will likely be the last one
|
// The one containing the client id will likely be the last one
|
||||||
Collections.reverse(possibleScripts);
|
Collections.reverse(possibleScripts);
|
||||||
|
|
||||||
final HashMap<String, List<String>> headers = new HashMap<>();
|
final Map<String, List<String>> headers = Collections.singletonMap("Range",
|
||||||
headers.put("Range", singletonList("bytes=0-50000"));
|
Collections.singletonList("bytes=0-50000"));
|
||||||
|
|
||||||
for (final Element element : possibleScripts) {
|
for (final Element element : possibleScripts) {
|
||||||
final String srcUrl = element.attr("src");
|
final String srcUrl = element.attr("src");
|
||||||
|
@ -28,6 +28,8 @@ import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
|
|||||||
import static org.schabi.newpipe.extractor.utils.Utils.getStringResultFromRegexArray;
|
import static org.schabi.newpipe.extractor.utils.Utils.getStringResultFromRegexArray;
|
||||||
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
|
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
|
||||||
|
|
||||||
|
import static java.util.Collections.singletonList;
|
||||||
|
|
||||||
import com.grack.nanojson.JsonArray;
|
import com.grack.nanojson.JsonArray;
|
||||||
import com.grack.nanojson.JsonBuilder;
|
import com.grack.nanojson.JsonBuilder;
|
||||||
import com.grack.nanojson.JsonObject;
|
import com.grack.nanojson.JsonObject;
|
||||||
@ -595,9 +597,9 @@ public final class YoutubeParsingHelper {
|
|||||||
// @formatter:on
|
// @formatter:on
|
||||||
|
|
||||||
final Map<String, List<String>> headers = new HashMap<>();
|
final Map<String, List<String>> headers = new HashMap<>();
|
||||||
headers.put("X-YouTube-Client-Name", Collections.singletonList("1"));
|
headers.put("X-YouTube-Client-Name", singletonList("1"));
|
||||||
headers.put("X-YouTube-Client-Version",
|
headers.put("X-YouTube-Client-Version",
|
||||||
Collections.singletonList(HARDCODED_CLIENT_VERSION));
|
singletonList(HARDCODED_CLIENT_VERSION));
|
||||||
|
|
||||||
// This endpoint is fetched by the YouTube website to get the items of its main menu and is
|
// This endpoint is fetched by the YouTube website to get the items of its main menu and is
|
||||||
// pretty lightweight (around 30kB)
|
// pretty lightweight (around 30kB)
|
||||||
@ -619,8 +621,8 @@ public final class YoutubeParsingHelper {
|
|||||||
}
|
}
|
||||||
final String url = "https://www.youtube.com/sw.js";
|
final String url = "https://www.youtube.com/sw.js";
|
||||||
final Map<String, List<String>> headers = new HashMap<>();
|
final Map<String, List<String>> headers = new HashMap<>();
|
||||||
headers.put("Origin", Collections.singletonList("https://www.youtube.com"));
|
headers.put("Origin", singletonList("https://www.youtube.com"));
|
||||||
headers.put("Referer", Collections.singletonList("https://www.youtube.com"));
|
headers.put("Referer", singletonList("https://www.youtube.com"));
|
||||||
final String response = getDownloader().get(url, headers).responseBody();
|
final String response = getDownloader().get(url, headers).responseBody();
|
||||||
try {
|
try {
|
||||||
clientVersion = getStringResultFromRegexArray(response,
|
clientVersion = getStringResultFromRegexArray(response,
|
||||||
@ -641,9 +643,7 @@ public final class YoutubeParsingHelper {
|
|||||||
}
|
}
|
||||||
// Don't provide a search term in order to have a smaller response
|
// Don't provide a search term in order to have a smaller response
|
||||||
final String url = "https://www.youtube.com/results?search_query=&ucbcb=1";
|
final String url = "https://www.youtube.com/results?search_query=&ucbcb=1";
|
||||||
final Map<String, List<String>> headers = new HashMap<>();
|
final String html = getDownloader().get(url, getCookieHeader()).responseBody();
|
||||||
addCookieHeader(headers);
|
|
||||||
final String html = getDownloader().get(url, headers).responseBody();
|
|
||||||
final JsonObject initialData = getInitialData(html);
|
final JsonObject initialData = getInitialData(html);
|
||||||
final JsonArray serviceTrackingParams = initialData.getObject("responseContext")
|
final JsonArray serviceTrackingParams = initialData.getObject("responseContext")
|
||||||
.getArray("serviceTrackingParams");
|
.getArray("serviceTrackingParams");
|
||||||
@ -821,13 +821,13 @@ public final class YoutubeParsingHelper {
|
|||||||
// @formatter:on
|
// @formatter:on
|
||||||
|
|
||||||
final Map<String, List<String>> headers = new HashMap<>();
|
final Map<String, List<String>> headers = new HashMap<>();
|
||||||
headers.put("X-YouTube-Client-Name", Collections.singletonList(
|
headers.put("X-YouTube-Client-Name", singletonList(
|
||||||
HARDCODED_YOUTUBE_MUSIC_KEY[1]));
|
HARDCODED_YOUTUBE_MUSIC_KEY[1]));
|
||||||
headers.put("X-YouTube-Client-Version", Collections.singletonList(
|
headers.put("X-YouTube-Client-Version", singletonList(
|
||||||
HARDCODED_YOUTUBE_MUSIC_KEY[2]));
|
HARDCODED_YOUTUBE_MUSIC_KEY[2]));
|
||||||
headers.put("Origin", Collections.singletonList("https://music.youtube.com"));
|
headers.put("Origin", singletonList("https://music.youtube.com"));
|
||||||
headers.put("Referer", Collections.singletonList("music.youtube.com"));
|
headers.put("Referer", singletonList("music.youtube.com"));
|
||||||
headers.put("Content-Type", Collections.singletonList("application/json"));
|
headers.put("Content-Type", singletonList("application/json"));
|
||||||
|
|
||||||
final Response response = getDownloader().post(url, headers, json);
|
final Response response = getDownloader().post(url, headers, json);
|
||||||
// Ensure to have a valid response
|
// Ensure to have a valid response
|
||||||
@ -851,8 +851,8 @@ public final class YoutubeParsingHelper {
|
|||||||
try {
|
try {
|
||||||
final String url = "https://music.youtube.com/sw.js";
|
final String url = "https://music.youtube.com/sw.js";
|
||||||
final Map<String, List<String>> headers = new HashMap<>();
|
final Map<String, List<String>> headers = new HashMap<>();
|
||||||
headers.put("Origin", Collections.singletonList("https://music.youtube.com"));
|
headers.put("Origin", singletonList("https://music.youtube.com"));
|
||||||
headers.put("Referer", Collections.singletonList("https://music.youtube.com"));
|
headers.put("Referer", singletonList("https://music.youtube.com"));
|
||||||
final String response = getDownloader().get(url, headers).responseBody();
|
final String response = getDownloader().get(url, headers).responseBody();
|
||||||
musicClientVersion = getStringResultFromRegexArray(response,
|
musicClientVersion = getStringResultFromRegexArray(response,
|
||||||
INNERTUBE_CONTEXT_CLIENT_VERSION_REGEXES, 1);
|
INNERTUBE_CONTEXT_CLIENT_VERSION_REGEXES, 1);
|
||||||
@ -860,9 +860,7 @@ public final class YoutubeParsingHelper {
|
|||||||
musicClientName = Parser.matchGroup1(INNERTUBE_CLIENT_NAME_REGEX, response);
|
musicClientName = Parser.matchGroup1(INNERTUBE_CLIENT_NAME_REGEX, response);
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
final String url = "https://music.youtube.com/?ucbcb=1";
|
final String url = "https://music.youtube.com/?ucbcb=1";
|
||||||
final Map<String, List<String>> headers = new HashMap<>();
|
final String html = getDownloader().get(url, getCookieHeader()).responseBody();
|
||||||
addCookieHeader(headers);
|
|
||||||
final String html = getDownloader().get(url, headers).responseBody();
|
|
||||||
|
|
||||||
musicKey = getStringResultFromRegexArray(html, INNERTUBE_API_KEY_REGEXES, 1);
|
musicKey = getStringResultFromRegexArray(html, INNERTUBE_API_KEY_REGEXES, 1);
|
||||||
musicClientVersion = getStringResultFromRegexArray(html,
|
musicClientVersion = getStringResultFromRegexArray(html,
|
||||||
@ -1066,7 +1064,7 @@ public final class YoutubeParsingHelper {
|
|||||||
throws IOException, ExtractionException {
|
throws IOException, ExtractionException {
|
||||||
final Map<String, List<String>> headers = new HashMap<>();
|
final Map<String, List<String>> headers = new HashMap<>();
|
||||||
addClientInfoHeaders(headers);
|
addClientInfoHeaders(headers);
|
||||||
headers.put("Content-Type", Collections.singletonList("application/json"));
|
headers.put("Content-Type", singletonList("application/json"));
|
||||||
|
|
||||||
final Response response = getDownloader().post(YOUTUBEI_V1_URL + endpoint + "?key="
|
final Response response = getDownloader().post(YOUTUBEI_V1_URL + endpoint + "?key="
|
||||||
+ getKey() + DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization);
|
+ getKey() + DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization);
|
||||||
@ -1100,9 +1098,9 @@ public final class YoutubeParsingHelper {
|
|||||||
@Nonnull final String innerTubeApiKey,
|
@Nonnull final String innerTubeApiKey,
|
||||||
@Nullable final String endPartOfUrlRequest) throws IOException, ExtractionException {
|
@Nullable final String endPartOfUrlRequest) throws IOException, ExtractionException {
|
||||||
final Map<String, List<String>> headers = new HashMap<>();
|
final Map<String, List<String>> headers = new HashMap<>();
|
||||||
headers.put("Content-Type", Collections.singletonList("application/json"));
|
headers.put("Content-Type", singletonList("application/json"));
|
||||||
headers.put("User-Agent", Collections.singletonList(userAgent));
|
headers.put("User-Agent", singletonList(userAgent));
|
||||||
headers.put("X-Goog-Api-Format-Version", Collections.singletonList("2"));
|
headers.put("X-Goog-Api-Format-Version", singletonList("2"));
|
||||||
|
|
||||||
final String baseEndpointUrl = YOUTUBEI_V1_GAPIS_URL + endpoint + "?key=" + innerTubeApiKey
|
final String baseEndpointUrl = YOUTUBEI_V1_GAPIS_URL + endpoint + "?key=" + innerTubeApiKey
|
||||||
+ DISABLE_PRETTY_PRINT_PARAMETER;
|
+ DISABLE_PRETTY_PRINT_PARAMETER;
|
||||||
@ -1306,16 +1304,22 @@ public final class YoutubeParsingHelper {
|
|||||||
*/
|
*/
|
||||||
public static void addClientInfoHeaders(@Nonnull final Map<String, List<String>> headers)
|
public static void addClientInfoHeaders(@Nonnull final Map<String, List<String>> headers)
|
||||||
throws IOException, ExtractionException {
|
throws IOException, ExtractionException {
|
||||||
headers.computeIfAbsent("Origin", k -> Collections.singletonList(
|
headers.computeIfAbsent("Origin", k -> singletonList("https://www.youtube.com"));
|
||||||
"https://www.youtube.com"));
|
headers.computeIfAbsent("Referer", k -> singletonList("https://www.youtube.com"));
|
||||||
headers.computeIfAbsent("Referer", k -> Collections.singletonList(
|
headers.computeIfAbsent("X-YouTube-Client-Name", k -> singletonList("1"));
|
||||||
"https://www.youtube.com"));
|
|
||||||
headers.computeIfAbsent("X-YouTube-Client-Name", k -> Collections.singletonList("1"));
|
|
||||||
if (headers.get("X-YouTube-Client-Version") == null) {
|
if (headers.get("X-YouTube-Client-Version") == null) {
|
||||||
headers.put("X-YouTube-Client-Version", Collections.singletonList(getClientVersion()));
|
headers.put("X-YouTube-Client-Version", singletonList(getClientVersion()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a map with the required cookie header.
|
||||||
|
* @return A singleton map containing the header.
|
||||||
|
*/
|
||||||
|
public static Map<String, List<String>> getCookieHeader() {
|
||||||
|
return Collections.singletonMap("Cookie", singletonList(generateConsentCookie()));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add the <code>CONSENT</code> cookie to prevent redirect to <code>consent.youtube.com</code>
|
* Add the <code>CONSENT</code> cookie to prevent redirect to <code>consent.youtube.com</code>
|
||||||
* @see #CONSENT_COOKIE
|
* @see #CONSENT_COOKIE
|
||||||
|
@ -584,10 +584,9 @@ public final class YoutubeDashManifestCreatorsUtils {
|
|||||||
}
|
}
|
||||||
} else if (isAndroidStreamingUrl || isIosStreamingUrl) {
|
} else if (isAndroidStreamingUrl || isIosStreamingUrl) {
|
||||||
try {
|
try {
|
||||||
final Map<String, List<String>> headers = new HashMap<>();
|
final Map<String, List<String>> headers = Collections.singletonMap("User-Agent",
|
||||||
headers.put("User-Agent", Collections.singletonList(
|
Collections.singletonList(isAndroidStreamingUrl
|
||||||
isAndroidStreamingUrl ? getAndroidUserAgent(null)
|
? getAndroidUserAgent(null) : getIosUserAgent(null)));
|
||||||
: getIosUserAgent(null)));
|
|
||||||
final byte[] emptyBody = "".getBytes(StandardCharsets.UTF_8);
|
final byte[] emptyBody = "".getBytes(StandardCharsets.UTF_8);
|
||||||
return downloader.post(baseStreamingUrl, headers, emptyBody);
|
return downloader.post(baseStreamingUrl, headers, emptyBody);
|
||||||
} catch (final IOException | ExtractionException e) {
|
} catch (final IOException | ExtractionException e) {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package org.schabi.newpipe.extractor.services.youtube.extractors;
|
package org.schabi.newpipe.extractor.services.youtube.extractors;
|
||||||
|
|
||||||
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.addCookieHeader;
|
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getCookieHeader;
|
||||||
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
|
import static org.schabi.newpipe.extractor.utils.Utils.UTF_8;
|
||||||
|
|
||||||
import com.grack.nanojson.JsonArray;
|
import com.grack.nanojson.JsonArray;
|
||||||
@ -17,9 +17,7 @@ import org.schabi.newpipe.extractor.suggestion.SuggestionExtractor;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Created by Christian Schabesberger on 28.09.16.
|
* Created by Christian Schabesberger on 28.09.16.
|
||||||
@ -59,10 +57,7 @@ public class YoutubeSuggestionExtractor extends SuggestionExtractor {
|
|||||||
+ "&gl=" + URLEncoder.encode(getExtractorContentCountry().getCountryCode(), UTF_8)
|
+ "&gl=" + URLEncoder.encode(getExtractorContentCountry().getCountryCode(), UTF_8)
|
||||||
+ "&q=" + URLEncoder.encode(query, UTF_8);
|
+ "&q=" + URLEncoder.encode(query, UTF_8);
|
||||||
|
|
||||||
final Map<String, List<String>> headers = new HashMap<>();
|
String response = dl.get(url, getCookieHeader(), getExtractorLocalization()).responseBody();
|
||||||
addCookieHeader(headers);
|
|
||||||
|
|
||||||
String response = dl.get(url, headers, getExtractorLocalization()).responseBody();
|
|
||||||
// trim JSONP part "JP(...)"
|
// trim JSONP part "JP(...)"
|
||||||
response = response.substring(3, response.length() - 1);
|
response = response.substring(3, response.length() - 1);
|
||||||
try {
|
try {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user