Skip to content

Commit 07b045f

Browse files
committed
[YouTube] Support the iOS client in YoutubeDashManifestCreator and decrypt again the n parameter, if present, for all clients
This commits reverts a new behavior introduced in this branch, which only applied the decryption if needed on streams from the WEB client. Also fix rebase issues and documentations style in YoutubeDashManifestCreator.
1 parent 436ddde commit 07b045f

2 files changed

Lines changed: 25 additions & 33 deletions

File tree

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

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@
3333
import java.util.Objects;
3434

3535
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.addClientInfoHeaders;
36-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getYoutubeAndroidAppUserAgent;
36+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getAndroidUserAgent;
37+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getIosUserAgent;
3738
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isAndroidStreamingUrl;
39+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isIosStreamingUrl;
40+
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isTvHtml5SimplyEmbeddedPlayerStreamingUrl;
3841
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isWebStreamingUrl;
3942
import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;
4043
import static org.schabi.newpipe.extractor.utils.Utils.isBlank;
@@ -562,54 +565,44 @@ private static Response getInitializationResponse(@Nonnull String baseStreamingU
562565
@Nonnull final ItagItem itagItem,
563566
final DeliveryType deliveryType)
564567
throws YoutubeDashManifestCreationException {
565-
final boolean isAWebStreamingUrl = isWebStreamingUrl(baseStreamingUrl);
568+
final boolean isAHtml5StreamingUrl = isWebStreamingUrl(baseStreamingUrl)
569+
|| isTvHtml5SimplyEmbeddedPlayerStreamingUrl(baseStreamingUrl);
566570
final boolean isAnAndroidStreamingUrl = isAndroidStreamingUrl(baseStreamingUrl);
567-
final boolean isAnAndroidStreamingUrlAndAPostLiveDvrStream = isAnAndroidStreamingUrl
568-
&& deliveryType == DeliveryType.LIVE;
569-
if (isAWebStreamingUrl) {
571+
final boolean isAnIosStreamingUrl = isIosStreamingUrl(baseStreamingUrl);
572+
if (isAHtml5StreamingUrl) {
570573
baseStreamingUrl += ALR_YES;
571574
}
572575
baseStreamingUrl = appendRnParamAndSqParamIfNeeded(baseStreamingUrl, deliveryType);
573576

574577
final Downloader downloader = NewPipe.getDownloader();
575-
if (isAWebStreamingUrl) {
578+
if (isAHtml5StreamingUrl) {
576579
final String mimeTypeExpected = itagItem.getMediaFormat().getMimeType();
577580
if (!isNullOrEmpty(mimeTypeExpected)) {
578581
return getStreamingWebUrlWithoutRedirects(downloader, baseStreamingUrl,
579582
mimeTypeExpected, deliveryType);
580583
}
581-
} else if (isAnAndroidStreamingUrlAndAPostLiveDvrStream) {
584+
} else if (isAnAndroidStreamingUrl || isAnIosStreamingUrl) {
582585
try {
583586
final Map<String, List<String>> headers = new HashMap<>();
584587
headers.put("User-Agent", Collections.singletonList(
585-
getYoutubeAndroidAppUserAgent(null)));
588+
isAnAndroidStreamingUrl ? getAndroidUserAgent(null)
589+
: getIosUserAgent(null)));
586590
final byte[] emptyBody = "".getBytes(StandardCharsets.UTF_8);
587591
return downloader.post(baseStreamingUrl, headers, emptyBody);
588592
} catch (final IOException | ExtractionException e) {
589593
throw new YoutubeDashManifestCreationException(
590594
"Could not generate the DASH manifest: error when trying to get the "
591-
+ "ANDROID streaming post-live-DVR URL response", e);
595+
+ (isAnIosStreamingUrl ? "ANDROID" : "IOS")
596+
+ " streaming URL response", e);
592597
}
593598
}
594599

