[YouTube] Fix usage of WEB client headers for all HTML5 URLs in DASH creators

Also use TVHTML5 user agent for requests from this client in these
DASH manifests creators.
This commit is contained in:
AudricV 2025-01-31 21:46:40 +01:00 committed by Stypox
parent c48d449853
commit 96911ae2a4
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23

View File

@ -1,8 +1,9 @@
package org.schabi.newpipe.extractor.services.youtube.dashmanifestcreators; package org.schabi.newpipe.extractor.services.youtube.dashmanifestcreators;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getAndroidUserAgent; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getAndroidUserAgent;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getClientInfoHeaders;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getIosUserAgent; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getIosUserAgent;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getOriginReferrerHeaders;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTvHtml5UserAgent;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isAndroidStreamingUrl; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isAndroidStreamingUrl;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isIosStreamingUrl; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isIosStreamingUrl;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isTvHtml5StreamingUrl; import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isTvHtml5StreamingUrl;
@ -27,6 +28,7 @@ import org.w3c.dom.Element;
import java.io.IOException; import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
@ -602,8 +604,9 @@ public final class YoutubeDashManifestCreatorsUtils {
@Nonnull final ItagItem itagItem, @Nonnull final ItagItem itagItem,
final DeliveryType deliveryType) final DeliveryType deliveryType)
throws CreationException { throws CreationException {
final boolean isTvHtml5StreamingUrl = isTvHtml5StreamingUrl(baseStreamingUrl);
final boolean isHtml5StreamingUrl = isWebStreamingUrl(baseStreamingUrl) final boolean isHtml5StreamingUrl = isWebStreamingUrl(baseStreamingUrl)
|| isTvHtml5StreamingUrl(baseStreamingUrl) || isTvHtml5StreamingUrl
|| isWebEmbeddedPlayerStreamingUrl(baseStreamingUrl); || isWebEmbeddedPlayerStreamingUrl(baseStreamingUrl);
final boolean isAndroidStreamingUrl = isAndroidStreamingUrl(baseStreamingUrl); final boolean isAndroidStreamingUrl = isAndroidStreamingUrl(baseStreamingUrl);
final boolean isIosStreamingUrl = isIosStreamingUrl(baseStreamingUrl); final boolean isIosStreamingUrl = isIosStreamingUrl(baseStreamingUrl);
@ -617,7 +620,7 @@ public final class YoutubeDashManifestCreatorsUtils {
final String mimeTypeExpected = itagItem.getMediaFormat().getMimeType(); final String mimeTypeExpected = itagItem.getMediaFormat().getMimeType();
if (!isNullOrEmpty(mimeTypeExpected)) { if (!isNullOrEmpty(mimeTypeExpected)) {
return getStreamingWebUrlWithoutRedirects(downloader, baseStreamingUrl, return getStreamingWebUrlWithoutRedirects(downloader, baseStreamingUrl,
mimeTypeExpected); mimeTypeExpected, isTvHtml5StreamingUrl);
} }
} else if (isAndroidStreamingUrl || isIosStreamingUrl) { } else if (isAndroidStreamingUrl || isIosStreamingUrl) {
try { try {
@ -732,6 +735,8 @@ public final class YoutubeDashManifestCreatorsUtils {
* @param downloader the {@link Downloader} instance to be used * @param downloader the {@link Downloader} instance to be used
* @param streamingUrl the streaming URL which we are trying to get a streaming URL * @param streamingUrl the streaming URL which we are trying to get a streaming URL
* without any redirection on the network and/or IP used * without any redirection on the network and/or IP used
* @param isTvHtml5StreamingUrl whether the streaming URL comes from TVHTML5 client, in
* order to use an appropriate HTTP User-Agent header
* @param responseMimeTypeExpected the response mime type expected from Google video servers * @param responseMimeTypeExpected the response mime type expected from Google video servers
* @return the {@link Response} of the stream, which should have no redirections * @return the {@link Response} of the stream, which should have no redirections
*/ */
@ -740,10 +745,15 @@ public final class YoutubeDashManifestCreatorsUtils {
private static Response getStreamingWebUrlWithoutRedirects( private static Response getStreamingWebUrlWithoutRedirects(
@Nonnull final Downloader downloader, @Nonnull final Downloader downloader,
@Nonnull String streamingUrl, @Nonnull String streamingUrl,
@Nonnull final String responseMimeTypeExpected) @Nonnull final String responseMimeTypeExpected,
final boolean isTvHtml5StreamingUrl)
throws CreationException { throws CreationException {
try { try {
final var headers = getClientInfoHeaders(); final var headers = new HashMap<>(
getOriginReferrerHeaders("https://www.youtube.com"));
if (isTvHtml5StreamingUrl) {
headers.put("User-Agent", List.of(getTvHtml5UserAgent()));
}
String responseMimeType = ""; String responseMimeType = "";