2020-10-12 20:30:45 +02:00
package org.mosad.teapod.util
import android.util.Log
2020-10-12 22:43:42 +02:00
import com.google.gson.JsonObject
2020-10-12 20:30:45 +02:00
import com.google.gson.JsonParser
2020-11-27 11:06:16 +01:00
import kotlinx.coroutines.*
2020-10-12 20:30:45 +02:00
import java.net.URL
import java.net.URLEncoder
import org.mosad.teapod.util.DataTypes.MediaType
class TMDBApiController {
private val apiUrl = " https://api.themoviedb.org/3 "
private val searchMovieUrl = " $apiUrl /search/movie "
private val searchTVUrl = " $apiUrl /search/tv "
2020-10-13 15:56:07 +02:00
private val getMovieUrl = " $apiUrl /movie "
2020-10-12 20:30:45 +02:00
private val apiKey = " de959cf9c07a08b5ca7cb51cda9a40c2 "
private val language = " de "
2020-10-12 22:43:42 +02:00
private val preparedParameters = " ?api_key= $apiKey &language= $language "
2020-10-12 20:30:45 +02:00
private val imageUrl = " https://image.tmdb.org/t/p/w500 "
2020-11-27 11:06:16 +01:00
suspend fun search ( title : String , type : MediaType ) : TMDBResponse {
2020-10-12 22:43:42 +02:00
val searchTerm = title . replace ( " (Sub) " , " " ) . trim ( )
2020-10-12 20:30:45 +02:00
return when ( type ) {
2021-06-06 17:54:19 +02:00
MediaType . MOVIE -> searchMovie ( searchTerm )
MediaType . TVSHOW -> searchTVShow ( searchTerm )
2020-10-12 22:43:42 +02:00
else -> {
Log . e ( javaClass . name , " Wrong Type: $type " )
2020-10-12 20:30:45 +02:00
TMDBResponse ( )
}
}
}
2021-06-06 17:54:19 +02:00
@Suppress ( " BlockingMethodInNonBlockingContext " )
private suspend fun searchTVShow ( title : String ) : TMDBResponse = withContext ( Dispatchers . IO ) {
2020-10-12 22:43:42 +02:00
val url = URL ( " $searchTVUrl $preparedParameters &query= ${URLEncoder.encode(title, "UTF-8")} " )
2021-06-06 17:54:19 +02:00
val response = JsonParser . parseString ( url . readText ( ) ) . asJsonObject
//println(response)
2020-10-12 20:30:45 +02:00
2021-06-06 17:54:19 +02:00
return @withContext if ( response . get ( " total_results " ) . asInt > 0 ) {
response . get ( " results " ) . asJsonArray . first ( ) . asJsonObject . let {
val id = getStringNotNull ( it , " id " ) . toInt ( )
val overview = getStringNotNull ( it , " overview " )
val posterPath = getStringNotNullPrefix ( it , " poster_path " , imageUrl )
val backdropPath = getStringNotNullPrefix ( it , " backdrop_path " , imageUrl )
2020-10-12 20:30:45 +02:00
2021-06-06 17:54:19 +02:00
TMDBResponse ( id , " " , overview , posterPath , backdropPath )
2020-10-12 20:30:45 +02:00
}
2021-06-06 17:54:19 +02:00
} else {
TMDBResponse ( )
2020-11-27 11:06:16 +01:00
}
2020-10-12 20:30:45 +02:00
}
2021-06-06 17:54:19 +02:00
@Suppress ( " BlockingMethodInNonBlockingContext " )
private suspend fun searchMovie ( title : String ) : TMDBResponse = withContext ( Dispatchers . IO ) {
2020-10-12 22:43:42 +02:00
val url = URL ( " $searchMovieUrl $preparedParameters &query= ${URLEncoder.encode(title, "UTF-8")} " )
2021-06-06 17:54:19 +02:00
val response = JsonParser . parseString ( url . readText ( ) ) . asJsonObject
//println(response)
return @withContext if ( response . get ( " total_results " ) . asInt > 0 ) {
response . get ( " results " ) . asJsonArray . first ( ) . asJsonObject . let {
val id = getStringNotNull ( it , " id " ) . toInt ( )
val overview = getStringNotNull ( it , " overview " )
val posterPath = getStringNotNullPrefix ( it , " poster_path " , imageUrl )
val backdropPath = getStringNotNullPrefix ( it , " backdrop_path " , imageUrl )
val runtime = getMovieRuntime ( id )
TMDBResponse ( id , " " , overview , posterPath , backdropPath , runtime )
2020-10-12 20:30:45 +02:00
}
2021-06-06 17:54:19 +02:00
} else {
TMDBResponse ( )
2020-11-27 11:06:16 +01:00
}
2020-10-12 20:30:45 +02:00
}
2020-10-13 15:56:07 +02:00
/ * *
* currently only used for runtime , need a rework
* /
2021-06-06 17:54:19 +02:00
@Suppress ( " BlockingMethodInNonBlockingContext " )
suspend fun getMovieRuntime ( id : Int ) : Int = withContext ( Dispatchers . IO ) {
2020-10-13 15:56:07 +02:00
val url = URL ( " $getMovieUrl / $id ?api_key= $apiKey &language= $language " )
2021-06-06 17:54:19 +02:00
val response = JsonParser . parseString ( url . readText ( ) ) . asJsonObject
return @withContext getStringNotNull ( response , " runtime " ) . toInt ( )
2020-10-13 15:56:07 +02:00
}
2020-10-12 22:43:42 +02:00
/ * *
* return memberName as string if it ' s not JsonNull ,
* else return an empty string
* /
private fun getStringNotNull ( jsonObject : JsonObject , memberName : String ) : String {
return getStringNotNullPrefix ( jsonObject , memberName , " " )
}
/ * *
* return memberName as string with a prefix if it ' s not JsonNull ,
* else return an empty string
* /
private fun getStringNotNullPrefix ( jsonObject : JsonObject , memberName : String , prefix : String ) : String {
return if ( ! jsonObject . get ( memberName ) . isJsonNull ) {
prefix + jsonObject . get ( memberName ) . asString
} else {
" "
}
}
2020-10-12 20:30:45 +02:00
}