2323import org .schabi .newpipe .extractor .Page ;
2424import org .schabi .newpipe .extractor .exceptions .ExtractionException ;
2525import org .schabi .newpipe .extractor .exceptions .ParsingException ;
26+ import org .schabi .newpipe .extractor .playlist .PlaylistExtractor ;
2627import org .schabi .newpipe .extractor .playlist .PlaylistInfo ;
2728import org .schabi .newpipe .extractor .services .youtube .extractors .YoutubeMixPlaylistExtractor ;
2829import org .schabi .newpipe .extractor .stream .StreamInfoItem ;
2930
3031import java .io .IOException ;
3132import java .nio .charset .StandardCharsets ;
32- import java .util .HashMap ;
3333import java .util .HashSet ;
3434import java .util .Map ;
3535import java .util .Set ;
3636
37+ @ SuppressWarnings ({"MismatchedQueryAndUpdateOfCollection" , "NewClassNamingConvention" })
3738public class YoutubeMixPlaylistExtractorTest {
3839
3940 private static final String RESOURCE_PATH = DownloaderFactory .RESOURCE_PATH + "services/youtube/extractor/mix/" ;
40- private static final Map <String , String > dummyCookie = new HashMap <>();
41-
41+ private static final Map <String , String > dummyCookie = Map .of (YoutubeMixPlaylistExtractor .COOKIE_NAME , "whatever" );
4242 private static YoutubeMixPlaylistExtractor extractor ;
4343
4444 public static class Mix {
@@ -50,7 +50,6 @@ public static void setUp() throws Exception {
5050 YoutubeTestsUtils .ensureStateless ();
5151 YoutubeParsingHelper .setConsentAccepted (true );
5252 NewPipe .init (DownloaderFactory .getDownloader (RESOURCE_PATH + "mix" ));
53- dummyCookie .put (YoutubeMixPlaylistExtractor .COOKIE_NAME , "whatever" );
5453 extractor = (YoutubeMixPlaylistExtractor ) YouTube
5554 .getPlaylistExtractor ("https://www.youtube.com/watch?v=" + VIDEO_ID
5655 + "&list=RD" + VIDEO_ID );
@@ -135,7 +134,6 @@ void getPlaylistType() throws ParsingException {
135134 }
136135
137136 public static class MixWithIndex {
138-
139137 private static final String VIDEO_ID = "FAqYW76GLPA" ;
140138 private static final String VIDEO_TITLE = "Mix – " ;
141139 private static final int INDEX = 7 ; // YT starts the index with 1...
@@ -146,7 +144,6 @@ public static void setUp() throws Exception {
146144 YoutubeTestsUtils .ensureStateless ();
147145 YoutubeParsingHelper .setConsentAccepted (true );
148146 NewPipe .init (DownloaderFactory .getDownloader (RESOURCE_PATH + "mixWithIndex" ));
149- dummyCookie .put (YoutubeMixPlaylistExtractor .COOKIE_NAME , "whatever" );
150147 extractor = (YoutubeMixPlaylistExtractor ) YouTube
151148 .getPlaylistExtractor ("https://www.youtube.com/watch?v=" + VIDEO_ID_AT_INDEX
152149 + "&list=RD" + VIDEO_ID + "&index=" + INDEX );
@@ -233,7 +230,6 @@ public static void setUp() throws Exception {
233230 YoutubeTestsUtils .ensureStateless ();
234231 YoutubeParsingHelper .setConsentAccepted (true );
235232 NewPipe .init (DownloaderFactory .getDownloader (RESOURCE_PATH + "myMix" ));
236- dummyCookie .put (YoutubeMixPlaylistExtractor .COOKIE_NAME , "whatever" );
237233 extractor = (YoutubeMixPlaylistExtractor ) YouTube
238234 .getPlaylistExtractor ("https://www.youtube.com/watch?v=" + VIDEO_ID
239235 + "&list=RDMM" + VIDEO_ID );
@@ -316,15 +312,13 @@ void getPlaylistType() throws ParsingException {
316312 }
317313
318314 public static class Invalid {
319-
320315 private static final String VIDEO_ID = "QMVCAPd5cwBcg" ;
321316
322317 @ BeforeAll
323318 public static void setUp () throws IOException {
324319 YoutubeTestsUtils .ensureStateless ();
325320 YoutubeParsingHelper .setConsentAccepted (true );
326321 NewPipe .init (DownloaderFactory .getDownloader (RESOURCE_PATH + "invalid" ));
327- dummyCookie .put (YoutubeMixPlaylistExtractor .COOKIE_NAME , "whatever" );
328322 }
329323
330324 @ Test
@@ -348,7 +342,6 @@ void invalidVideoId() throws Exception {
348342 }
349343
350344 public static class ChannelMix {
351-
352345 private static final String CHANNEL_ID = "UCXuqSBlHAE6Xw-yeJA0Tunw" ;
353346 private static final String VIDEO_ID_OF_CHANNEL = "mnk6gnOBYIo" ;
354347 private static final String CHANNEL_TITLE = "Linus Tech Tips" ;
@@ -359,7 +352,6 @@ public static void setUp() throws Exception {
359352 YoutubeTestsUtils .ensureStateless ();
360353 YoutubeParsingHelper .setConsentAccepted (true );
361354 NewPipe .init (DownloaderFactory .getDownloader (RESOURCE_PATH + "channelMix" ));
362- dummyCookie .put (YoutubeMixPlaylistExtractor .COOKIE_NAME , "whatever" );
363355 extractor = (YoutubeMixPlaylistExtractor ) YouTube
364356 .getPlaylistExtractor ("https://www.youtube.com/watch?v=" + VIDEO_ID_OF_CHANNEL
365357 + "&list=RDCM" + CHANNEL_ID );
@@ -424,7 +416,6 @@ public static void setUp() throws Exception {
424416 YoutubeTestsUtils .ensureStateless ();
425417 YoutubeParsingHelper .setConsentAccepted (true );
426418 NewPipe .init (DownloaderFactory .getDownloader (RESOURCE_PATH + "genreMix" ));
427- dummyCookie .put (YoutubeMixPlaylistExtractor .COOKIE_NAME , "whatever" );
428419 extractor = (YoutubeMixPlaylistExtractor ) YouTube
429420 .getPlaylistExtractor ("https://www.youtube.com/watch?v=" + VIDEO_ID
430421 + "&list=RDGMEMYH9CUrFO7CfLJpaD7UR85w" );
@@ -504,4 +495,93 @@ void getPlaylistType() throws ParsingException {
504495 assertEquals (PlaylistInfo .PlaylistType .MIX_GENRE , extractor .getPlaylistType ());
505496 }
506497 }
498+
499+ public static class Music {
500+ private static final String VIDEO_ID = "dQw4w9WgXcQ" ;
501+ private static final String MIX_TITLE = "Mix – Rick Astley - Never Gonna Give You Up (Official Music Video)" ;
502+
503+ @ BeforeAll
504+ public static void setUp () throws Exception {
505+ YoutubeTestsUtils .ensureStateless ();
506+ YoutubeParsingHelper .setConsentAccepted (true );
507+ NewPipe .init (DownloaderFactory .getDownloader (RESOURCE_PATH + "musicMix" ));
508+ extractor = (YoutubeMixPlaylistExtractor )
509+ YouTube .getPlaylistExtractor ("https://m.youtube.com/watch?v=" + VIDEO_ID
510+ + "&list=RDAMVM" + VIDEO_ID );
511+ extractor .fetchPage ();
512+ }
513+
514+ @ Test
515+ void getServiceId () {
516+ assertEquals (YouTube .getServiceId (), extractor .getServiceId ());
517+ }
518+
519+ @ Test
520+ void getName () throws Exception {
521+ assertEquals (MIX_TITLE , extractor .getName ());
522+ }
523+
524+ @ Test
525+ void getThumbnailUrl () throws Exception {
526+ final String thumbnailUrl = extractor .getThumbnailUrl ();
527+ assertIsSecureUrl (thumbnailUrl );
528+ ExtractorAsserts .assertContains ("yt" , thumbnailUrl );
529+ ExtractorAsserts .assertContains (VIDEO_ID , thumbnailUrl );
530+ }
531+
532+ @ Test
533+ void getInitialPage () throws Exception {
534+ final InfoItemsPage <StreamInfoItem > streams = extractor .getInitialPage ();
535+ assertFalse (streams .getItems ().isEmpty ());
536+ assertTrue (streams .hasNextPage ());
537+ }
538+
539+ @ Test
540+ void getPage () throws Exception {
541+ final byte [] body = JsonWriter .string (prepareDesktopJsonBuilder (
542+ NewPipe .getPreferredLocalization (), NewPipe .getPreferredContentCountry ())
543+ .value ("videoId" , VIDEO_ID )
544+ .value ("playlistId" , "RD" + VIDEO_ID )
545+ .value ("params" , "OAE%3D" )
546+ .done ())
547+ .getBytes (StandardCharsets .UTF_8 );
548+
549+ final InfoItemsPage <StreamInfoItem > streams = extractor .getPage (new Page (
550+ YOUTUBEI_V1_URL + "next?key=" + getKey (), null , null , dummyCookie , body ));
551+ assertFalse (streams .getItems ().isEmpty ());
552+ assertTrue (streams .hasNextPage ());
553+ }
554+
555+ @ Test
556+ void getContinuations () throws Exception {
557+ InfoItemsPage <StreamInfoItem > streams = extractor .getInitialPage ();
558+ final Set <String > urls = new HashSet <>();
559+
560+ // Should work infinitely, but for testing purposes only 3 times
561+ for (int i = 0 ; i < 3 ; i ++) {
562+ assertTrue (streams .hasNextPage ());
563+ assertFalse (streams .getItems ().isEmpty ());
564+
565+ for (final StreamInfoItem item : streams .getItems ()) {
566+ // TODO Duplicates are appearing
567+ // assertFalse(urls.contains(item.getUrl()));
568+ urls .add (item .getUrl ());
569+ }
570+
571+ streams = extractor .getPage (streams .getNextPage ());
572+ }
573+ assertTrue (streams .hasNextPage ());
574+ assertFalse (streams .getItems ().isEmpty ());
575+ }
576+
577+ @ Test
578+ void getStreamCount () throws ParsingException {
579+ assertEquals (ListExtractor .ITEM_COUNT_INFINITE , extractor .getStreamCount ());
580+ }
581+
582+ @ Test
583+ void getPlaylistType () throws ParsingException {
584+ assertEquals (PlaylistInfo .PlaylistType .MIX_MUSIC , extractor .getPlaylistType ());
585+ }
586+ }
507587}
0 commit comments