2021-07-17 13:15:14 +02:00
/ * *
* Teapod
*
2022-01-02 17:59:23 +01:00
* Copyright 2020 - 2022 < seil0 @mosad . xyz >
2021-07-17 13:15:14 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
*
* /
2021-07-10 23:37:16 +02:00
package org.mosad.teapod.util.tmdb
2021-12-31 16:03:15 +01:00
import com.github.kittinunf.fuel.Fuel
import com.github.kittinunf.fuel.core.FuelError
import com.github.kittinunf.fuel.core.Parameters
import com.github.kittinunf.fuel.json.FuelJson
import com.github.kittinunf.fuel.json.responseJson
import com.github.kittinunf.result.Result
2021-07-10 23:37:16 +02:00
import kotlinx.coroutines.*
2021-12-31 16:03:15 +01:00
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import org.mosad.teapod.util.concatenate
2021-07-10 23:37:16 +02:00
2021-07-17 13:15:14 +02:00
/ * *
* Controller for tmdb api integration .
* Data types are in TMDBDataTypes . For the type definitions see :
* https : //developers.themoviedb.org/3/getting-started/introduction
*
* /
2021-07-10 23:37:16 +02:00
class TMDBApiController {
2021-12-31 16:03:15 +01:00
private val json = Json { ignoreUnknownKeys = true }
2021-07-10 23:37:16 +02:00
private val apiUrl = " https://api.themoviedb.org/3 "
private val apiKey = " de959cf9c07a08b5ca7cb51cda9a40c2 "
private val language = " de "
companion object {
const val imageUrl = " https://image.tmdb.org/t/p/w500 "
}
2021-12-31 16:03:15 +01:00
private suspend fun request (
endpoint : String ,
parameters : Parameters = emptyList ( )
) : Result < FuelJson , FuelError > = coroutineScope {
val path = " $apiUrl $endpoint "
val params = concatenate ( listOf ( " api_key " to apiKey , " language " to language ) , parameters )
// TODO handle FileNotFoundException
return @coroutineScope ( Dispatchers . IO ) {
val ( _ , _ , result ) = Fuel . get ( path , params )
. responseJson ( )
result
}
}
2021-07-17 13:15:14 +02:00
/ * *
2022-01-02 17:51:45 +01:00
* Search for a movie in tmdb
* @param query The query text ( movie title )
* @return A TMDBSearch < TMDBSearchResultMovie > object , or
* NoneTMDBSearchMovie if nothing was found
2021-07-17 13:15:14 +02:00
* /
2022-01-02 17:51:45 +01:00
suspend fun searchMovie ( query : String ) : TMDBSearch < TMDBSearchResultMovie > {
2021-12-31 16:03:15 +01:00
val searchEndpoint = " /search/multi "
val parameters = listOf ( " query " to query , " include_adult " to false )
2021-07-10 23:37:16 +02:00
2021-12-31 16:03:15 +01:00
val result = request ( searchEndpoint , parameters )
return result . component1 ( ) ?. obj ( ) ?. let {
json . decodeFromString ( it . toString ( ) )
2022-01-02 17:51:45 +01:00
} ?: NoneTMDBSearchMovie
}
/ * *
* Search for a tv show in tmdb
* @param query The query text ( tv show title )
* @return A TMDBSearch < TMDBSearchResultTVShow > object , or
* NoneTMDBSearchTVShow if nothing was found
* /
suspend fun searchTVShow ( query : String ) : TMDBSearch < TMDBSearchResultTVShow > {
val searchEndpoint = " /search/tv "
val parameters = listOf ( " query " to query , " include_adult " to false )
val result = request ( searchEndpoint , parameters )
return result . component1 ( ) ?. obj ( ) ?. let {
json . decodeFromString ( it . toString ( ) )
} ?: NoneTMDBSearchTVShow
2021-07-10 23:37:16 +02:00
}
2021-07-17 13:15:14 +02:00
/ * *
* Get details for a movie from tmdb
* @param movieId The tmdb ID of the movie
2021-12-31 16:03:15 +01:00
* @return A TMDBMovie object , or NoneTMDBMovie if not found
2021-07-17 13:15:14 +02:00
* /
2021-12-31 16:03:15 +01:00
suspend fun getMovieDetails ( movieId : Int ) : TMDBMovie {
val movieEndpoint = " /movie/ $movieId "
// TODO is FileNotFoundException handling needed?
val result = request ( movieEndpoint )
return result . component1 ( ) ?. obj ( ) ?. let {
json . decodeFromString ( it . toString ( ) )
} ?: NoneTMDBMovie
2021-07-10 23:37:16 +02:00
}
2021-07-17 13:15:14 +02:00
/ * *
* Get details for a tv show from tmdb
* @param tvId The tmdb ID of the tv show
2021-12-31 16:03:15 +01:00
* @return A TMDBTVShow object , or NoneTMDBTVShow if not found
2021-07-17 13:15:14 +02:00
* /
2021-12-31 16:03:15 +01:00
suspend fun getTVShowDetails ( tvId : Int ) : TMDBTVShow {
val tvShowEndpoint = " /tv/ $tvId "
// TODO is FileNotFoundException handling needed?
val result = request ( tvShowEndpoint )
return result . component1 ( ) ?. obj ( ) ?. let {
json . decodeFromString ( it . toString ( ) )
} ?: NoneTMDBTVShow
2021-07-10 23:37:16 +02:00
}
2021-12-31 16:03:15 +01:00
@Suppress ( " unused " )
2021-07-17 13:15:14 +02:00
/ * *
* Get details for a tv show season from tmdb
* @param tvId The tmdb ID of the tv show
* @param seasonNumber The tmdb season number
2021-12-31 16:03:15 +01:00
* @return A TMDBTVSeason object , or NoneTMDBTVSeason if not found
2021-07-17 13:15:14 +02:00
* /
2021-12-31 16:03:15 +01:00
suspend fun getTVSeasonDetails ( tvId : Int , seasonNumber : Int ) : TMDBTVSeason {
val tvShowSeasonEndpoint = " /tv/ $tvId /season/ $seasonNumber "
// TODO is FileNotFoundException handling needed?
val result = request ( tvShowSeasonEndpoint )
return result . component1 ( ) ?. obj ( ) ?. let {
json . decodeFromString ( it . toString ( ) )
} ?: NoneTMDBTVSeason
2021-07-10 23:37:16 +02:00
}
2021-12-31 16:03:15 +01:00
}