From b1f995a78c6ed792296d35969afcd118c97584a1 Mon Sep 17 00:00:00 2001 From: "Thiago F. G. Albuquerque" Date: Thu, 20 Feb 2025 16:26:03 -0300 Subject: [PATCH] [#11930] Playlist with more than 50 items --- app/build.gradle | 3 ++ .../local/playlist/LocalPlaylistFragment.java | 14 +++++-- .../playlist/LocalPlaylistFragmentTest.java | 37 +++++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d03bd64e3..2cdc952af 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -255,6 +255,9 @@ dependencies { // HTTP client implementation "com.squareup.okhttp3:okhttp:4.12.0" + // Apache Commons Collections + implementation group: 'org.apache.commons', name: 'commons-collections4', version: '4.4' + // Media player implementation "com.google.android.exoplayer:exoplayer-core:${exoPlayerVersion}" implementation "com.google.android.exoplayer:exoplayer-dash:${exoPlayerVersion}" diff --git a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java index cb47c2199..008dc4a9c 100644 --- a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java @@ -31,6 +31,7 @@ import androidx.viewbinding.ViewBinding; import com.evernote.android.state.State; +import org.apache.commons.collections4.queue.CircularFifoQueue; import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; import org.schabi.newpipe.NewPipeDatabase; @@ -460,10 +461,15 @@ public class LocalPlaylistFragment extends BaseLocalListFragment entityStream) { - final String videoIDs = entityStream - .map(entity -> getYouTubeId(entity.getUrl())) - .filter(Objects::nonNull) - .collect(Collectors.joining(",")); + final CircularFifoQueue last50 = new CircularFifoQueue<>(50); + + entityStream + .map(entity -> getYouTubeId(entity.getUrl())) + .filter(Objects::nonNull) + .forEachOrdered(last50::add); + + final String videoIDs = last50.stream() + .collect(Collectors.joining(",")); return "http://www.youtube.com/watch_videos?video_ids=" + videoIDs; } diff --git a/app/src/test/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragmentTest.java b/app/src/test/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragmentTest.java index fb93a9e86..eca798a24 100644 --- a/app/src/test/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragmentTest.java +++ b/app/src/test/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragmentTest.java @@ -10,6 +10,7 @@ import org.junit.Test; import org.schabi.newpipe.database.stream.model.StreamEntity; import org.schabi.newpipe.extractor.stream.StreamType; +import java.util.List; import java.util.stream.Stream; public class LocalPlaylistFragmentTest { @@ -30,6 +31,42 @@ public class LocalPlaylistFragmentTest { Assert.assertEquals("http://www.youtube.com/watch_videos?video_ids=1,2,3", url); } + @Test + public void exportMoreThan50Items() { + /* + * Playlist has more than 50 items => take the last 50 + * (YouTube limitation) + */ + + final List ids = List.of( + + -1, 0, + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, + 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, + 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, + 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50 + ); + + final Stream entityStream = ids.stream() + .map(id -> "https://www.youtube.com/watch?v=" + id) + .map(LocalPlaylistFragmentTest::newStreamEntity); + + final String url = LocalPlaylistFragment.export(YOUTUBE_TEMP_PLAYLIST, entityStream, null); + + Assert.assertEquals( + + "http://www.youtube.com/watch_videos?video_ids=" + + "1,2,3,4,5,6,7,8,9,10," + + "11,12,13,14,15,16,17,18,19,20," + + "21,22,23,24,25,26,27,28,29,30," + + "31,32,33,34,35,36,37,38,39,40," + + "41,42,43,44,45,46,47,48,49,50", + + url + ); + } + @Test public void exportJustUrls() {