rework the tmdb controller
the tmdb interation now provides additional information: * tv seasons & episodes * movie & tv show (air date, status)
This commit is contained in:
parent
d417181b70
commit
44f99295e9
|
@ -25,6 +25,8 @@ import org.mosad.teapod.ui.activity.main.viewmodel.MediaFragmentViewModel
|
|||
import org.mosad.teapod.util.DataTypes.MediaType
|
||||
import org.mosad.teapod.util.Episode
|
||||
import org.mosad.teapod.util.StorageController
|
||||
import org.mosad.teapod.util.tmdb.Movie
|
||||
import org.mosad.teapod.util.tmdb.TMDBApiController
|
||||
|
||||
/**
|
||||
* The media detail fragment.
|
||||
|
@ -85,21 +87,25 @@ class MediaFragment(private val mediaId: Int) : Fragment() {
|
|||
*/
|
||||
private fun updateGUI() = with(model) {
|
||||
// generic gui
|
||||
val backdropUrl = if (tmdb.backdropUrl.isNotEmpty()) tmdb.backdropUrl else media.info.posterUrl
|
||||
val posterUrl = if (tmdb.posterUrl.isNotEmpty()) tmdb.posterUrl else media.info.posterUrl
|
||||
val backdropUrl = tmdbResult.backdropPath?.let { TMDBApiController.imageUrl + it }
|
||||
?: media.info.posterUrl
|
||||
val posterUrl = tmdbResult.posterPath?.let { TMDBApiController.imageUrl + it }
|
||||
?: media.info.posterUrl
|
||||
|
||||
// load poster and backdrop
|
||||
Glide.with(requireContext()).load(posterUrl)
|
||||
.into(binding.imagePoster)
|
||||
Glide.with(requireContext()).load(backdropUrl)
|
||||
.apply(RequestOptions.placeholderOf(ColorDrawable(Color.DKGRAY)))
|
||||
.apply(RequestOptions.bitmapTransform(BlurTransformation(20, 3)))
|
||||
.into(binding.imageBackdrop)
|
||||
|
||||
Glide.with(requireContext()).load(posterUrl)
|
||||
.into(binding.imagePoster)
|
||||
|
||||
binding.textTitle.text = media.info.title
|
||||
binding.textYear.text = media.info.year.toString()
|
||||
binding.textAge.text = media.info.age.toString()
|
||||
binding.textOverview.text = media.info.shortDesc
|
||||
|
||||
// set "my list" indicator
|
||||
if (StorageController.myList.contains(media.id)) {
|
||||
Glide.with(requireContext()).load(R.drawable.ic_baseline_check_24).into(binding.imageMyListAction)
|
||||
} else {
|
||||
|
@ -133,12 +139,13 @@ class MediaFragment(private val mediaId: Int) : Fragment() {
|
|||
fragments.add(MediaFragmentEpisodes())
|
||||
pagerAdapter.notifyDataSetChanged()
|
||||
} else if (media.type == MediaType.MOVIE) {
|
||||
val tmdbMovie = (tmdbResult as Movie)
|
||||
|
||||
if (tmdb.runtime > 0) {
|
||||
if (tmdbMovie.runtime != null) {
|
||||
binding.textEpisodesOrRuntime.text = resources.getQuantityString(
|
||||
R.plurals.text_runtime,
|
||||
tmdb.runtime,
|
||||
tmdb.runtime
|
||||
tmdbMovie.runtime,
|
||||
tmdbMovie.runtime
|
||||
)
|
||||
} else {
|
||||
binding.textEpisodesOrRuntime.visibility = View.GONE
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
package org.mosad.teapod.ui.activity.main.viewmodel
|
||||
|
||||
import android.app.Application
|
||||
import android.util.Log
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import org.mosad.teapod.parser.AoDParser
|
||||
import org.mosad.teapod.util.*
|
||||
import org.mosad.teapod.util.DataTypes.MediaType
|
||||
import org.mosad.teapod.util.tmdb.Movie
|
||||
import org.mosad.teapod.util.tmdb.TMDBApiController
|
||||
import org.mosad.teapod.util.tmdb.TMDBResult
|
||||
|
||||
/**
|
||||
* handle media, next ep and tmdb
|
||||
|
@ -15,7 +19,7 @@ class MediaFragmentViewModel(application: Application) : AndroidViewModel(applic
|
|||
internal set
|
||||
var nextEpisode = Episode()
|
||||
internal set
|
||||
var tmdb = TMDBResponse()
|
||||
lateinit var tmdbResult: TMDBResult // TODO rename
|
||||
internal set
|
||||
|
||||
/**
|
||||
|
@ -23,14 +27,32 @@ class MediaFragmentViewModel(application: Application) : AndroidViewModel(applic
|
|||
*/
|
||||
suspend fun load(mediaId: Int) {
|
||||
media = AoDParser.getMediaById(mediaId)
|
||||
tmdb = TMDBApiController().search(media.info.title, media.type)
|
||||
|
||||
val tmdbApiController = TMDBApiController()
|
||||
val searchTitle = stripTitleInfo(media.info.title)
|
||||
val tmdbId = tmdbApiController.search(searchTitle, media.type)
|
||||
|
||||
tmdbResult = when (media.type) {
|
||||
MediaType.MOVIE -> tmdbApiController.getMovieDetails(tmdbId)
|
||||
MediaType.TVSHOW -> tmdbApiController.getTVShowDetails(tmdbId)
|
||||
else -> Movie(-1)
|
||||
}
|
||||
println(tmdbResult) // TODO
|
||||
|
||||
// TESTING
|
||||
if (media.type == MediaType.TVSHOW) {
|
||||
val seasonNumber = guessSeasonFromTitle(media.info.title)
|
||||
Log.d("test", "season number: $seasonNumber")
|
||||
|
||||
// TODO Important: only use tmdb info if media title and episode number match exactly
|
||||
val tmdbTVSeason = tmdbApiController.getTVSeasonDetails(tmdbId, seasonNumber)
|
||||
Log.d("test", "Season Info: $tmdbTVSeason.")
|
||||
}
|
||||
|
||||
// TESTING END
|
||||
|
||||
if (media.type == MediaType.TVSHOW) {
|
||||
nextEpisode = if (media.episodes.firstOrNull{ !it.watched } != null) {
|
||||
media.episodes.first{ !it.watched }
|
||||
} else {
|
||||
media.episodes.first()
|
||||
}
|
||||
nextEpisode = media.episodes.firstOrNull{ !it.watched } ?: media.episodes.first()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -45,4 +67,30 @@ class MediaFragmentViewModel(application: Application) : AndroidViewModel(applic
|
|||
?: media.episodes.first()
|
||||
}
|
||||
|
||||
// remove unneeded info from the media title before searching
|
||||
private fun stripTitleInfo(title: String): String {
|
||||
return title.replace("(Sub)", "")
|
||||
.replace(Regex("-?\\s?[0-9]+.\\s?(Staffel|Season)"), "")
|
||||
.replace(Regex("(Staffel|Season)\\s?[0-9]+"), "")
|
||||
.trim()
|
||||
}
|
||||
|
||||
/** guess Season from title
|
||||
* if the title ends with a number, that could be the season
|
||||
* if the title ends with Regex("-?\\s?[0-9]+.\\s?(Staffel|Season)") or
|
||||
* Regex("(Staffel|Season)\\s?[0-9]+"), that is the season information
|
||||
*/
|
||||
private fun guessSeasonFromTitle(title: String): Int {
|
||||
val helpTitle = title.replace("(Sub)", "").trim()
|
||||
Log.d("test", "helpTitle: $helpTitle")
|
||||
|
||||
return if (helpTitle.last().isDigit()) {
|
||||
helpTitle.last().digitToInt()
|
||||
} else {
|
||||
Regex("([0-9]+.\\s?(Staffel|Season))|((Staffel|Season)\\s?[0-9]+)")
|
||||
.find(helpTitle)
|
||||
?.value?.filter { it.isDigit() }?.toInt() ?: 1
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -98,18 +98,6 @@ data class Stream(
|
|||
val language : Locale
|
||||
)
|
||||
|
||||
/**
|
||||
* this class is used for tmdb responses
|
||||
*/
|
||||
data class TMDBResponse(
|
||||
val id: Int = 0,
|
||||
val title: String = "",
|
||||
val overview: String = "",
|
||||
val posterUrl: String = "",
|
||||
val backdropUrl: String = "",
|
||||
val runtime: Int = 0
|
||||
)
|
||||
|
||||
/**
|
||||
* this class is used to represent the aod json API?
|
||||
*/
|
||||
|
|
|
@ -1,121 +0,0 @@
|
|||
package org.mosad.teapod.util
|
||||
|
||||
import android.util.Log
|
||||
import com.google.gson.JsonObject
|
||||
import com.google.gson.JsonParser
|
||||
import kotlinx.coroutines.*
|
||||
import org.mosad.teapod.util.DataTypes.MediaType
|
||||
import java.net.URL
|
||||
import java.net.URLEncoder
|
||||
|
||||
class TMDBApiController {
|
||||
|
||||
private val apiUrl = "https://api.themoviedb.org/3"
|
||||
private val searchMovieUrl = "$apiUrl/search/movie"
|
||||
private val searchTVUrl = "$apiUrl/search/tv"
|
||||
private val getMovieUrl = "$apiUrl/movie"
|
||||
private val apiKey = "de959cf9c07a08b5ca7cb51cda9a40c2"
|
||||
private val language = "de"
|
||||
private val preparedParameters = "?api_key=$apiKey&language=$language"
|
||||
|
||||
private val imageUrl = "https://image.tmdb.org/t/p/w500"
|
||||
|
||||
suspend fun search(title: String, type: MediaType): TMDBResponse {
|
||||
// remove unneeded text from the media title before searching
|
||||
val searchTerm = title.replace("(Sub)", "")
|
||||
.replace(Regex("-?\\s?[0-9]+.\\s?(Staffel|Season)"), "")
|
||||
.replace(Regex("(Staffel|Season)\\s?[0-9]+"), "")
|
||||
.trim()
|
||||
|
||||
return when (type) {
|
||||
MediaType.MOVIE -> searchMovie(searchTerm)
|
||||
MediaType.TVSHOW -> searchTVShow(searchTerm)
|
||||
else -> {
|
||||
Log.e(javaClass.name, "Wrong Type: $type")
|
||||
TMDBResponse()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
private suspend fun searchTVShow(title: String): TMDBResponse = withContext(Dispatchers.IO) {
|
||||
val url = URL("$searchTVUrl$preparedParameters&query=${URLEncoder.encode(title, "UTF-8")}")
|
||||
val response = JsonParser.parseString(url.readText()).asJsonObject
|
||||
// println(response)
|
||||
|
||||
val sortedResults = response.get("results").asJsonArray.toList().sortedBy {
|
||||
getStringNotNull(it.asJsonObject, "name")
|
||||
}
|
||||
|
||||
return@withContext if (sortedResults.isNotEmpty()) {
|
||||
sortedResults.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)
|
||||
|
||||
TMDBResponse(id, "", overview, posterPath, backdropPath)
|
||||
}
|
||||
} else {
|
||||
TMDBResponse()
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
private suspend fun searchMovie(title: String): TMDBResponse = withContext(Dispatchers.IO) {
|
||||
val url = URL("$searchMovieUrl$preparedParameters&query=${URLEncoder.encode(title, "UTF-8")}")
|
||||
val response = JsonParser.parseString(url.readText()).asJsonObject
|
||||
// println(response)
|
||||
|
||||
val sortedResults = response.get("results").asJsonArray.toList().sortedBy {
|
||||
getStringNotNull(it.asJsonObject, "title")
|
||||
}
|
||||
|
||||
return@withContext if (sortedResults.isNotEmpty()) {
|
||||
sortedResults.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)
|
||||
}
|
||||
} else {
|
||||
TMDBResponse()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* currently only used for runtime, need a rework
|
||||
*/
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
suspend fun getMovieRuntime(id: Int): Int = withContext(Dispatchers.IO) {
|
||||
val url = URL("$getMovieUrl/$id?api_key=$apiKey&language=$language")
|
||||
|
||||
val response = JsonParser.parseString(url.readText()).asJsonObject
|
||||
return@withContext getStringNotNull(response,"runtime").toInt()
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,139 @@
|
|||
package org.mosad.teapod.util.tmdb
|
||||
|
||||
import android.util.Log
|
||||
import com.google.gson.JsonParser
|
||||
import kotlinx.coroutines.*
|
||||
import org.mosad.teapod.util.DataTypes.MediaType
|
||||
import java.io.FileNotFoundException
|
||||
import java.net.URL
|
||||
import java.net.URLEncoder
|
||||
|
||||
// TODO use Klaxon?
|
||||
class TMDBApiController {
|
||||
|
||||
private val apiUrl = "https://api.themoviedb.org/3"
|
||||
private val searchMovieUrl = "$apiUrl/search/movie"
|
||||
private val searchTVUrl = "$apiUrl/search/tv"
|
||||
private val detailsMovieUrl = "$apiUrl/movie"
|
||||
private val detailsTVUrl = "$apiUrl/tv"
|
||||
private val apiKey = "de959cf9c07a08b5ca7cb51cda9a40c2"
|
||||
private val language = "de"
|
||||
private val preparedParameters = "?api_key=$apiKey&language=$language"
|
||||
|
||||
companion object{
|
||||
const val imageUrl = "https://image.tmdb.org/t/p/w500"
|
||||
}
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
suspend fun search(query: String, type: MediaType): Int = withContext(Dispatchers.IO) {
|
||||
val searchUrl = when (type) {
|
||||
MediaType.MOVIE -> searchMovieUrl
|
||||
MediaType.TVSHOW -> searchTVUrl
|
||||
else -> {
|
||||
Log.e(javaClass.name, "Wrong Type: $type")
|
||||
return@withContext -1
|
||||
}
|
||||
}
|
||||
|
||||
val url = URL("$searchUrl$preparedParameters&query=${URLEncoder.encode(query, "UTF-8")}")
|
||||
val response = JsonParser.parseString(url.readText()).asJsonObject
|
||||
val sortedResults = response.get("results").asJsonArray.toList().sortedBy {
|
||||
it.asJsonObject.get("title")?.asString
|
||||
}
|
||||
|
||||
return@withContext sortedResults.first().asJsonObject?.get("id")?.asInt ?: -1
|
||||
}
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
suspend fun getMovieDetails(movieId: Int): Movie = withContext(Dispatchers.IO) {
|
||||
val url = URL("$detailsMovieUrl/$movieId?api_key=$apiKey&language=$language")
|
||||
|
||||
val response = try {
|
||||
JsonParser.parseString(url.readText()).asJsonObject
|
||||
} catch (ex: FileNotFoundException) {
|
||||
Log.w(javaClass.name, "The resource you requested could not be found")
|
||||
return@withContext Movie(-1)
|
||||
}
|
||||
|
||||
return@withContext try {
|
||||
Movie(
|
||||
id = response.get("id").asInt,
|
||||
name = response.get("title")?.asString,
|
||||
overview = response.get("overview")?.asString,
|
||||
posterPath = response.get("poster_path")?.asString,
|
||||
backdropPath = response.get("backdrop_path")?.asString,
|
||||
releaseDate = response.get("release_date")?.asString,
|
||||
runtime = response.get("runtime")?.asInt
|
||||
)
|
||||
} catch (ex: Exception) {
|
||||
Log.w(javaClass.name, "Error", ex)
|
||||
Movie(-1)
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
suspend fun getTVShowDetails(tvId: Int): TVShow = withContext(Dispatchers.IO) {
|
||||
val url = URL("$detailsTVUrl/$tvId?api_key=$apiKey&language=$language")
|
||||
|
||||
val response = try {
|
||||
JsonParser.parseString(url.readText()).asJsonObject
|
||||
} catch (ex: FileNotFoundException) {
|
||||
Log.w(javaClass.name, "The resource you requested could not be found")
|
||||
return@withContext TVShow(-1)
|
||||
}
|
||||
|
||||
return@withContext try {
|
||||
TVShow(
|
||||
id = response.get("id").asInt,
|
||||
name = response.get("name")?.asString,
|
||||
overview = response.get("overview")?.asString,
|
||||
posterPath = response.get("poster_path")?.asString,
|
||||
backdropPath = response.get("backdrop_path")?.asString,
|
||||
firstAirDate = response.get("first_air_date")?.asString,
|
||||
status = response.get("status")?.asString
|
||||
)
|
||||
} catch (ex: Exception) {
|
||||
Log.w(javaClass.name, "Error", ex)
|
||||
TVShow(-1)
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
suspend fun getTVSeasonDetails(tvId: Int, seasonNumber: Int): TVSeason = withContext(Dispatchers.IO) {
|
||||
val url = URL("$detailsTVUrl/$tvId/season/$seasonNumber?api_key=$apiKey&language=$language")
|
||||
|
||||
val response = try {
|
||||
JsonParser.parseString(url.readText()).asJsonObject
|
||||
} catch (ex: FileNotFoundException) {
|
||||
Log.w(javaClass.name, "The resource you requested could not be found")
|
||||
return@withContext TVSeason(-1)
|
||||
}
|
||||
// println(response)
|
||||
|
||||
return@withContext try {
|
||||
val episodes = response.get("episodes").asJsonArray.map {
|
||||
TVEpisode(
|
||||
id = it.asJsonObject.get("id").asInt,
|
||||
name = it.asJsonObject.get("name")?.asString,
|
||||
overview = it.asJsonObject.get("overview")?.asString,
|
||||
airDate = it.asJsonObject.get("air_date")?.asString,
|
||||
episodeNumber = it.asJsonObject.get("episode_number")?.asInt
|
||||
)
|
||||
}
|
||||
|
||||
TVSeason(
|
||||
id = response.get("id").asInt,
|
||||
name = response.asJsonObject.get("name")?.asString,
|
||||
overview = response.asJsonObject.get("overview")?.asString,
|
||||
posterPath = response.asJsonObject.get("poster_path")?.asString,
|
||||
airDate = response.asJsonObject.get("air_date")?.asString,
|
||||
episodes = episodes,
|
||||
seasonNumber = response.get("season_number")?.asInt
|
||||
)
|
||||
} catch (ex: Exception) {
|
||||
Log.w(javaClass.name, "Error", ex)
|
||||
TVSeason(-1)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package org.mosad.teapod.util.tmdb
|
||||
|
||||
abstract class TMDBResult{
|
||||
abstract val id: Int
|
||||
abstract val name: String?
|
||||
abstract val overview: String?
|
||||
abstract val posterPath: String?
|
||||
abstract val backdropPath: String?
|
||||
}
|
||||
|
||||
data class Movie(
|
||||
override val id: Int,
|
||||
override val name: String? = null,
|
||||
override val overview: String? = null,
|
||||
override val posterPath: String? = null,
|
||||
override val backdropPath: String? = null,
|
||||
val releaseDate: String? = null,
|
||||
val runtime: Int? = null
|
||||
// TODO generes
|
||||
): TMDBResult()
|
||||
|
||||
data class TVShow(
|
||||
override val id: Int,
|
||||
override val name: String? = null,
|
||||
override val overview: String? = null,
|
||||
override val posterPath: String? = null,
|
||||
override val backdropPath: String? = null,
|
||||
val firstAirDate: String? = null,
|
||||
val status: String? = null,
|
||||
// TODO generes
|
||||
): TMDBResult()
|
||||
|
||||
data class TVSeason(
|
||||
val id: Int,
|
||||
val name: String? = null,
|
||||
val overview: String? = null,
|
||||
val posterPath: String? = null,
|
||||
val airDate: String? = null,
|
||||
val episodes: List<TVEpisode>? = null,
|
||||
val seasonNumber: Int? = null
|
||||
)
|
||||
|
||||
// TODO decide whether to use nullable or not
|
||||
data class TVEpisode(
|
||||
val id: Int,
|
||||
val name: String? = null,
|
||||
val overview: String? = null,
|
||||
val airDate: String? = null,
|
||||
val episodeNumber: Int? = null
|
||||
)
|
Loading…
Reference in New Issue