2021-07-17 13:15:14 +02:00
/ * *
* Teapod
*
2022-01-02 17:59:23 +01:00
* Copyright 2020 - 2022 < seil0 @mosad . xyz >
2021-07-17 13:15:14 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
*
* /
2021-07-11 12:56:21 +02:00
package org.mosad.teapod.util
2021-07-17 13:15:14 +02:00
import android.util.Log
2021-07-11 12:56:21 +02:00
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import kotlinx.coroutines.*
import java.io.FileNotFoundException
import java.net.URL
2022-01-03 14:10:41 +01:00
/ * *
* TODO remove gson usage
* /
2021-07-11 12:56:21 +02:00
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 ( )
mediaList = Gson ( ) . fromJson ( json , MediaList :: class . java )
}
}
2021-07-17 13:15:14 +02:00
/ * *
* Get the meta data for a movie from MetaDB
* @param aodId The AoD id of the media
* @return A meta movie object , or null if not found
* /
2021-07-11 12:56:21 +02:00
suspend fun getMovieMetadata ( aodId : Int ) : MovieMeta ? {
return metaCacheList . firstOrNull {
it . aodId == aodId
2021-07-17 13:15:14 +02:00
} as MovieMeta ? ?: getMovieMetadataFromDB ( aodId )
2021-07-11 12:56:21 +02:00
}
2021-07-17 13:15:14 +02:00
/ * *
* Get the meta data for a tv show from MetaDB
* @param aodId The AoD id of the media
* @return A meta tv show object , or null if not found
* /
2021-07-11 12:56:21 +02:00
suspend fun getTVShowMetadata ( aodId : Int ) : TVShowMeta ? {
return metaCacheList . firstOrNull {
it . aodId == aodId
2021-07-17 13:15:14 +02:00
} as TVShowMeta ? ?: getTVShowMetadataFromDB ( aodId )
2021-07-11 12:56:21 +02:00
}
@Suppress ( " BlockingMethodInNonBlockingContext " )
2021-07-17 13:15:14 +02:00
private suspend fun getMovieMetadataFromDB ( aodId : Int ) : MovieMeta ? = withContext ( Dispatchers . IO ) {
2021-07-11 12:56:21 +02:00
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 ) {
2021-07-17 13:15:14 +02:00
Log . w ( javaClass . name , " Waring: The requested file was not found. Requested ID: $aodId " , ex )
2021-07-11 12:56:21 +02:00
null
}
}
@Suppress ( " BlockingMethodInNonBlockingContext " )
2021-07-17 13:15:14 +02:00
private suspend fun getTVShowMetadataFromDB ( aodId : Int ) : TVShowMeta ? = withContext ( Dispatchers . IO ) {
2021-07-11 12:56:21 +02:00
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 ) {
2021-07-17 13:15:14 +02:00
Log . w ( javaClass . name , " Waring: The requested file was not found. Requested ID: $aodId " , ex )
2021-07-11 12:56:21 +02:00
null
}
}
}
2021-07-17 13:15:14 +02:00
// class representing the media list json object
2021-07-11 12:56:21 +02:00
data class MediaList (
val media : List < Int >
)
2021-07-17 13:15:14 +02:00
// abstract class used for meta data objects (tv, movie)
2021-07-11 12:56:21 +02:00
abstract class Meta {
abstract val id : Int
abstract val aodId : Int
abstract val tmdbId : Int
}
2021-07-17 13:15:14 +02:00
// class representing the movie json object
2021-07-11 12:56:21 +02:00
data class MovieMeta (
override val id : Int ,
@SerializedName ( " aod_id " )
override val aodId : Int ,
@SerializedName ( " tmdb_id " )
override val tmdbId : Int
) : Meta ( )
2021-07-17 13:15:14 +02:00
// class representing the tv show json object
2021-07-11 12:56:21 +02:00
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 ( )
2021-07-17 13:15:14 +02:00
// class used in TVShowMeta, part of the tv show json object
2021-07-11 12:56:21 +02:00
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 " )
2021-07-17 19:40:16 +02:00
val openingStart : Long ,
2021-07-11 12:56:21 +02:00
@SerializedName ( " opening_duration " )
2021-07-17 19:40:16 +02:00
val openingDuration : Long ,
2021-07-11 12:56:21 +02:00
@SerializedName ( " ending_start " )
2021-07-17 19:40:16 +02:00
val endingStart : Long ,
2021-07-11 12:56:21 +02:00
@SerializedName ( " ending_duration " )
2021-07-17 19:40:16 +02:00
val endingDuration : Long
2021-07-11 12:56:21 +02:00
)