Skip to content

Commit c28478a

Browse files
committed
getYouTubeId(): Changing implementation to use YoutubeStreamLinkHandler
(PR review from @Stypox)
1 parent d81244e commit c28478a

2 files changed

Lines changed: 23 additions & 29 deletions

File tree

app/src/main/java/org/schabi/newpipe/local/playlist/ExportPlaylist.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package org.schabi.newpipe.local.playlist
22

33
import android.content.Context
4-
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
54
import org.schabi.newpipe.R
65
import org.schabi.newpipe.database.playlist.PlaylistStreamEntry
6+
import org.schabi.newpipe.extractor.exceptions.ParsingException
7+
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeStreamLinkHandlerFactory
78
import org.schabi.newpipe.local.playlist.PlayListShareMode.JUST_URLS
89
import org.schabi.newpipe.local.playlist.PlayListShareMode.WITH_TITLES
910
import org.schabi.newpipe.local.playlist.PlayListShareMode.YOUTUBE_TEMP_PLAYLIST
10-
import java.util.Objects.nonNull
1111

1212
fun export(
1313
shareMode: PlayListShareMode,
@@ -48,9 +48,8 @@ fun exportJustUrls(playlist: List<PlaylistStreamEntry>): String {
4848
fun exportAsYoutubeTempPlaylist(playlist: List<PlaylistStreamEntry>): String {
4949

5050
val videoIDs = playlist.asReversed().asSequence()
51-
.map { it.streamEntity }
52-
.map { getYouTubeId(it.url) }
53-
.filter(::nonNull)
51+
.map { it.streamEntity.url }
52+
.mapNotNull(::getYouTubeId)
5453
.take(50)
5554
.toList()
5655
.asReversed()
@@ -59,14 +58,15 @@ fun exportAsYoutubeTempPlaylist(playlist: List<PlaylistStreamEntry>): String {
5958
return "https://www.youtube.com/watch_videos?video_ids=$videoIDs"
6059
}
6160

61+
val linkHandler: YoutubeStreamLinkHandlerFactory = YoutubeStreamLinkHandlerFactory.getInstance()
62+
6263
/**
6364
* Gets the video id from a YouTube URL.
6465
*
6566
* @param url YouTube URL
6667
* @return the video id
6768
*/
6869
fun getYouTubeId(url: String): String? {
69-
val httpUrl = url.toHttpUrlOrNull()
7070

71-
return httpUrl?.queryParameter("v")
71+
return try { linkHandler.getId(url) } catch (e: ParsingException) { null }
7272
}

app/src/test/java/org/schabi/newpipe/local/playlist/ExportPlaylistTest.kt

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@ class ExportPlaylistTest {
1616
@Test
1717
fun exportAsYouTubeTempPlaylist() {
1818
val playlist = asPlaylist(
19-
"https://www.youtube.com/watch?v=1",
19+
"https://www.youtube.com/watch?v=10000000000",
2020
"https://soundcloud.com/cautious-clayofficial/cold-war-2", // non-Youtube URLs should be ignored
21-
"https://www.youtube.com/watch?v=2",
22-
"https://www.youtube.com/watch?v=3"
21+
"https://www.youtube.com/watch?v=20000000000",
22+
"https://www.youtube.com/watch?v=30000000000"
2323
)
2424

2525
val url = export(YOUTUBE_TEMP_PLAYLIST, playlist, mock(Context::class.java))
2626

27-
assertEquals("http://www.youtube.com/watch_videos?video_ids=1,2,3", url)
27+
assertEquals(
28+
"https://www.youtube.com/watch_videos?video_ids=" +
29+
"10000000000," +
30+
"20000000000," +
31+
"30000000000",
32+
url
33+
)
2834
}
2935

3036
@Test
@@ -34,30 +40,18 @@ class ExportPlaylistTest {
3440
* (YouTube limitation)
3541
*/
3642

37-
val ids = listOf(
38-
-1, 0,
39-
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
40-
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
41-
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
42-
31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
43-
41, 42, 43, 44, 45, 46, 47, 48, 49, 50
44-
)
45-
4643
val playlist = asPlaylist(
47-
ids.stream()
48-
.map { id: Int -> "https://www.youtube.com/watch?v=$id" }
44+
(10..70)
45+
.map { id -> "https://www.youtube.com/watch?v=aaaaaaaaa$id" } // YouTube video IDs are 11 characters long
46+
.stream()
4947
)
5048

5149
val url = export(YOUTUBE_TEMP_PLAYLIST, playlist, mock(Context::class.java))
5250

53-
assertEquals(
54-
"http://www.youtube.com/watch_videos?video_ids=" +
55-
"1,2,3,4,5,6,7,8,9,10," +
56-
"11,12,13,14,15,16,17,18,19,20," +
57-
"21,22,23,24,25,26,27,28,29,30," +
58-
"31,32,33,34,35,36,37,38,39,40," +
59-
"41,42,43,44,45,46,47,48,49,50",
51+
val videoIDs = (21..70).map { id -> "aaaaaaaaa$id" }.joinToString(",")
6052

53+
assertEquals(
54+
"https://www.youtube.com/watch_videos?video_ids=$videoIDs",
6155
url
6256
)
6357
}

0 commit comments

Comments
 (0)