Skip to content

Commit b1f995a

Browse files
committed
[#11930] Playlist with more than 50 items
1 parent acac50a commit b1f995a

3 files changed

Lines changed: 50 additions & 4 deletions

File tree

app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ dependencies {
255255
// HTTP client
256256
implementation "com.squareup.okhttp3:okhttp:4.12.0"
257257

258+
// Apache Commons Collections
259+
implementation group: 'org.apache.commons', name: 'commons-collections4', version: '4.4'
260+
258261
// Media player
259262
implementation "com.google.android.exoplayer:exoplayer-core:${exoPlayerVersion}"
260263
implementation "com.google.android.exoplayer:exoplayer-dash:${exoPlayerVersion}"

app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import com.evernote.android.state.State;
3333

34+
import org.apache.commons.collections4.queue.CircularFifoQueue;
3435
import org.reactivestreams.Subscriber;
3536
import org.reactivestreams.Subscription;
3637
import org.schabi.newpipe.NewPipeDatabase;
@@ -460,10 +461,15 @@ static String exportJustUrls(final Stream<StreamEntity> entityStream) {
460461

461462
static String exportAsYoutubeTempPlaylist(final Stream<StreamEntity> entityStream) {
462463

463-
final String videoIDs = entityStream
464-
.map(entity -> getYouTubeId(entity.getUrl()))
465-
.filter(Objects::nonNull)
466-
.collect(Collectors.joining(","));
464+
final CircularFifoQueue<String> last50 = new CircularFifoQueue<>(50);
465+
466+
entityStream
467+
.map(entity -> getYouTubeId(entity.getUrl()))
468+
.filter(Objects::nonNull)
469+
.forEachOrdered(last50::add);
470+
471+
final String videoIDs = last50.stream()
472+
.collect(Collectors.joining(","));
467473

468474
return "http://www.youtube.com/watch_videos?video_ids=" + videoIDs;
469475
}

app/src/test/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragmentTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.schabi.newpipe.database.stream.model.StreamEntity;
1111
import org.schabi.newpipe.extractor.stream.StreamType;
1212

13+
import java.util.List;
1314
import java.util.stream.Stream;
1415

1516
public class LocalPlaylistFragmentTest {
@@ -30,6 +31,42 @@ public void exportAsYouTubeTempPlaylist() {
3031
Assert.assertEquals("http://www.youtube.com/watch_videos?video_ids=1,2,3", url);
3132
}
3233

34+
@Test
35+
public void exportMoreThan50Items() {
36+
/*
37+
* Playlist has more than 50 items => take the last 50
38+
* (YouTube limitation)
39+
*/
40+
41+
final List<Integer> ids = List.of(
42+
43+
-1, 0,
44+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
45+
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
46+
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
47+
31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
48+
41, 42, 43, 44, 45, 46, 47, 48, 49, 50
49+
);
50+
51+
final Stream<StreamEntity> entityStream = ids.stream()
52+
.map(id -> "https://www.youtube.com/watch?v=" + id)
53+
.map(LocalPlaylistFragmentTest::newStreamEntity);
54+
55+
final String url = LocalPlaylistFragment.export(YOUTUBE_TEMP_PLAYLIST, entityStream, null);
56+
57+
Assert.assertEquals(
58+
59+
"http://www.youtube.com/watch_videos?video_ids="
60+
+ "1,2,3,4,5,6,7,8,9,10,"
61+
+ "11,12,13,14,15,16,17,18,19,20,"
62+
+ "21,22,23,24,25,26,27,28,29,30,"
63+
+ "31,32,33,34,35,36,37,38,39,40,"
64+
+ "41,42,43,44,45,46,47,48,49,50",
65+
66+
url
67+
);
68+
}
69+
3370
@Test
3471
public void exportJustUrls() {
3572

0 commit comments

Comments
 (0)