Refactor ExportPlaylist to use more idiomatic kotlin code

This commit is contained in:
Yevhen Babiichuk (DustDFG) 2025-12-31 14:28:11 +02:00
parent 8893a27668
commit 8379aa0a9d
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
import android.content.Context
@ -21,11 +26,7 @@ fun export(
}
}
fun exportWithTitles(
playlist: List<PlaylistStreamEntry>,
context: Context
): String {
private fun exportWithTitles(playlist: List<PlaylistStreamEntry>, context: Context): String {
return playlist.asSequence()
.map { it.streamEntity }
.map { entity ->
@ -38,18 +39,14 @@ fun exportWithTitles(
.joinToString(separator = "\n")
}
fun exportJustUrls(playlist: List<PlaylistStreamEntry>): String {
return playlist.asSequence()
.map { it.streamEntity.url }
.joinToString(separator = "\n")
private fun exportJustUrls(playlist: List<PlaylistStreamEntry>): String {
return playlist.joinToString(separator = "\n") { it.streamEntity.url }
}
fun exportAsYoutubeTempPlaylist(playlist: List<PlaylistStreamEntry>): String {
private fun exportAsYoutubeTempPlaylist(playlist: List<PlaylistStreamEntry>): String {
val videoIDs = playlist.asReversed().asSequence()
.map { it.streamEntity.url }
.mapNotNull(::getYouTubeId)
.mapNotNull { getYouTubeId(it.streamEntity.url) }
.take(50) // YouTube limitation: temp playlists can't have more than 50 items
.toList()
.asReversed()
@ -58,7 +55,7 @@ fun exportAsYoutubeTempPlaylist(playlist: List<PlaylistStreamEntry>): String {
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.
@ -66,7 +63,7 @@ val linkHandler: YoutubeStreamLinkHandlerFactory = YoutubeStreamLinkHandlerFacto
* @param url YouTube URL
* @return the video id
*/
fun getYouTubeId(url: String): String? {
private fun getYouTubeId(url: String): String? {
return try { linkHandler.getId(url) } catch (e: ParsingException) { null }
}