mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2025-04-06 02:37:18 +00:00
getYouTubeId(): Changing implementation to use YoutubeStreamLinkHandler
(PR review from @Stypox)
This commit is contained in:
parent
d81244e77c
commit
c28478ae53
@ -1,13 +1,13 @@
|
||||
package org.schabi.newpipe.local.playlist
|
||||
|
||||
import android.content.Context
|
||||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||
import org.schabi.newpipe.R
|
||||
import org.schabi.newpipe.database.playlist.PlaylistStreamEntry
|
||||
import org.schabi.newpipe.extractor.exceptions.ParsingException
|
||||
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeStreamLinkHandlerFactory
|
||||
import org.schabi.newpipe.local.playlist.PlayListShareMode.JUST_URLS
|
||||
import org.schabi.newpipe.local.playlist.PlayListShareMode.WITH_TITLES
|
||||
import org.schabi.newpipe.local.playlist.PlayListShareMode.YOUTUBE_TEMP_PLAYLIST
|
||||
import java.util.Objects.nonNull
|
||||
|
||||
fun export(
|
||||
shareMode: PlayListShareMode,
|
||||
@ -48,9 +48,8 @@ fun exportJustUrls(playlist: List<PlaylistStreamEntry>): String {
|
||||
fun exportAsYoutubeTempPlaylist(playlist: List<PlaylistStreamEntry>): String {
|
||||
|
||||
val videoIDs = playlist.asReversed().asSequence()
|
||||
.map { it.streamEntity }
|
||||
.map { getYouTubeId(it.url) }
|
||||
.filter(::nonNull)
|
||||
.map { it.streamEntity.url }
|
||||
.mapNotNull(::getYouTubeId)
|
||||
.take(50)
|
||||
.toList()
|
||||
.asReversed()
|
||||
@ -59,6 +58,8 @@ fun exportAsYoutubeTempPlaylist(playlist: List<PlaylistStreamEntry>): String {
|
||||
return "https://www.youtube.com/watch_videos?video_ids=$videoIDs"
|
||||
}
|
||||
|
||||
val linkHandler: YoutubeStreamLinkHandlerFactory = YoutubeStreamLinkHandlerFactory.getInstance()
|
||||
|
||||
/**
|
||||
* Gets the video id from a YouTube URL.
|
||||
*
|
||||
@ -66,7 +67,6 @@ fun exportAsYoutubeTempPlaylist(playlist: List<PlaylistStreamEntry>): String {
|
||||
* @return the video id
|
||||
*/
|
||||
fun getYouTubeId(url: String): String? {
|
||||
val httpUrl = url.toHttpUrlOrNull()
|
||||
|
||||
return httpUrl?.queryParameter("v")
|
||||
return try { linkHandler.getId(url) } catch (e: ParsingException) { null }
|
||||
}
|
||||
|
@ -16,15 +16,21 @@ class ExportPlaylistTest {
|
||||
@Test
|
||||
fun exportAsYouTubeTempPlaylist() {
|
||||
val playlist = asPlaylist(
|
||||
"https://www.youtube.com/watch?v=1",
|
||||
"https://www.youtube.com/watch?v=10000000000",
|
||||
"https://soundcloud.com/cautious-clayofficial/cold-war-2", // non-Youtube URLs should be ignored
|
||||
"https://www.youtube.com/watch?v=2",
|
||||
"https://www.youtube.com/watch?v=3"
|
||||
"https://www.youtube.com/watch?v=20000000000",
|
||||
"https://www.youtube.com/watch?v=30000000000"
|
||||
)
|
||||
|
||||
val url = export(YOUTUBE_TEMP_PLAYLIST, playlist, mock(Context::class.java))
|
||||
|
||||
assertEquals("http://www.youtube.com/watch_videos?video_ids=1,2,3", url)
|
||||
assertEquals(
|
||||
"https://www.youtube.com/watch_videos?video_ids=" +
|
||||
"10000000000," +
|
||||
"20000000000," +
|
||||
"30000000000",
|
||||
url
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -34,30 +40,18 @@ class ExportPlaylistTest {
|
||||
* (YouTube limitation)
|
||||
*/
|
||||
|
||||
val ids = listOf(
|
||||
-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
|
||||
)
|
||||
|
||||
val playlist = asPlaylist(
|
||||
ids.stream()
|
||||
.map { id: Int -> "https://www.youtube.com/watch?v=$id" }
|
||||
(10..70)
|
||||
.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))
|
||||
|
||||
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",
|
||||
val videoIDs = (21..70).map { id -> "aaaaaaaaa$id" }.joinToString(",")
|
||||
|
||||
assertEquals(
|
||||
"https://www.youtube.com/watch_videos?video_ids=$videoIDs",
|
||||
url
|
||||
)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user