mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2026-03-15 20:29:43 +00:00
Refactor ExportPlaylist to use more idiomatic kotlin code
This commit is contained in:
@@ -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 }
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -9,7 +14,6 @@ import org.schabi.newpipe.database.stream.model.StreamEntity
|
||||
import org.schabi.newpipe.extractor.stream.StreamType
|
||||
import org.schabi.newpipe.local.playlist.PlayListShareMode.JUST_URLS
|
||||
import org.schabi.newpipe.local.playlist.PlayListShareMode.YOUTUBE_TEMP_PLAYLIST
|
||||
import java.util.stream.Stream
|
||||
|
||||
class ExportPlaylistTest {
|
||||
|
||||
@@ -41,9 +45,7 @@ class ExportPlaylistTest {
|
||||
*/
|
||||
|
||||
val playlist = asPlaylist(
|
||||
(10..70)
|
||||
.map { id -> "https://www.youtube.com/watch?v=aaaaaaaaa$id" } // YouTube video IDs are 11 characters long
|
||||
.stream()
|
||||
(10..70).map { id -> "https://www.youtube.com/watch?v=aaaaaaaaa$id" } // YouTube video IDs are 11 characters long
|
||||
)
|
||||
|
||||
val url = export(YOUTUBE_TEMP_PLAYLIST, playlist, mock(Context::class.java))
|
||||
@@ -78,13 +80,11 @@ class ExportPlaylistTest {
|
||||
}
|
||||
|
||||
fun asPlaylist(vararg urls: String): List<PlaylistStreamEntry> {
|
||||
return asPlaylist(Stream.of(*urls))
|
||||
return asPlaylist(listOf(*urls))
|
||||
}
|
||||
|
||||
fun asPlaylist(urls: Stream<String>): List<PlaylistStreamEntry> {
|
||||
return urls
|
||||
.map { url: String -> newPlaylistStreamEntry(url) }
|
||||
.toList()
|
||||
fun asPlaylist(urls: List<String>): List<PlaylistStreamEntry> {
|
||||
return urls.map { newPlaylistStreamEntry(it) }
|
||||
}
|
||||
|
||||
fun newPlaylistStreamEntry(url: String): PlaylistStreamEntry {
|
||||
|
||||
Reference in New Issue
Block a user