ktor impl

This commit is contained in:
shabinder 2021-01-26 20:24:28 +05:30
parent d52ab8155b
commit a647d1ebce
8 changed files with 216 additions and 42 deletions

View File

@ -5,12 +5,13 @@ import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.setContent
import com.shabinder.common.authenticateSpotify
import com.shabinder.common.spotify.authenticateSpotify
import com.shabinder.common.ui.SpotiFlyerMain
import com.shabinder.common.youtube.YoutubeMusic
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class MainActivity : AppCompatActivity() {
class MainActivity : AppCompatActivity(),YoutubeMusic {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
@ -18,7 +19,9 @@ class MainActivity : AppCompatActivity() {
SpotiFlyerMain()
scope.launch(Dispatchers.IO) {
val token = authenticateSpotify()
val response = getYoutubeMusicResponse("filhaal")
Log.i("Spotify",token.toString())
Log.i("Youtube",response)
}
}
}

View File

@ -1,13 +0,0 @@
package com.shabinder.common.database
import com.squareup.sqldelight.db.SqlDriver
import com.squareup.sqldelight.sqlite.driver.JdbcSqliteDriver
import com.shabinder.database.TodoDatabase
@Suppress("FunctionName") // FactoryFunction
actual fun TestDatabaseDriver(): SqlDriver {
val driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
TodoDatabase.Schema.create(driver)
return driver
}

View File

@ -1,14 +0,0 @@
package com.shabinder.common.database
import android.content.Context
import com.squareup.sqldelight.android.AndroidSqliteDriver
import com.squareup.sqldelight.db.SqlDriver
import com.shabinder.database.TodoDatabase
@Suppress("FunctionName") // FactoryFunction
fun TodoDatabaseDriver(context: Context): SqlDriver =
AndroidSqliteDriver(
schema = TodoDatabase.Schema,
context = context,
name = "TodoDatabase.db"
)

View File

@ -12,6 +12,7 @@ kotlin {
implementation(project(":common:database"))
implementation("org.kodein.di:kodein-di:${Versions.kodein}")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.1")
implementation(Extras.jsonKlaxon)
implementation(Ktor.clientCore)
implementation(Ktor.clientCio)
implementation(Ktor.clientJson)

View File

@ -0,0 +1,101 @@
package com.shabinder.common.gaana
import com.shabinder.common.spotify.kotlinxSerializer
import io.ktor.client.*
import io.ktor.client.features.json.*
import io.ktor.client.request.*
private const val TOKEN = "b2e6d7fbc136547a940516e9b77e5990"
private const val BASE_URL = "https://api.gaana.com/"
interface GaanaRequests {
/*
* Api Request: http://api.gaana.com/?type=playlist&subtype=playlist_detail&seokey=gaana-dj-hindi-top-50-1&token=b2e6d7fbc136547a940516e9b77e5990&format=JSON
*
* subtype : ["most_popular_playlist" , "playlist_home_featured" ,"playlist_detail" ,"user_playlist" ,"topCharts"]
**/
suspend fun getGaanaPlaylist(
type: String = "playlist",
subtype: String = "playlist_detail",
seokey: String,
format: String = "JSON",
limit: Int = 2000
): GaanaPlaylist{
return httpClient.get(
"$BASE_URL/?type=$type&subtype=$subtype&seokey=$seokey&token=$TOKEN&format=$format&limit=$limit"
)
}
/*
* Api Request: http://api.gaana.com/?type=album&subtype=album_detail&seokey=kabir-singh&token=b2e6d7fbc136547a940516e9b77e5990&format=JSON
*
* subtype : ["most_popular" , "new_release" ,"featured_album" ,"similar_album" ,"all_albums", "album" ,"album_detail" ,"album_detail_info"]
**/
suspend fun getGaanaAlbum(
type: String = "album",
subtype: String = "album_detail",
seokey: String,
format: String = "JSON",
limit: Int = 2000
): GaanaAlbum{
return httpClient.get(
"$BASE_URL/?type=$type&subtype=$subtype&seokey=$seokey&token=$TOKEN&format=$format&limit=$limit"
)
}
/*
* Api Request: http://api.gaana.com/?type=song&subtype=song_detail&seokey=pachtaoge&token=b2e6d7fbc136547a940516e9b77e5990&format=JSON
*
* subtype : ["most_popular" , "hot_songs" ,"recommendation" ,"song_detail"]
**/
suspend fun getGaanaSong(
type: String = "song",
subtype: String = "song_detail",
seokey: String,
format: String = "JSON",
): GaanaSong{
return httpClient.get(
"$BASE_URL/?type=$type&subtype=$subtype&seokey=$seokey&token=$TOKEN&format=$format"
)
}
/*
* Api Request: https://api.gaana.com/?type=artist&subtype=artist_details_info&seokey=neha-kakkar&token=b2e6d7fbc136547a940516e9b77e5990&format=JSON
*
* subtype : ["most_popular" , "artist_list" ,"artist_track_listing" ,"artist_album" ,"similar_artist","artist_details" ,"artist_details_info"]
**/
suspend fun getGaanaArtistDetails(
type: String = "artist",
subtype: String = "artist_details_info",
seokey: String,
format: String = "JSON",
): GaanaArtistDetails{
return httpClient.get(
"$BASE_URL/?type=$type&subtype=$subtype&seokey=$seokey&token=$TOKEN&format=$format"
)
}
/*
* Api Request: http://api.gaana.com/?type=artist&subtype=artist_track_listing&seokey=neha-kakkar&limit=50&token=b2e6d7fbc136547a940516e9b77e5990&format=JSON
*
* subtype : ["most_popular" , "artist_list" ,"artist_track_listing" ,"artist_album" ,"similar_artist","artist_details" ,"artist_details_info"]
**/
suspend fun getGaanaArtistTracks(
type: String = "artist",
subtype: String = "artist_track_listing",
seokey: String,
format: String = "JSON",
limit: Int = 50
): GaanaArtistTracks{
return httpClient.get(
"$BASE_URL/?type=$type&subtype=$subtype&seokey=$seokey&token=$TOKEN&format=$format&limit=$limit"
)
}
}
val httpClient by lazy {
HttpClient {
install(JsonFeature) {
serializer = kotlinxSerializer
}
}
}

View File

@ -1,6 +1,5 @@
package com.shabinder.common
package com.shabinder.common.spotify
import com.shabinder.common.spotify.Token
import io.ktor.client.*
import io.ktor.client.features.auth.*
import io.ktor.client.features.auth.providers.*
@ -16,19 +15,21 @@ suspend fun authenticateSpotify(): Token {
}
}
private val spotifyAuthClient = HttpClient {
val clientId = "694d8bf4f6ec420fa66ea7fb4c68f89d"
val clientSecret = "02ca2d4021a7452dae2328b47a6e8fe8"
private val spotifyAuthClient by lazy {
HttpClient {
val clientId = "694d8bf4f6ec420fa66ea7fb4c68f89d"
val clientSecret = "02ca2d4021a7452dae2328b47a6e8fe8"
install(Auth) {
basic {
sendWithoutRequest = true
username = clientId
password = clientSecret
install(Auth) {
basic {
sendWithoutRequest = true
username = clientId
password = clientSecret
}
}
install(JsonFeature) {
serializer = kotlinxSerializer
}
}
install(JsonFeature) {
serializer = kotlinxSerializer
}
}

View File

@ -0,0 +1,47 @@
package com.shabinder.common.spotify
import io.ktor.client.*
import io.ktor.client.features.json.*
import io.ktor.client.request.*
private const val BASE_URL = "https://api.spotify.com/v1/"
private val spotifyRequestsClient by lazy {
HttpClient {
install(JsonFeature) {
serializer = kotlinxSerializer
}
}
}
interface SpotifyRequests {
suspend fun getPlaylist(playlistID: String):Playlist{
return spotifyRequestsClient.get("$BASE_URL/playlists/$playlistID")
}
suspend fun getPlaylistTracks(
playlistID: String?,
offset: Int = 0,
limit: Int = 100
):PagingObjectPlaylistTrack{
return spotifyRequestsClient.get("$BASE_URL/playlists/$playlistID/tracks?offset=$offset&limit=$limit")
}
suspend fun getTrack(id: String?):Track{
return spotifyRequestsClient.get("$BASE_URL/tracks/$id")
}
suspend fun getEpisode(id: String?) :Track{
return spotifyRequestsClient.get("$BASE_URL/episodes/$id")
}
suspend fun getShow(id: String?): Track{
return spotifyRequestsClient.get("$BASE_URL/shows/$id")
}
suspend fun getAlbum(id: String):Album{
return spotifyRequestsClient.get("$BASE_URL/albums/$id")
}
}

View File

@ -0,0 +1,48 @@
package com.shabinder.common.youtube
import com.shabinder.common.gaana.httpClient
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
import kotlinx.serialization.json.putJsonObject
private const val apiKey = "AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30"
interface YoutubeMusic {
suspend fun getYoutubeMusicResponse(query: String):String{
return httpClient.post("https://music.youtube.com/youtubei/v1/search?alt=json&key=$apiKey"){
contentType(ContentType.Application.Json)
headers{
//append("Content-Type"," application/json")
append("Referer"," https://music.youtube.com/search")
}
body = buildJsonObject {
putJsonObject("context"){
putJsonObject("client"){
put("clientName" ,"WEB_REMIX")
put("clientVersion" ,"0.1")
}
}
put("query",query)
}
}
}
}
/*
fun makeJsonBody(query: String):JsonObject{
val client = JsonObject()
client["clientName"] = "WEB_REMIX"
client["clientVersion"] = "0.1"
val context = JsonObject()
context["client"] = client
val mainObject = JsonObject()
mainObject["context"] = context
mainObject["query"] = query
return mainObject
}*/