Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ class SearchPagingSource(
) : PagingSource<Page, SearchResultItem>() {
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
Expand Down Expand Up @@ -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/")
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}
}
Loading