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:
parent
554af530e3
commit
b4daac0814
|
@ -155,7 +155,7 @@ class MediaFragment(private val mediaIdStr: String, mediaCr: Item = NoneItem) :
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
println("else")
|
binding.textEpisodesOrRuntime.visibility = View.GONE
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ import kotlinx.coroutines.launch
|
||||||
import kotlinx.serialization.ExperimentalSerializationApi
|
import kotlinx.serialization.ExperimentalSerializationApi
|
||||||
import org.mosad.teapod.parser.crunchyroll.*
|
import org.mosad.teapod.parser.crunchyroll.*
|
||||||
import org.mosad.teapod.preferences.Preferences
|
import org.mosad.teapod.preferences.Preferences
|
||||||
|
import org.mosad.teapod.util.DataTypes.MediaType
|
||||||
import org.mosad.teapod.util.Meta
|
import org.mosad.teapod.util.Meta
|
||||||
import org.mosad.teapod.util.tmdb.*
|
import org.mosad.teapod.util.tmdb.*
|
||||||
|
|
||||||
|
@ -29,6 +30,8 @@ class MediaFragmentViewModel(application: Application) : AndroidViewModel(applic
|
||||||
internal set
|
internal set
|
||||||
val currentEpisodesCrunchy = arrayListOf<Episode>() // used for EpisodeItemAdapter (easier updates)
|
val currentEpisodesCrunchy = arrayListOf<Episode>() // used for EpisodeItemAdapter (easier updates)
|
||||||
|
|
||||||
|
var mediaType = MediaType.OTHER
|
||||||
|
internal set
|
||||||
var tmdbResult: TMDBResult = NoneTMDB // TODO rename
|
var tmdbResult: TMDBResult = NoneTMDB // TODO rename
|
||||||
internal set
|
internal set
|
||||||
var tmdbTVSeason: TMDBTVSeason = NoneTMDBTVSeason
|
var tmdbTVSeason: TMDBTVSeason = NoneTMDBTVSeason
|
||||||
|
@ -59,6 +62,11 @@ class MediaFragmentViewModel(application: Application) : AndroidViewModel(applic
|
||||||
currentEpisodesCrunchy.addAll(episodesCrunchy.items)
|
currentEpisodesCrunchy.addAll(episodesCrunchy.items)
|
||||||
println("episodes: $episodesCrunchy")
|
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
|
// TODO check if metaDB knows the title
|
||||||
mediaMeta = null // set mediaMeta to null, if metaDB doesn't know the media
|
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() {
|
suspend fun loadTmdbInfo() {
|
||||||
val tmdbApiController = TMDBApiController()
|
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)
|
println(tmdbSearchResult)
|
||||||
|
|
||||||
tmdbResult = if (tmdbSearchResult.results.isNotEmpty()) {
|
tmdbResult = if (tmdbSearchResult.results.isNotEmpty()) {
|
||||||
val result = tmdbSearchResult.results.first()
|
when (val result = tmdbSearchResult.results.first()) {
|
||||||
|
is TMDBSearchResultMovie -> tmdbApiController.getMovieDetails(result.id)
|
||||||
when (result.mediaType) {
|
is TMDBSearchResultTVShow -> tmdbApiController.getTVShowDetails(result.id)
|
||||||
"movie" -> tmdbApiController.getMovieDetails(result.id)
|
|
||||||
"tv" -> tmdbApiController.getTVShowDetails(result.id)
|
|
||||||
else -> NoneTMDB
|
else -> NoneTMDB
|
||||||
}
|
}
|
||||||
} else NoneTMDB
|
} else NoneTMDB
|
||||||
|
|
|
@ -69,19 +69,35 @@ class TMDBApiController {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Search for a media(movie or tv show) in tmdb
|
* Search for a movie in tmdb
|
||||||
* @param query The query text
|
* @param query The query text (movie title)
|
||||||
* @return A TMDBSearch object, or NoneTMDBSearch if nothing was found
|
* @return A TMDBSearch<TMDBSearchResultMovie> object, or
|
||||||
|
* NoneTMDBSearchMovie if nothing was found
|
||||||
*/
|
*/
|
||||||
@ExperimentalSerializationApi
|
suspend fun searchMovie(query: String): TMDBSearch<TMDBSearchResultMovie> {
|
||||||
suspend fun searchMulti(query: String): TMDBSearch {
|
|
||||||
val searchEndpoint = "/search/multi"
|
val searchEndpoint = "/search/multi"
|
||||||
val parameters = listOf("query" to query, "include_adult" to false)
|
val parameters = listOf("query" to query, "include_adult" to false)
|
||||||
|
|
||||||
val result = request(searchEndpoint, parameters)
|
val result = request(searchEndpoint, parameters)
|
||||||
return result.component1()?.obj()?.let {
|
return result.component1()?.obj()?.let {
|
||||||
json.decodeFromString(it.toString())
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -22,38 +22,8 @@
|
||||||
|
|
||||||
package org.mosad.teapod.util.tmdb
|
package org.mosad.teapod.util.tmdb
|
||||||
|
|
||||||
import android.os.Build
|
import kotlinx.serialization.SerialName
|
||||||
import androidx.annotation.RequiresApi
|
import kotlinx.serialization.Serializable
|
||||||
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())
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* These data classes represent the tmdb api json objects.
|
* These data classes represent the tmdb api json objects.
|
||||||
|
@ -76,6 +46,42 @@ data class TMDBBase(
|
||||||
override val backdropPath: String?
|
override val backdropPath: String?
|
||||||
) : TMDBResult
|
) : 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
|
@Serializable
|
||||||
data class TMDBMovie(
|
data class TMDBMovie(
|
||||||
@SerialName("id") override val id: Int,
|
@SerialName("id") override val id: Int,
|
||||||
|
|
Loading…
Reference in New Issue