595600
try {
596-
final Map<String, List<String>> headers = new HashMap<>();
597-
if (isAnAndroidStreamingUrl) {
598-
headers.put("User-Agent", Collections.singletonList(
599-
getYoutubeAndroidAppUserAgent(null)));
600-
}
601-
602-
return downloader.get(baseStreamingUrl, headers);
601+
return downloader.get(baseStreamingUrl);
603602
} catch (final IOException | ExtractionException e) {
604-
if (isAnAndroidStreamingUrl) {
605603
throw new YoutubeDashManifestCreationException(
606-
"Could not generate the DASH manifest: error when trying to get the "
607-
+ "ANDROID streaming URL response", e);
608-
} else {
609-
throw new YoutubeDashManifestCreationException(
610-
"Could not generate the DASH manifest: error when trying to get the "
611-
+ "streaming URL response", e);
612-
}
604+
"Could not generate the DASH manifest: error when trying to get the streaming "
605+
+ "URL response", e);
613606
}
614607
}
615608

@@ -834,8 +827,10 @@ private static int getStreamDuration(@Nonnull final String[] segmentDuration)
834827
* sequence of the stream
835828
* @param deliveryType the {@link DeliveryType} of the stream, see the enum for
836829
* possible values
830+
* @param itagItem the {@link ItagItem} which will be used to get the duration
831+
* of progressive streams
837832
* @param durationSecondsFallback the duration in seconds, extracted from player response, used
838-
* as a fallback
833+
* as a fallback if the duration could not be determined
839834
* @return a {@link Document} object which contains a {@code <MPD>} element
840835
* @throws YoutubeDashManifestCreationException if something goes wrong when generating/
841836
* appending the {@link Document object} or the
@@ -1698,7 +1693,7 @@ public static double getProgressiveStreamsClearFactor() {
16981693
}
16991694

17001695
/**
1701-
* Set the clear factor of cached OTF streams
1696+
* Set the clear factor of cached OTF streams.
17021697
*
17031698
* @param otfStreamsClearFactor the clear factor of OTF streams manifests cache.
17041699
*/
@@ -1707,7 +1702,7 @@ public static void setOtfStreamsClearFactor(final double otfStreamsClearFactor)
17071702
}
17081703

17091704
/**
1710-
* Set the clear factor of cached post-live-DVR streams
1705+
* Set the clear factor of cached post-live-DVR streams.
17111706
*
17121707
* @param postLiveDvrStreamsClearFactor the clear factor of post-live-DVR streams manifests
17131708
* cache.
@@ -1718,7 +1713,7 @@ public static void setPostLiveDvrStreamsClearFactor(
17181713
}
17191714

17201715
/**
1721-
* Set the clear factor of cached progressive streams
1716+
* Set the clear factor of cached progressive streams.
17221717
*
17231718
* @param progressiveStreamsClearFactor the clear factor of progressive streams manifests
17241719
* cache.

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@
2929
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.fixThumbnailUrl;
3030
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.generateContentPlaybackNonce;
3131
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.generateTParameter;
32-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getClientVersion;
3332
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonAndroidPostResponse;
3433
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonIosPostResponse;
3534
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonPostResponse;
3635
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
37-
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isWebStreamingUrl;
3836
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareAndroidMobileJsonBuilder;
3937
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder;
4038
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareIosMobileJsonBuilder;
@@ -1366,9 +1364,8 @@ private void buildAndAddItagInfoToList(
13661364
// Add the content playback nonce to the stream URL
13671365
streamUrl += "&" + CPN + "=" + contentPlaybackNonce;
13681366

1369-
if (isWebStreamingUrl(streamUrl)) {
1370-
streamUrl = tryDecryptUrl(streamUrl, videoId) + "&cver=" + getClientVersion();
1371-
}
1367+
// Decrypt the n parameter if it is present
1368+
streamUrl = tryDecryptUrl(streamUrl, videoId);
13721369

13731370
final JsonObject initRange = formatData.getObject("initRange");
13741371
final JsonObject indexRange = formatData.getObject("indexRange");

0 commit comments

Comments
 (0)