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:
@ -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
|
||||
)
|
Reference in New Issue
Block a user