14 changed files with 198 additions and 221 deletions
@ -1,159 +0,0 @@
|
||||
/** |
||||
* Teapod |
||||
* |
||||
* Copyright 2020-2022 <seil0@mosad.xyz> |
||||
* |
||||
* 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. |
||||
* |
||||
*/ |
||||
|
||||
package org.mosad.teapod.util |
||||
|
||||
import android.util.Log |
||||
import com.google.gson.Gson |
||||
import com.google.gson.annotations.SerializedName |
||||
import kotlinx.coroutines.* |
||||
import java.io.FileNotFoundException |
||||
import java.net.URL |
||||
|
||||
/** |
||||
* TODO remove gson usage |
||||
*/ |
||||
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) |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 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 |
||||
*/ |
||||
suspend fun getMovieMetadata(aodId: Int): MovieMeta? { |
||||
return metaCacheList.firstOrNull { |
||||
it.aodId == aodId |
||||
} as MovieMeta? ?: getMovieMetadataFromDB(aodId) |
||||
} |
||||
|
||||
/** |
||||
* 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 |
||||
*/ |
||||
suspend fun getTVShowMetadata(aodId: Int): TVShowMeta? { |
||||
return metaCacheList.firstOrNull { |
||||
it.aodId == aodId |
||||
} as TVShowMeta? ?: getTVShowMetadataFromDB(aodId) |
||||
} |
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext") |
||||
private suspend fun getMovieMetadataFromDB(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) { |
||||
Log.w(javaClass.name, "Waring: The requested file was not found. Requested ID: $aodId", ex) |
||||
null |
||||
} |
||||
} |
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext") |
||||
private suspend fun getTVShowMetadataFromDB(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) { |
||||
Log.w(javaClass.name, "Waring: The requested file was not found. Requested ID: $aodId", ex) |
||||
null |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
// class representing the media list json object |
||||
data class MediaList( |
||||
val media: List<Int> |
||||
) |
||||
|
||||
// abstract class used for meta data objects (tv, movie) |
||||
abstract class Meta { |
||||
abstract val id: Int |
||||
abstract val aodId: Int |
||||
abstract val tmdbId: Int |
||||
} |
||||
|
||||
// class representing the movie json object |
||||
data class MovieMeta( |
||||
override val id: Int, |
||||
@SerializedName("aod_id") |
||||
override val aodId: Int, |
||||
@SerializedName("tmdb_id") |
||||
override val tmdbId: Int |
||||
): Meta() |
||||
|
||||
// class representing the tv show json object |
||||
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() |
||||
|
||||
// class used in TVShowMeta, part of the tv show json object |
||||
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: Long, |
||||
@SerializedName("opening_duration") |
||||
val openingDuration: Long, |
||||
@SerializedName("ending_start") |
||||
val endingStart: Long, |
||||
@SerializedName("ending_duration") |
||||
val endingDuration: Long |
||||
) |
@ -0,0 +1,57 @@
|
||||
package org.mosad.teapod.util.metadb |
||||
|
||||
import kotlinx.serialization.SerialName |
||||
import kotlinx.serialization.Serializable |
||||
|
||||
// class representing the media list json object |
||||
@Serializable |
||||
data class MediaList( |
||||
@SerialName("media") val media: List<String> |
||||
) |
||||
|
||||
// abstract class used for meta data objects (tv, movie) |
||||
abstract class Meta { |
||||
abstract val id: Int |
||||
abstract val tmdbId: Int |
||||
abstract val crSeriesId: String |
||||
} |
||||
|
||||
// class representing the movie json object |
||||
@Serializable |
||||
data class MovieMeta( |
||||
@SerialName("id") override val id: Int, |
||||
@SerialName("tmdb_id") override val tmdbId: Int, |
||||
@SerialName("cr_series_id") override val crSeriesId: String, |
||||
): Meta() |
||||
|
||||
// class representing the tv show json object |
||||
@Serializable |
||||
data class TVShowMeta( |
||||
@SerialName("id") override val id: Int, |
||||
@SerialName("tmdb_id") override val tmdbId: Int, |
||||
@SerialName("cr_series_id") override val crSeriesId: String, |
||||
@SerialName("seasons") val seasons: List<SeasonMeta>, |
||||
): Meta() |
||||
|
||||
// class used in TVShowMeta, part of the tv show json object |
||||
@Serializable |
||||
data class SeasonMeta( |
||||
@SerialName("id") val id: Int, |
||||
@SerialName("tmdb_season_id") val tmdbSeasonId: Int, |
||||
@SerialName("tmdb_season_number") val tmdbSeasonNumber: Int, |
||||
@SerialName("cr_season_ids") val crSeasonIds: List<String>, |
||||
@SerialName("episodes") val episodes: List<EpisodeMeta>, |
||||
) |
||||
|
||||
// class used in TVShowMeta, part of the tv show json object |
||||
@Serializable |
||||
data class EpisodeMeta( |
||||
@SerialName("id") val id: Int, |
||||
@SerialName("tmdb_episode_id") val tmdbEpisodeId: Int, |
||||
@SerialName("tmdb_episode_number") val tmdbEpisodeNumber: Int, |
||||
@SerialName("cr_episode_ids") val crEpisodeIds: List<String>, |
||||
@SerialName("opening_start") val openingStart: Long, |
||||
@SerialName("opening_duration") val openingDuration: Long, |
||||
@SerialName("ending_start") val endingStart: Long, |
||||
@SerialName("ending_duration") val endingDuration: Long |
||||
) |
@ -0,0 +1,88 @@
|
||||
/** |
||||
* Teapod |
||||
* |
||||
* Copyright 2020-2022 <seil0@mosad.xyz> |
||||
* |
||||
* 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. |
||||
* |
||||
*/ |
||||
|
||||
package org.mosad.teapod.util.metadb |
||||
|
||||
import android.util.Log |
||||
import io.ktor.client.* |
||||
import io.ktor.client.features.* |
||||
import io.ktor.client.features.json.* |
||||
import io.ktor.client.features.json.serializer.* |
||||
import io.ktor.client.request.* |
||||
import io.ktor.http.* |
||||
import kotlinx.coroutines.Dispatchers |
||||
import kotlinx.coroutines.withContext |
||||
import kotlinx.serialization.decodeFromString |
||||
import kotlinx.serialization.json.Json |
||||
|
||||
object MetaDBController { |
||||
private val TAG = javaClass.name |
||||
|
||||
private const val repoUrl = "https://gitlab.com/Seil0/teapodmetadb/-/raw/main/crunchy/" |
||||
|
||||
private val client = HttpClient { |
||||
install(JsonFeature) { |
||||
serializer = KotlinxSerializer(Json) |
||||
} |
||||
} |
||||
|
||||
private var mediaList = MediaList(listOf()) |
||||
private var metaCacheList = arrayListOf<Meta>() |
||||
|
||||
suspend fun list() = withContext(Dispatchers.IO) { |
||||
val raw: String = client.get("$repoUrl/list.json") |
||||
mediaList = Json.decodeFromString(raw) |
||||
} |
||||
|
||||
/** |
||||
* Get the meta data for a movie from MetaDB |
||||
* @param crSeriesId The crunchyroll media id |
||||
* @return A meta object, or null if not found |
||||
*/ |
||||
suspend fun getTVShowMetadata(crSeriesId: String): TVShowMeta? { |
||||
return if (mediaList.media.contains(crSeriesId)) { |
||||
metaCacheList.firstOrNull { |
||||
it.crSeriesId == crSeriesId |
||||
} as TVShowMeta? ?: getTVShowMetadataFromDB(crSeriesId) |
||||
} else { |
||||
null |
||||
} |
||||
} |
||||
|
||||
private suspend fun getTVShowMetadataFromDB(crSeriesId: String): TVShowMeta? = withContext(Dispatchers.IO) { |
||||
return@withContext try { |
||||
val raw: String = client.get("$repoUrl/tv/$crSeriesId/media.json") |
||||
val meta: TVShowMeta = Json.decodeFromString(raw) |
||||
metaCacheList.add(meta) |
||||
|
||||
meta |
||||
} catch (ex: ClientRequestException) { |
||||
when (ex.response.status) { |
||||
HttpStatusCode.NotFound -> Log.w(TAG, "The requested file was not found. Series ID: $crSeriesId", ex) |
||||
else -> Log.e(TAG, "Error while requesting meta data. Series ID: $crSeriesId", ex) |
||||
} |
||||
|
||||
null // todo return none object |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue