Fix episode removal in simkl (#555)

This commit is contained in:
self-similarity 2023-08-15 18:37:33 +00:00 committed by GitHub
parent 4d98690adb
commit 4e01d327c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -60,8 +60,8 @@ class SimklApi(index: Int) : AccountManager(index), SyncAPI {
private var lastScoreTime = -1L private var lastScoreTime = -1L
companion object { companion object {
private const val clientId = BuildConfig.SIMKL_CLIENT_ID private const val clientId: String = BuildConfig.SIMKL_CLIENT_ID
private const val clientSecret = BuildConfig.SIMKL_CLIENT_SECRET private const val clientSecret: String = BuildConfig.SIMKL_CLIENT_SECRET
private var lastLoginState = "" private var lastLoginState = ""
const val SIMKL_TOKEN_KEY: String = "simkl_token" const val SIMKL_TOKEN_KEY: String = "simkl_token"
@ -498,6 +498,9 @@ class SimklApi(index: Int) : AccountManager(index), SyncAPI {
val episodes: Array<EpisodeMetadata>?, val episodes: Array<EpisodeMetadata>?,
override var isFavorite: Boolean? = null, override var isFavorite: Boolean? = null,
override var maxEpisodes: Int? = null, override var maxEpisodes: Int? = null,
/** Save seen episodes separately to know the change from old to new.
* Required to remove seen episodes if count decreases */
val oldEpisodes: Int,
) : SyncAPI.AbstractSyncStatus() ) : SyncAPI.AbstractSyncStatus()
override suspend fun getStatus(id: String): SyncAPI.AbstractSyncStatus? { override suspend fun getStatus(id: String): SyncAPI.AbstractSyncStatus? {
@ -521,7 +524,8 @@ class SimklApi(index: Int) : AccountManager(index), SyncAPI {
score = foundItem.user_rating, score = foundItem.user_rating,
watchedEpisodes = foundItem.watched_episodes_count, watchedEpisodes = foundItem.watched_episodes_count,
maxEpisodes = foundItem.total_episodes_count, maxEpisodes = foundItem.total_episodes_count,
episodes = episodes episodes = episodes,
oldEpisodes = foundItem.watched_episodes_count ?: 0,
) )
} else { } else {
return if (searchResult != null) { return if (searchResult != null) {
@ -530,7 +534,8 @@ class SimklApi(index: Int) : AccountManager(index), SyncAPI {
score = 0, score = 0,
watchedEpisodes = 0, watchedEpisodes = 0,
maxEpisodes = if (searchResult.type == "movie") 0 else null, maxEpisodes = if (searchResult.type == "movie") 0 else null,
episodes = episodes episodes = episodes,
oldEpisodes = 0,
) )
} else { } else {
null null
@ -604,17 +609,18 @@ class SimklApi(index: Int) : AccountManager(index), SyncAPI {
SimklListStatusType.ReWatching.value SimklListStatusType.ReWatching.value
).contains(status.status) ).contains(status.status)
) { ) {
val cutEpisodes = simklStatus.episodes.take(watchedEpisodes) suspend fun postEpisodes(
url: String,
val (seasons, episodes) = if (cutEpisodes.any { it.season != null }) { rawEpisodes: List<EpisodeMetadata>
EpisodeMetadata.convertToSeasons(cutEpisodes) to null ): Boolean {
val (seasons, episodes) = if (rawEpisodes.any { it.season != null }) {
EpisodeMetadata.convertToSeasons(rawEpisodes) to null
} else { } else {
null to EpisodeMetadata.convertToEpisodes(cutEpisodes) null to EpisodeMetadata.convertToEpisodes(rawEpisodes)
} }
debugPrint { "Synced history using $url: seasons=${seasons?.toList()}, episodes=${episodes?.toList()}" }
debugPrint { "Synced history for ${status.watchedEpisodes} given size of ${simklStatus.episodes.size}: seasons=${seasons?.toList()}, episodes=${episodes?.toList()}" } return app.post(
val episodeResponse = app.post( url,
"$mainUrl/sync/history",
json = StatusRequest( json = StatusRequest(
shows = listOf( shows = listOf(
HistoryMediaObject( HistoryMediaObject(
@ -628,8 +634,21 @@ class SimklApi(index: Int) : AccountManager(index), SyncAPI {
movies = emptyList() movies = emptyList()
), ),
interceptor = interceptor interceptor = interceptor
) ).isSuccessful
episodeResponse.isSuccessful }
// If episodes decrease: remove all episodes beyond watched episodes.
val removeResponse = if (simklStatus.oldEpisodes > watchedEpisodes) {
val removeEpisodes = simklStatus.episodes
.drop(watchedEpisodes)
postEpisodes("$mainUrl/sync/history/remove", removeEpisodes)
} else {
true
}
val cutEpisodes = simklStatus.episodes.take(watchedEpisodes)
val addResponse = postEpisodes("$mainUrl/sync/history/", cutEpisodes)
removeResponse && addResponse
} else true } else true
val newStatus = val newStatus =