Merge pull request #12981 from dustdfg/export_playlist_refactor

Refactor ExportPlaylist to use more idiomatic kotlin code
This commit is contained in:
Aayush Gupta 2026-01-01 21:40:05 +08:00 committed by GitHub
commit 31e2323aa5
2 changed files with 21 additions and 24 deletions

View file

@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2025 NewPipe contributors <https://newpipe.net>
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package org.schabi.newpipe.local.playlist package org.schabi.newpipe.local.playlist
import android.content.Context import android.content.Context
@ -21,11 +26,7 @@ fun export(
} }
} }
fun exportWithTitles( private fun exportWithTitles(playlist: List<PlaylistStreamEntry>, context: Context): String {
playlist: List<PlaylistStreamEntry>,
context: Context
): String {
return playlist.asSequence() return playlist.asSequence()
.map { it.streamEntity } .map { it.streamEntity }
.map { entity -> .map { entity ->
@ -38,18 +39,14 @@ fun exportWithTitles(
.joinToString(separator = "\n") .joinToString(separator = "\n")
} }
fun exportJustUrls(playlist: List<PlaylistStreamEntry>): String { private fun exportJustUrls(playlist: List<PlaylistStreamEntry>): String {
return playlist.joinToString(separator = "\n") { it.streamEntity.url }
return playlist.asSequence()
.map { it.streamEntity.url }
.joinToString(separator = "\n")
} }
fun exportAsYoutubeTempPlaylist(playlist: List<PlaylistStreamEntry>): String { private fun exportAsYoutubeTempPlaylist(playlist: List<PlaylistStreamEntry>): String {
val videoIDs = playlist.asReversed().asSequence() val videoIDs = playlist.asReversed().asSequence()
.map { it.streamEntity.url } .mapNotNull { getYouTubeId(it.streamEntity.url) }
.mapNotNull(::getYouTubeId)
.take(50) // YouTube limitation: temp playlists can't have more than 50 items .take(50) // YouTube limitation: temp playlists can't have more than 50 items
.toList() .toList()
.asReversed() .asReversed()
@ -58,7 +55,7 @@ fun exportAsYoutubeTempPlaylist(playlist: List<PlaylistStreamEntry>): String {
return "https://www.youtube.com/watch_videos?video_ids=$videoIDs" return "https://www.youtube.com/watch_videos?video_ids=$videoIDs"
} }
val linkHandler: YoutubeStreamLinkHandlerFactory = YoutubeStreamLinkHandlerFactory.getInstance() private val linkHandler: YoutubeStreamLinkHandlerFactory = YoutubeStreamLinkHandlerFactory.getInstance()
/** /**
* Gets the video id from a YouTube URL. * Gets the video id from a YouTube URL.
@ -66,7 +63,7 @@ val linkHandler: YoutubeStreamLinkHandlerFactory = YoutubeStreamLinkHandlerFacto
* @param url YouTube URL * @param url YouTube URL
* @return the video id * @return the video id
*/ */
fun getYouTubeId(url: String): String? { private fun getYouTubeId(url: String): String? {
return try { linkHandler.getId(url) } catch (e: ParsingException) { null } return try { linkHandler.getId(url) } catch (e: ParsingException) { null }
} }

View file

@ -1,3 +1,8 @@
/*
* SPDX-FileCopyrightText: 2025 NewPipe contributors <https://newpipe.net>
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package org.schabi.newpipe.local.playlist package org.schabi.newpipe.local.playlist
import android.content.Context import android.content.Context
@ -9,7 +14,6 @@ import org.schabi.newpipe.database.stream.model.StreamEntity
import org.schabi.newpipe.extractor.stream.StreamType import org.schabi.newpipe.extractor.stream.StreamType
import org.schabi.newpipe.local.playlist.PlayListShareMode.JUST_URLS import org.schabi.newpipe.local.playlist.PlayListShareMode.JUST_URLS
import org.schabi.newpipe.local.playlist.PlayListShareMode.YOUTUBE_TEMP_PLAYLIST import org.schabi.newpipe.local.playlist.PlayListShareMode.YOUTUBE_TEMP_PLAYLIST
import java.util.stream.Stream
class ExportPlaylistTest { class ExportPlaylistTest {
@ -41,9 +45,7 @@ class ExportPlaylistTest {
*/ */
val playlist = asPlaylist( val playlist = asPlaylist(
(10..70) (10..70).map { id -> "https://www.youtube.com/watch?v=aaaaaaaaa$id" } // YouTube video IDs are 11 characters long
.map { id -> "https://www.youtube.com/watch?v=aaaaaaaaa$id" } // YouTube video IDs are 11 characters long
.stream()
) )
val url = export(YOUTUBE_TEMP_PLAYLIST, playlist, mock(Context::class.java)) val url = export(YOUTUBE_TEMP_PLAYLIST, playlist, mock(Context::class.java))
@ -78,13 +80,11 @@ class ExportPlaylistTest {
} }
fun asPlaylist(vararg urls: String): List<PlaylistStreamEntry> { fun asPlaylist(vararg urls: String): List<PlaylistStreamEntry> {
return asPlaylist(Stream.of(*urls)) return asPlaylist(listOf(*urls))
} }
fun asPlaylist(urls: Stream<String>): List<PlaylistStreamEntry> { fun asPlaylist(urls: List<String>): List<PlaylistStreamEntry> {
return urls return urls.map { newPlaylistStreamEntry(it) }
.map { url: String -> newPlaylistStreamEntry(url) }
.toList()
} }
fun newPlaylistStreamEntry(url: String): PlaylistStreamEntry { fun newPlaylistStreamEntry(url: String): PlaylistStreamEntry {