diff --git a/app/src/main/java/io/github/aedev/flow/data/paging/SearchPagingSource.kt b/app/src/main/java/io/github/aedev/flow/data/paging/SearchPagingSource.kt index 757eb96e7..36eec6f9c 100644 --- a/app/src/main/java/io/github/aedev/flow/data/paging/SearchPagingSource.kt +++ b/app/src/main/java/io/github/aedev/flow/data/paging/SearchPagingSource.kt @@ -68,6 +68,20 @@ class SearchPagingSource( ) : PagingSource() { companion object { private const val TAG = "SearchPagingSource" + private val VIDEO_ID_PATTERNS = + listOf( + "v=([A-Za-z0-9_-]{11})".toRegex(), + "youtu\\.be/([A-Za-z0-9_-]{11})".toRegex(), + "shorts/([A-Za-z0-9_-]{11})".toRegex(), + ) + + internal fun extractVideoId(url: String): String { + for (pat in VIDEO_ID_PATTERNS) { + val m = pat.find(url) ?: continue + return m.groupValues[1] + } + return url.substringAfterLast("/").substringBefore("?").take(11) + } } private val service = ServiceList.YouTube @@ -381,20 +395,6 @@ class SearchPagingSource( ) }.distinctByNonBlankKey(Video::id) - private fun extractVideoId(url: String): String { - val patterns = - listOf( - "v=([A-Za-z0-9_-]{11})".toRegex(), - "youtu\\.be/([A-Za-z0-9_-]{11})".toRegex(), - "shorts/([A-Za-z0-9_-]{11})".toRegex(), - ) - for (pat in patterns) { - val m = pat.find(url) ?: continue - return m.groupValues[1] - } - return url.substringAfterLast("/").substringBefore("?").take(11) - } - private fun extractChannelId(url: String): String = url .substringAfter("/channel/") diff --git a/app/src/test/java/io/github/aedev/flow/data/paging/SearchPagingSourceTest.kt b/app/src/test/java/io/github/aedev/flow/data/paging/SearchPagingSourceTest.kt new file mode 100644 index 000000000..adea3d152 --- /dev/null +++ b/app/src/test/java/io/github/aedev/flow/data/paging/SearchPagingSourceTest.kt @@ -0,0 +1,21 @@ +package io.github.aedev.flow.data.paging + +import org.junit.Assert.assertEquals +import org.junit.Test + +class SearchPagingSourceTest { + @Test + fun testExtractVideoIdFormats() { + val standardUrl = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" + assertEquals("dQw4w9WgXcQ", SearchPagingSource.extractVideoId(standardUrl)) + + val shortUrl = "https://youtu.be/dQw4w9WgXcQ" + assertEquals("dQw4w9WgXcQ", SearchPagingSource.extractVideoId(shortUrl)) + + val shortsUrl = "https://www.youtube.com/shorts/dQw4w9WgXcQ" + assertEquals("dQw4w9WgXcQ", SearchPagingSource.extractVideoId(shortsUrl)) + + val fallbackUrl = "https://www.youtube.com/embed/dQw4w9WgXcQ" + assertEquals("dQw4w9WgXcQ", SearchPagingSource.extractVideoId(fallbackUrl)) + } +}