use tmdb data if missing on aod
* episode description
This commit is contained in:
119
app/src/main/java/org/mosad/teapod/util/MetaDBController.kt
Normal file
119
app/src/main/java/org/mosad/teapod/util/MetaDBController.kt
Normal file
@ -0,0 +1,119 @@
|
||||
package org.mosad.teapod.util
|
||||
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import kotlinx.coroutines.*
|
||||
import java.io.FileNotFoundException
|
||||
import java.net.URL
|
||||
|
||||
class MetaDBController {
|
||||
|
||||
companion object {
|
||||
private const val repoUrl = "https://gitlab.com/Seil0/teapodmetadb/-/raw/main/aod/"
|
||||
|
||||
var mediaList = MediaList(listOf())
|
||||
private var metaCacheList = arrayListOf<Meta>()
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
suspend fun list() = withContext(Dispatchers.IO) {
|
||||
val url = URL("$repoUrl/list.json")
|
||||
val json = url.readText()
|
||||
|
||||
Thread.sleep(5000)
|
||||
|
||||
mediaList = Gson().fromJson(json, MediaList::class.java)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getMovieMetadata(aodId: Int): MovieMeta? {
|
||||
return metaCacheList.firstOrNull {
|
||||
it.aodId == aodId
|
||||
} as MovieMeta? ?: getMovieMetadata2(aodId)
|
||||
}
|
||||
|
||||
suspend fun getTVShowMetadata(aodId: Int): TVShowMeta? {
|
||||
return metaCacheList.firstOrNull {
|
||||
it.aodId == aodId
|
||||
} as TVShowMeta? ?: getTVShowMetadata2(aodId)
|
||||
}
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
private suspend fun getMovieMetadata2(aodId: Int): MovieMeta? = withContext(Dispatchers.IO) {
|
||||
val url = URL("$repoUrl/movie/$aodId/media.json")
|
||||
return@withContext try {
|
||||
val json = url.readText()
|
||||
val meta = Gson().fromJson(json, MovieMeta::class.java)
|
||||
metaCacheList.add(meta)
|
||||
|
||||
meta
|
||||
} catch (ex: FileNotFoundException) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
private suspend fun getTVShowMetadata2(aodId: Int): TVShowMeta? = withContext(Dispatchers.IO) {
|
||||
val url = URL("$repoUrl/tv/$aodId/media.json")
|
||||
return@withContext try {
|
||||
val json = url.readText()
|
||||
val meta = Gson().fromJson(json, TVShowMeta::class.java)
|
||||
metaCacheList.add(meta)
|
||||
|
||||
meta
|
||||
} catch (ex: FileNotFoundException) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// TODO move data classes
|
||||
data class MediaList(
|
||||
val media: List<Int>
|
||||
)
|
||||
|
||||
abstract class Meta {
|
||||
abstract val id: Int
|
||||
abstract val aodId: Int
|
||||
abstract val tmdbId: Int
|
||||
}
|
||||
|
||||
data class MovieMeta(
|
||||
override val id: Int,
|
||||
@SerializedName("aod_id")
|
||||
override val aodId: Int,
|
||||
@SerializedName("tmdb_id")
|
||||
override val tmdbId: Int
|
||||
): Meta()
|
||||
|
||||
data class TVShowMeta(
|
||||
override val id: Int,
|
||||
@SerializedName("aod_id")
|
||||
override val aodId: Int,
|
||||
@SerializedName("tmdb_id")
|
||||
override val tmdbId: Int,
|
||||
@SerializedName("tmdb_season_id")
|
||||
val tmdbSeasonId: Int,
|
||||
@SerializedName("tmdb_season_number")
|
||||
val tmdbSeasonNumber: Int,
|
||||
@SerializedName("episodes")
|
||||
val episodes: List<EpisodeMeta>
|
||||
): Meta()
|
||||
|
||||
data class EpisodeMeta(
|
||||
val id: Int,
|
||||
@SerializedName("aod_media_id")
|
||||
val aodMediaId: Int,
|
||||
@SerializedName("tmdb_id")
|
||||
val tmdbId: Int,
|
||||
@SerializedName("tmdb_number")
|
||||
val tmdbNumber: Int,
|
||||
@SerializedName("opening_start")
|
||||
val openingStart: Int,
|
||||
@SerializedName("opening_duration")
|
||||
val openingDuration: Int,
|
||||
@SerializedName("ending_start")
|
||||
val endingStart: Int,
|
||||
@SerializedName("ending_duration")
|
||||
val endingDuration: Int
|
||||
)
|
@ -12,8 +12,9 @@ import jp.wasabeef.glide.transformations.RoundedCornersTransformation
|
||||
import org.mosad.teapod.R
|
||||
import org.mosad.teapod.databinding.ItemEpisodeBinding
|
||||
import org.mosad.teapod.util.Episode
|
||||
import org.mosad.teapod.util.tmdb.TVEpisode
|
||||
|
||||
class EpisodeItemAdapter(private val episodes: List<Episode>) : RecyclerView.Adapter<EpisodeItemAdapter.EpisodeViewHolder>() {
|
||||
class EpisodeItemAdapter(private val episodes: List<Episode>, private val tmdbEpisodes: List<TVEpisode>?) : RecyclerView.Adapter<EpisodeItemAdapter.EpisodeViewHolder>() {
|
||||
|
||||
var onImageClick: ((String, Int) -> Unit)? = null
|
||||
|
||||
@ -32,7 +33,13 @@ class EpisodeItemAdapter(private val episodes: List<Episode>) : RecyclerView.Ada
|
||||
}
|
||||
|
||||
holder.binding.textEpisodeTitle.text = titleText
|
||||
holder.binding.textEpisodeDesc.text = ep.shortDesc
|
||||
holder.binding.textEpisodeDesc.text = if (ep.shortDesc.isNotEmpty()) {
|
||||
ep.shortDesc
|
||||
} else if (tmdbEpisodes != null && position < tmdbEpisodes.size){
|
||||
tmdbEpisodes[position].overview
|
||||
} else {
|
||||
""
|
||||
}
|
||||
|
||||
if (episodes[position].posterUrl.isNotEmpty()) {
|
||||
Glide.with(context).load(ep.posterUrl)
|
||||
|
@ -99,14 +99,14 @@ class TMDBApiController {
|
||||
}
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
suspend fun getTVSeasonDetails(tvId: Int, seasonNumber: Int): TVSeason = withContext(Dispatchers.IO) {
|
||||
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)
|
||||
return@withContext null
|
||||
}
|
||||
// println(response)
|
||||
|
||||
@ -114,25 +114,25 @@ class TMDBApiController {
|
||||
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
|
||||
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 ?: -1
|
||||
)
|
||||
}
|
||||
|
||||
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,
|
||||
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
|
||||
seasonNumber = response.get("season_number")?.asInt ?: -1
|
||||
)
|
||||
} catch (ex: Exception) {
|
||||
Log.w(javaClass.name, "Error", ex)
|
||||
TVSeason(-1)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -32,19 +32,19 @@ data class TVShow(
|
||||
|
||||
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
|
||||
val name: String,
|
||||
val overview: String,
|
||||
val posterPath: String,
|
||||
val airDate: String,
|
||||
val episodes: List<TVEpisode>,
|
||||
val seasonNumber: Int
|
||||
)
|
||||
|
||||
// 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
|
||||
val name: String,
|
||||
val overview: String,
|
||||
val airDate: String,
|
||||
val episodeNumber: Int
|
||||
)
|
Reference in New Issue
Block a user