Skip to content

Commit

Permalink
Added Search feature to Youtube Scraper (#194)
Browse files Browse the repository at this point in the history
* Added search functionality to YoutubeSkraper.kt and test in YoutubeSkraperTest.kt
* Changes to adapt library structure.
  • Loading branch information
OmarELRayes committed Aug 25, 2021
1 parent e323b70 commit acfb307
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ open class YoutubeSkraper @JvmOverloads constructor(

val jsonMetadata = page?.readJsonMetadata()

val fieldName = if(path.startsWith(SEARCH_PREFIX)) "videoRenderer" else "gridVideoRenderer"

val rawPosts = jsonMetadata
?.findParents("gridVideoRenderer")
?.map { it["gridVideoRenderer"] }
?.findParents(fieldName)
?.map { it[fieldName] }
.orEmpty()

emitBatch(rawPosts) {
Expand All @@ -64,7 +66,7 @@ open class YoutubeSkraper @JvmOverloads constructor(
text = getString("title.runs.0.text"),
publishedAt = getString("publishedTimeText")?.extractTimeAgo(),
statistics = PostStatistics(
views = getString("viewCountText.simpleText")?.substringBefore(" ")?.toIntOrNull(),
views = getString("viewCountText.simpleText")?.substringBefore(" ")?.replace(",","")?.toIntOrNull(),
),
media = extractVideos()
)
Expand Down Expand Up @@ -205,5 +207,6 @@ open class YoutubeSkraper @JvmOverloads constructor(

companion object {
const val BASE_URL: String = "https://www.youtube.com"
const val SEARCH_PREFIX: String = "/results"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ fun YoutubeSkraper.getUserPosts(username: String): Flow<Post> {

suspend fun YoutubeSkraper.getUserInfo(username: String): PageInfo? {
return getPageInfo(path = "/user/${username}")
}

fun YoutubeSkraper.getSearchPosts(keyword: String): Flow<Post> {
return getPosts(path = "/results?search_query=${keyword.replace(" ","+")}")
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ class YoutubeSkraperTest : SkraperTck() {
fun `Check media downloading`() {
assertMediaDownloaded(Video("https://youtu.be/fjUO7xaUHJQ"))
}

@Test
fun `Check search posts`() {
assertPosts { skraper.getSearchPosts("Nothing Else Matters") }
}
}

0 comments on commit acfb307

Please sign in to comment.