Skip to content

Commit 81013e5

Browse files
committed
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.
1 parent cc79852 commit 81013e5

6 files changed

Lines changed: 39 additions & 1 deletion

File tree

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,8 @@ public void onFetchPage(@Nonnull final Downloader downloader)
760760
}
761761

762762
private void checkPlayabilityStatus(final JsonObject youtubePlayerResponse,
763-
JsonObject playabilityStatus) throws ParsingException {
763+
@Nonnull JsonObject playabilityStatus)
764+
throws ParsingException {
764765
String status = playabilityStatus.getString("status");
765766
// If status exist, and is not "OK", throw the specific exception based on error message
766767
// or a ContentNotAvailableException with the reason text if it's an unknown reason.
@@ -871,6 +872,7 @@ private void fetchVideoInfoPage() throws ParsingException, ReCaptchaException, I
871872
}
872873
}
873874

875+
@Nonnull
874876
private byte[] createPlayerBodyWithSts(final Localization localization,
875877
final ContentCountry contentCountry,
876878
final String videoId) throws ExtractionException,
@@ -988,6 +990,7 @@ private String getDeobfuscationFuncName(final String playerCode) throws Deobfusc
988990
"Could not find deobfuscate function with any of the given patterns.", exception);
989991
}
990992

993+
@Nonnull
991994
private String loadDeobfuscationCode() throws DeobfuscateException {
992995
try {
993996
final String deobfuscationFunctionName = getDeobfuscationFuncName(playerCode);
@@ -1115,6 +1118,7 @@ private static String getVideoInfoUrl(final String id, final String sts) {
11151118
"&sts=" + sts + "&ps=default&gl=US&hl=en";
11161119
}
11171120

1121+
@Nonnull
11181122
private Map<String, ItagItem> getItags(final String streamingDataKey,
11191123
final ItagItem.ItagType itagTypeWanted)
11201124
throws ParsingException {
@@ -1371,4 +1375,21 @@ public List<MetaInfo> getMetaInfo() throws ParsingException {
13711375
nextResponse.getObject("contents").getObject("twoColumnWatchNextResults")
13721376
.getObject("results").getObject("results").getArray("contents"));
13731377
}
1378+
1379+
/**
1380+
* Reset YouTube deobfuscation code.
1381+
* <p>
1382+
* This is needed for mocks in YouTube stream tests, because when they are ran, the
1383+
* {@code signatureTimestamp} is known (the {@code sts} string) so a different body than the
1384+
* body present in the mocks is send by the extractor instance. As a result, running all
1385+
* YouTube stream tests with the MockDownloader (like the CI does) will fail if this method is
1386+
* not called before fetching the page of a test.
1387+
* </p>
1388+
*/
1389+
public static void resetDeobfuscationCode() {
1390+
cachedDeobfuscationCode = null;
1391+
playerCode = null;
1392+
playerJsUrl = null;
1393+
sts = null;
1394+
}
13741395
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorAgeRestrictedTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.schabi.newpipe.extractor.StreamingService;
77
import org.schabi.newpipe.extractor.services.DefaultStreamExtractorTest;
88
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
9+
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor;
910
import org.schabi.newpipe.extractor.stream.StreamExtractor;
1011
import org.schabi.newpipe.extractor.stream.StreamType;
1112

@@ -28,6 +29,7 @@ public class YoutubeStreamExtractorAgeRestrictedTest extends DefaultStreamExtrac
2829
public static void setUp() throws Exception {
2930
YoutubeParsingHelper.resetClientVersionAndKey();
3031
YoutubeParsingHelper.setNumberGenerator(new Random(1));
32+
YoutubeStreamExtractor.resetDeobfuscationCode();
3133
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "ageRestricted"));
3234
extractor = YouTube.getStreamExtractor(URL);
3335
extractor.fetchPage();

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorControversialTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.schabi.newpipe.extractor.StreamingService;
88
import org.schabi.newpipe.extractor.services.DefaultStreamExtractorTest;
99
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
10+
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor;
1011
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeStreamLinkHandlerFactory;
1112
import org.schabi.newpipe.extractor.stream.StreamExtractor;
1213
import org.schabi.newpipe.extractor.stream.StreamType;
@@ -33,6 +34,7 @@ public class YoutubeStreamExtractorControversialTest extends DefaultStreamExtrac
3334
public static void setUp() throws Exception {
3435
YoutubeParsingHelper.resetClientVersionAndKey();
3536
YoutubeParsingHelper.setNumberGenerator(new Random(1));
37+
YoutubeStreamExtractor.resetDeobfuscationCode();
3638
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "controversial"));
3739
extractor = YouTube.getStreamExtractor(URL);
3840
extractor.fetchPage();

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorDefaultTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public static class NotAvailable {
6363
public static void setUp() throws IOException {
6464
YoutubeParsingHelper.resetClientVersionAndKey();
6565
YoutubeParsingHelper.setNumberGenerator(new Random(1));
66+
YoutubeStreamExtractor.resetDeobfuscationCode();
6667
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "notAvailable"));
6768
}
6869

