Skip to content

shubham0204/google-search-kotlin

Repository files navigation

Google Search Results - Kotlin

A simple library that provides an API to fetch Google Search results given a query to search

Setup

The library is distributed with Jitpack. In the root build.gradle file, add the jitpack repository,

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
}

Next, add the library dependency in the module-level build.gradle,

dependencies {
    implementation 'com.github.shubham0204:google-search-kotlin:0.0.1'
}

Usage

The library provides two static methods to fetch Google Search results - search and searchAsFlow. The arguments passed to both the methods are same, except that searchAsFlow returns a kotlinx.coroutines.flow.Flow object.

CoroutineScope(Dispatchers.Default).launch {
    val results: Flow<GoogleSearchProvider.GoogleSearchResult> = GoogleSearchProvider.searchAsFlow(
        term  = "" ,
        readPageText = false ,
        numResults = 10,
        lang = "en",
        safe = "active",
        timeframe = GoogleSearchProvider.SearchTimeframe.PAST_24HOURS,
        readPageText = false
    )
    results.collect {
        println( it.title )
        println( it.href )
    }
}