replace tmdb multi search with type search (movie/tv)

multi search often retuns a wrong result, therfore use movie or tv show search
This commit is contained in:
Jannik 2022-01-02 17:51:45 +01:00
parent 554af530e3
commit b4daac0814
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
4 changed files with 77 additions and 45 deletions

View File

@ -155,7 +155,7 @@ class MediaFragment(private val mediaIdStr: String, mediaCr: Item = NoneItem) :
}
}
else -> {
println("else")
binding.textEpisodesOrRuntime.visibility = View.GONE
}
}

View File

@ -8,6 +8,7 @@ import kotlinx.coroutines.launch
import kotlinx.serialization.ExperimentalSerializationApi
import org.mosad.teapod.parser.crunchyroll.*
import org.mosad.teapod.preferences.Preferences
import org.mosad.teapod.util.DataTypes.MediaType
import org.mosad.teapod.util.Meta
import org.mosad.teapod.util.tmdb.*
@ -29,6 +30,8 @@ class MediaFragmentViewModel(application: Application) : AndroidViewModel(applic
internal set
val currentEpisodesCrunchy = arrayListOf<Episode>() // used for EpisodeItemAdapter (easier updates)
var mediaType = MediaType.OTHER
internal set
var tmdbResult: TMDBResult = NoneTMDB // TODO rename
internal set
var tmdbTVSeason: TMDBTVSeason = NoneTMDBTVSeason
@ -59,6 +62,11 @@ class MediaFragmentViewModel(application: Application) : AndroidViewModel(applic
currentEpisodesCrunchy.addAll(episodesCrunchy.items)
println("episodes: $episodesCrunchy")
// set media type
mediaType = episodesCrunchy.items.firstOrNull()?.let {
if (it.episodeNumber != null) MediaType.TVSHOW else MediaType.MOVIE
} ?: MediaType.OTHER
// TODO check if metaDB knows the title
mediaMeta = null // set mediaMeta to null, if metaDB doesn't know the media
@ -94,15 +102,17 @@ class MediaFragmentViewModel(application: Application) : AndroidViewModel(applic
suspend fun loadTmdbInfo() {
val tmdbApiController = TMDBApiController()
val tmdbSearchResult = tmdbApiController.searchMulti(seriesCrunchy.title)
val tmdbSearchResult = when(mediaType) {
MediaType.MOVIE -> tmdbApiController.searchMovie(seriesCrunchy.title)
MediaType.TVSHOW -> tmdbApiController.searchTVShow(seriesCrunchy.title)
else -> NoneTMDBSearch
}
println(tmdbSearchResult)
tmdbResult = if (tmdbSearchResult.results.isNotEmpty()) {
val result = tmdbSearchResult.results.first()
when (result.mediaType) {
"movie" -> tmdbApiController.getMovieDetails(result.id)
"tv" -> tmdbApiController.getTVShowDetails(result.id)
when (val result = tmdbSearchResult.results.first()) {
is TMDBSearchResultMovie -> tmdbApiController.getMovieDetails(result.id)
is TMDBSearchResultTVShow -> tmdbApiController.getTVShowDetails(result.id)
else -> NoneTMDB
}
} else NoneTMDB

View File

@ -69,19 +69,35 @@ class TMDBApiController {
}
/**
* Search for a media(movie or tv show) in tmdb
* @param query The query text
* @return A TMDBSearch object, or NoneTMDBSearch if nothing was found
* Search for a movie in tmdb
* @param query The query text (movie title)
* @return A TMDBSearch<TMDBSearchResultMovie> object, or
* NoneTMDBSearchMovie if nothing was found
*/
@ExperimentalSerializationApi
suspend fun searchMulti(query: String): TMDBSearch {
suspend fun searchMovie(query: String): TMDBSearch<TMDBSearchResultMovie> {
val searchEndpoint = "/search/multi"
val parameters = listOf("query" to query, "include_adult" to false)
val result = request(searchEndpoint, parameters)
return result.component1()?.obj()?.let {
json.decodeFromString(it.toString())
} ?: NoneTMDBSearch
} ?: 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
}
/**

View File

@ -22,38 +22,8 @@
package org.mosad.teapod.util.tmdb
import android.os.Build
import androidx.annotation.RequiresApi
import kotlinx.serialization.*
import kotlinx.serialization.json.JsonNames
import java.text.DateFormat
import java.time.LocalDate
import java.util.*
/**
* New TMDB API data classes
*/
@ExperimentalSerializationApi
@Serializable
data class TMDBSearch(
val page: Int,
val results: List<TMDBSearchResult>
)
@ExperimentalSerializationApi
@Serializable
data class TMDBSearchResult(
@SerialName("id") val id: Int,
@SerialName("media_type") val mediaType: String,
@JsonNames("name", "title") val name: String, // tv show = name, movie = title
@SerialName("overview") val overview: String?,
@SerialName("poster_path") val posterPath: String?,
@SerialName("backdrop_path") val backdropPath: String?,
)
@ExperimentalSerializationApi
val NoneTMDBSearch = TMDBSearch(0, emptyList())
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
/**
* These data classes represent the tmdb api json objects.
@ -76,6 +46,42 @@ data class TMDBBase(
override val backdropPath: String?
) : TMDBResult
/**
* search results for movie and tv show
*/
@Serializable
data class TMDBSearch<T>(
val page: Int,
val results: List<T>
)
@Serializable
data class TMDBSearchResultMovie(
@SerialName("id") override val id: Int,
@SerialName("title") override val name: String,
@SerialName("overview") override val overview: String?,
@SerialName("poster_path") override val posterPath: String?,
@SerialName("backdrop_path") override val backdropPath: String?,
) : TMDBResult
@Serializable
data class TMDBSearchResultTVShow(
@SerialName("id") override val id: Int,
@SerialName("name") override val name: String,
@SerialName("overview") override val overview: String?,
@SerialName("poster_path") override val posterPath: String?,
@SerialName("backdrop_path") override val backdropPath: String?,
) : TMDBResult
val NoneTMDBSearch = TMDBSearch<TMDBBase>(0, emptyList())
val NoneTMDBSearchMovie = TMDBSearch<TMDBSearchResultMovie>(0, emptyList())
val NoneTMDBSearchTVShow = TMDBSearch<TMDBSearchResultTVShow>(0, emptyList())
/**
* detail return data types
*/
@Serializable
data class TMDBMovie(
@SerialName("id") override val id: Int,