@@ -119,6 +120,7 @@ public static class DescriptionTestPewdiepie extends DefaultStreamExtractorTest
119120
public static void setUp() throws Exception {
120121
YoutubeParsingHelper.resetClientVersionAndKey();
121122
YoutubeParsingHelper.setNumberGenerator(new Random(1));
123+
YoutubeStreamExtractor.resetDeobfuscationCode();
122124
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "pewdiwpie"));
123125
extractor = YouTube.getStreamExtractor(URL);
124126
extractor.fetchPage();
@@ -162,6 +164,7 @@ public static class DescriptionTestUnboxing extends DefaultStreamExtractorTest {
162164
public static void setUp() throws Exception {
163165
YoutubeParsingHelper.resetClientVersionAndKey();
164166
YoutubeParsingHelper.setNumberGenerator(new Random(1));
167+
YoutubeStreamExtractor.resetDeobfuscationCode();
165168
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "unboxing"));
166169
extractor = YouTube.getStreamExtractor(URL);
167170
extractor.fetchPage();
@@ -215,6 +218,7 @@ public static class RatingsDisabledTest extends DefaultStreamExtractorTest {
215218
public static void setUp() throws Exception {
216219
YoutubeParsingHelper.resetClientVersionAndKey();
217220
YoutubeParsingHelper.setNumberGenerator(new Random(1));
221+
YoutubeStreamExtractor.resetDeobfuscationCode();
218222
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "ratingsDisabled"));
219223
extractor = YouTube.getStreamExtractor(URL);
220224
extractor.fetchPage();
@@ -252,6 +256,7 @@ public static class StreamSegmentsTestTagesschau extends DefaultStreamExtractorT
252256
public static void setUp() throws Exception {
253257
YoutubeParsingHelper.resetClientVersionAndKey();
254258
YoutubeParsingHelper.setNumberGenerator(new Random(1));
259+
YoutubeStreamExtractor.resetDeobfuscationCode();
255260
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "streamSegmentsTagesschau"));
256261
extractor = YouTube.getStreamExtractor(URL);
257262
extractor.fetchPage();
@@ -313,6 +318,7 @@ public static class StreamSegmentsTestMaiLab extends DefaultStreamExtractorTest
313318
public static void setUp() throws Exception {
314319
YoutubeParsingHelper.resetClientVersionAndKey();
315320
YoutubeParsingHelper.setNumberGenerator(new Random(1));
321+
YoutubeStreamExtractor.resetDeobfuscationCode();
316322
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "streamSegmentsMaiLab"));
317323
extractor = YouTube.getStreamExtractor(URL);
318324
extractor.fetchPage();
@@ -383,6 +389,7 @@ public static void setUp() throws Exception {
383389
YoutubeParsingHelper.setNumberGenerator(new Random(1));
384390
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "publicBroadcast"));
385391
extractor = YouTube.getStreamExtractor(URL);
392+
YoutubeStreamExtractor.resetDeobfuscationCode();
386393
extractor.fetchPage();
387394
}
388395

@@ -432,6 +439,7 @@ public static class UnlistedTest {
432439

433440
@BeforeClass
434441
public static void setUp() throws Exception {
442+
YoutubeStreamExtractor.resetDeobfuscationCode();
435443
NewPipe.init(DownloaderTestImpl.getInstance());
436444
extractor = (YoutubeStreamExtractor) YouTube
437445
.getStreamExtractor("https://www.youtube.com/watch?v=tjz2u2DiveM");
@@ -451,6 +459,7 @@ public static class CCLicensed {
451459

452460
@BeforeClass
453461
public static void setUp() throws Exception {
462+
YoutubeStreamExtractor.resetDeobfuscationCode();
454463
NewPipe.init(DownloaderTestImpl.getInstance());
455464
extractor = YouTube.getStreamExtractor(URL);
456465
extractor.fetchPage();

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorLivestreamTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.schabi.newpipe.extractor.StreamingService;
99
import org.schabi.newpipe.extractor.services.DefaultStreamExtractorTest;
1010
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
11+
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor;
1112
import org.schabi.newpipe.extractor.stream.StreamExtractor;
1213
import org.schabi.newpipe.extractor.stream.StreamType;
1314

@@ -30,6 +31,7 @@ public class YoutubeStreamExtractorLivestreamTest extends DefaultStreamExtractor
3031
public static void setUp() throws Exception {
3132
YoutubeParsingHelper.resetClientVersionAndKey();
3233
YoutubeParsingHelper.setNumberGenerator(new Random(1));
34+
YoutubeStreamExtractor.resetDeobfuscationCode();
3335
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "live"));
3436
extractor = YouTube.getStreamExtractor(URL);
3537
extractor.fetchPage();

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorUnlistedTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.schabi.newpipe.extractor.StreamingService;
77
import org.schabi.newpipe.extractor.services.DefaultStreamExtractorTest;
88
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
9+
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor;
910
import org.schabi.newpipe.extractor.stream.StreamExtractor;
1011
import org.schabi.newpipe.extractor.stream.StreamType;
1112

@@ -28,6 +29,7 @@ public class YoutubeStreamExtractorUnlistedTest extends DefaultStreamExtractorTe
2829
public static void setUp() throws Exception {
2930
YoutubeParsingHelper.resetClientVersionAndKey();
3031
YoutubeParsingHelper.setNumberGenerator(new Random(1));
32+
YoutubeStreamExtractor.resetDeobfuscationCode();
3133
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "unlisted"));
3234
extractor = YouTube.getStreamExtractor(URL);
3335
extractor.fetchPage();

0 commit comments

Comments
 (0)