From 81013e5a8e13b78acfa789a935f48f12d9eddb9a Mon Sep 17 00:00:00 2001 From: TiA4f8R <74829229+TiA4f8R@users.noreply.github.com> Date: Sun, 6 Jun 2021 18:32:07 +0200 Subject: [PATCH] Add a static method to reset the YouTube deobfuscation code and use it in tests This method is needed for YouTube stream tests, because when all YouTube tests are ran, the signatureTimestamp is known (the sts string) so a different body than the body present in the mocks is send by the extractor instance. As a result, running all YouTube stream tests with the MockDownloader (like the CI does) will fail if this method is not called before fetching the page of a test. --- .../extractors/YoutubeStreamExtractor.java | 23 ++++++++++++++++++- ...utubeStreamExtractorAgeRestrictedTest.java | 2 ++ ...utubeStreamExtractorControversialTest.java | 2 ++ .../YoutubeStreamExtractorDefaultTest.java | 9 ++++++++ .../YoutubeStreamExtractorLivestreamTest.java | 2 ++ .../YoutubeStreamExtractorUnlistedTest.java | 2 ++ 6 files changed, 39 insertions(+), 1 deletion(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java index 4c2b5a3e1..3aebaee1e 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java @@ -760,7 +760,8 @@ public class YoutubeStreamExtractor extends StreamExtractor { } private void checkPlayabilityStatus(final JsonObject youtubePlayerResponse, - JsonObject playabilityStatus) throws ParsingException { + @Nonnull JsonObject playabilityStatus) + throws ParsingException { String status = playabilityStatus.getString("status"); // If status exist, and is not "OK", throw the specific exception based on error message // or a ContentNotAvailableException with the reason text if it's an unknown reason. @@ -871,6 +872,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { } } + @Nonnull private byte[] createPlayerBodyWithSts(final Localization localization, final ContentCountry contentCountry, final String videoId) throws ExtractionException, @@ -988,6 +990,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { "Could not find deobfuscate function with any of the given patterns.", exception); } + @Nonnull private String loadDeobfuscationCode() throws DeobfuscateException { try { final String deobfuscationFunctionName = getDeobfuscationFuncName(playerCode); @@ -1115,6 +1118,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { "&sts=" + sts + "&ps=default&gl=US&hl=en"; } + @Nonnull private Map getItags(final String streamingDataKey, final ItagItem.ItagType itagTypeWanted) throws ParsingException { @@ -1371,4 +1375,21 @@ public class YoutubeStreamExtractor extends StreamExtractor { nextResponse.getObject("contents").getObject("twoColumnWatchNextResults") .getObject("results").getObject("results").getArray("contents")); } + + /** + * Reset YouTube deobfuscation code. + *

+ * This is needed for mocks in YouTube stream tests, because when they are ran, the + * {@code signatureTimestamp} is known (the {@code sts} string) so a different body than the + * body present in the mocks is send by the extractor instance. As a result, running all + * YouTube stream tests with the MockDownloader (like the CI does) will fail if this method is + * not called before fetching the page of a test. + *

+ */ + public static void resetDeobfuscationCode() { + cachedDeobfuscationCode = null; + playerCode = null; + playerJsUrl = null; + sts = null; + } } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorAgeRestrictedTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorAgeRestrictedTest.java index 180dc173c..8a7b521f7 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorAgeRestrictedTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorAgeRestrictedTest.java @@ -6,6 +6,7 @@ import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.services.DefaultStreamExtractorTest; import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper; +import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor; import org.schabi.newpipe.extractor.stream.StreamExtractor; import org.schabi.newpipe.extractor.stream.StreamType; @@ -28,6 +29,7 @@ public class YoutubeStreamExtractorAgeRestrictedTest extends DefaultStreamExtrac public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); + YoutubeStreamExtractor.resetDeobfuscationCode(); NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "ageRestricted")); extractor = YouTube.getStreamExtractor(URL); extractor.fetchPage(); diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorControversialTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorControversialTest.java index 360b91960..b0f40a779 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorControversialTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorControversialTest.java @@ -7,6 +7,7 @@ import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.services.DefaultStreamExtractorTest; import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper; +import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor; import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeStreamLinkHandlerFactory; import org.schabi.newpipe.extractor.stream.StreamExtractor; import org.schabi.newpipe.extractor.stream.StreamType; @@ -33,6 +34,7 @@ public class YoutubeStreamExtractorControversialTest extends DefaultStreamExtrac public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); + YoutubeStreamExtractor.resetDeobfuscationCode(); NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "controversial")); extractor = YouTube.getStreamExtractor(URL); extractor.fetchPage(); diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorDefaultTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorDefaultTest.java index 1fefc8b85..2ce2a8283 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorDefaultTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorDefaultTest.java @@ -63,6 +63,7 @@ public class YoutubeStreamExtractorDefaultTest { public static void setUp() throws IOException { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); + YoutubeStreamExtractor.resetDeobfuscationCode(); NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "notAvailable")); } @@ -119,6 +120,7 @@ public class YoutubeStreamExtractorDefaultTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); + YoutubeStreamExtractor.resetDeobfuscationCode(); NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "pewdiwpie")); extractor = YouTube.getStreamExtractor(URL); extractor.fetchPage(); @@ -162,6 +164,7 @@ public class YoutubeStreamExtractorDefaultTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); + YoutubeStreamExtractor.resetDeobfuscationCode(); NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "unboxing")); extractor = YouTube.getStreamExtractor(URL); extractor.fetchPage(); @@ -215,6 +218,7 @@ public class YoutubeStreamExtractorDefaultTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); + YoutubeStreamExtractor.resetDeobfuscationCode(); NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "ratingsDisabled")); extractor = YouTube.getStreamExtractor(URL); extractor.fetchPage(); @@ -252,6 +256,7 @@ public class YoutubeStreamExtractorDefaultTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); + YoutubeStreamExtractor.resetDeobfuscationCode(); NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "streamSegmentsTagesschau")); extractor = YouTube.getStreamExtractor(URL); extractor.fetchPage(); @@ -313,6 +318,7 @@ public class YoutubeStreamExtractorDefaultTest { public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); + YoutubeStreamExtractor.resetDeobfuscationCode(); NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "streamSegmentsMaiLab")); extractor = YouTube.getStreamExtractor(URL); extractor.fetchPage(); @@ -383,6 +389,7 @@ public class YoutubeStreamExtractorDefaultTest { YoutubeParsingHelper.setNumberGenerator(new Random(1)); NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "publicBroadcast")); extractor = YouTube.getStreamExtractor(URL); + YoutubeStreamExtractor.resetDeobfuscationCode(); extractor.fetchPage(); } @@ -432,6 +439,7 @@ public class YoutubeStreamExtractorDefaultTest { @BeforeClass public static void setUp() throws Exception { + YoutubeStreamExtractor.resetDeobfuscationCode(); NewPipe.init(DownloaderTestImpl.getInstance()); extractor = (YoutubeStreamExtractor) YouTube .getStreamExtractor("https://www.youtube.com/watch?v=tjz2u2DiveM"); @@ -451,6 +459,7 @@ public class YoutubeStreamExtractorDefaultTest { @BeforeClass public static void setUp() throws Exception { + YoutubeStreamExtractor.resetDeobfuscationCode(); NewPipe.init(DownloaderTestImpl.getInstance()); extractor = YouTube.getStreamExtractor(URL); extractor.fetchPage(); diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorLivestreamTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorLivestreamTest.java index 346872c1e..34b6dc3c1 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorLivestreamTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorLivestreamTest.java @@ -8,6 +8,7 @@ import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.services.DefaultStreamExtractorTest; import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper; +import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor; import org.schabi.newpipe.extractor.stream.StreamExtractor; import org.schabi.newpipe.extractor.stream.StreamType; @@ -30,6 +31,7 @@ public class YoutubeStreamExtractorLivestreamTest extends DefaultStreamExtractor public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); + YoutubeStreamExtractor.resetDeobfuscationCode(); NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "live")); extractor = YouTube.getStreamExtractor(URL); extractor.fetchPage(); diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorUnlistedTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorUnlistedTest.java index 14a5e755f..29c70da7f 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorUnlistedTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorUnlistedTest.java @@ -6,6 +6,7 @@ import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.services.DefaultStreamExtractorTest; import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper; +import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor; import org.schabi.newpipe.extractor.stream.StreamExtractor; import org.schabi.newpipe.extractor.stream.StreamType; @@ -28,6 +29,7 @@ public class YoutubeStreamExtractorUnlistedTest extends DefaultStreamExtractorTe public static void setUp() throws Exception { YoutubeParsingHelper.resetClientVersionAndKey(); YoutubeParsingHelper.setNumberGenerator(new Random(1)); + YoutubeStreamExtractor.resetDeobfuscationCode(); NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "unlisted")); extractor = YouTube.getStreamExtractor(URL); extractor.fetchPage();