Skip to content

Commit

Permalink
Implement IMDb duplicate title checking support
Browse files Browse the repository at this point in the history
  • Loading branch information
Luna712 committed Oct 17, 2023
1 parent 4f91641 commit da47f09
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
22 changes: 17 additions & 5 deletions app/src/main/java/com/lagradost/cloudstream3/MainAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import com.lagradost.nicehttp.RequestBodyTypes
import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONException
import org.json.JSONObject
import java.text.SimpleDateFormat
import java.util.*
import kotlin.math.absoluteValue
Expand Down Expand Up @@ -1246,6 +1248,16 @@ interface LoadResponse {
return this.syncData[aniListIdPrefix]
}

fun LoadResponse.getImdbId(): String? {
val simklId = this.syncData[simklIdPrefix] ?: return null
return try {
val jsonObject = JSONObject(simklId)
jsonObject.optString("Imdb")
} catch (e: JSONException) {
null
}
}

fun LoadResponse.addMalId(id: Int?) {
this.syncData[malIdPrefix] = (id ?: return).toString()
this.addSimklId(SimklApi.Companion.SyncServices.Mal, id.toString())
Expand Down Expand Up @@ -1490,7 +1502,7 @@ data class TorrentLoadResponse(
override var syncData: MutableMap<String, String> = mutableMapOf(),
override var posterHeaders: Map<String, String>? = null,
override var backgroundPosterUrl: String? = null,
) : LoadResponse
) : LoadResponse

data class AnimeLoadResponse(
var engName: String? = null,
Expand Down Expand Up @@ -1521,7 +1533,7 @@ data class AnimeLoadResponse(
override var nextAiring: NextAiring? = null,
override var seasonNames: List<SeasonData>? = null,
override var backgroundPosterUrl: String? = null,
) : LoadResponse, EpisodeResponse {
) : LoadResponse, EpisodeResponse {
override fun getLatestEpisodes(): Map<DubStatus, Int?> {
return episodes.map { (status, episodes) ->
val maxSeason = episodes.maxOfOrNull { it.season ?: Int.MIN_VALUE }
Expand Down Expand Up @@ -1583,7 +1595,7 @@ data class LiveStreamLoadResponse(
override var syncData: MutableMap<String, String> = mutableMapOf(),
override var posterHeaders: Map<String, String>? = null,
override var backgroundPosterUrl: String? = null,
) : LoadResponse
) : LoadResponse

data class MovieLoadResponse(
override var name: String,
Expand All @@ -1606,7 +1618,7 @@ data class MovieLoadResponse(
override var syncData: MutableMap<String, String> = mutableMapOf(),
override var posterHeaders: Map<String, String>? = null,
override var backgroundPosterUrl: String? = null,
) : LoadResponse
) : LoadResponse

suspend fun <T> MainAPI.newMovieLoadResponse(
name: String,
Expand Down Expand Up @@ -1730,7 +1742,7 @@ data class TvSeriesLoadResponse(
override var nextAiring: NextAiring? = null,
override var seasonNames: List<SeasonData>? = null,
override var backgroundPosterUrl: String? = null,
) : LoadResponse, EpisodeResponse {
) : LoadResponse, EpisodeResponse {
override fun getLatestEpisodes(): Map<DubStatus, Int?> {
val maxSeason =
episodes.maxOfOrNull { it.season ?: Int.MIN_VALUE }.takeUnless { it == Int.MIN_VALUE }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.lagradost.cloudstream3.CommonActivity.getCastSession
import com.lagradost.cloudstream3.CommonActivity.showToast
import com.lagradost.cloudstream3.LoadResponse.Companion.addTrailer
import com.lagradost.cloudstream3.LoadResponse.Companion.getAniListId
import com.lagradost.cloudstream3.LoadResponse.Companion.getImdbId
import com.lagradost.cloudstream3.LoadResponse.Companion.getMalId
import com.lagradost.cloudstream3.LoadResponse.Companion.isMovie
import com.lagradost.cloudstream3.metaproviders.SyncRedirector
Expand Down Expand Up @@ -830,6 +831,7 @@ class ResultViewModel2 : ViewModel() {
R.string.duplicate_message_bookmarks,
response.name,
response.year,
response.getImdbId(),
getAllBookmarkedDataByWatchType()[status] ?: emptyList()
) { shouldContinue: Boolean, duplicateId: Int? ->
if (!shouldContinue) return@checkAndWarnDuplicates
Expand All @@ -852,7 +854,8 @@ class ResultViewModel2 : ViewModel() {
response.url,
response.apiName,
response.type,
response.posterUrl
response.posterUrl,
response.getImdbId()
)
)

Expand Down Expand Up @@ -899,6 +902,7 @@ class ResultViewModel2 : ViewModel() {
R.string.duplicate_message_subscriptions,
response.name,
response.year,
response.getImdbId(),
getAllSubscriptions(),
) { shouldContinue: Boolean, duplicateId: Int? ->
if (!shouldContinue) {
Expand All @@ -924,7 +928,8 @@ class ResultViewModel2 : ViewModel() {
response.url,
response.apiName,
response.type,
response.posterUrl
response.posterUrl,
response.getImdbId()
)
)

Expand Down Expand Up @@ -960,6 +965,7 @@ class ResultViewModel2 : ViewModel() {
R.string.duplicate_message_favorites,
response.name,
response.year,
response.getImdbId(),
getAllFavorites(),
) { shouldContinue: Boolean, duplicateId: Int? ->
if (!shouldContinue) {
Expand All @@ -984,7 +990,8 @@ class ResultViewModel2 : ViewModel() {
response.url,
response.apiName,
response.type,
response.posterUrl
response.posterUrl,
response.getImdbId()
)
)

Expand All @@ -1002,6 +1009,7 @@ class ResultViewModel2 : ViewModel() {
message: Int,
name: String,
year: Int?,
imdbId: String?,
data: List<DataStoreHelper.BaseSearchResponse>,
checkDuplicatesCallback: (shouldContinue: Boolean, duplicateId: Int?) -> Unit
) {
Expand All @@ -1015,11 +1023,11 @@ class ResultViewModel2 : ViewModel() {
return input.trim().replace("\\s+".toRegex(), " ")
}

// TODO support checking IMDB ID rather than name + year when available
val duplicateEntry = data.find {
// Normalize both strings for comparison
normalizeString(it.name) == normalizeString(name) && it.year == year
(imdbId != null && it.imdbId == imdbId) ||
(normalizeString(it.name) == normalizeString(name) && it.year == year)
}

if (duplicateEntry == null || context == null) {
checkDuplicatesCallback.invoke(true, null)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ object DataStoreHelper {
@JsonProperty("apiName") override val apiName: String,
@JsonProperty("type") override var type: TvType? = null,
@JsonProperty("posterUrl") override var posterUrl: String?,
@JsonProperty("imdbId") open val imdbId: String? = null,
@JsonProperty("quality") override var quality: SearchQuality? = null,
@JsonProperty("posterHeaders") override var posterHeaders: Map<String, String>? = null
) : SearchResponse
Expand All @@ -375,9 +376,10 @@ object DataStoreHelper {
override val apiName: String,
override var type: TvType?,
override var posterUrl: String?,
override var imdbId: String? = null,
override var quality: SearchQuality? = null,
override var posterHeaders: Map<String, String>? = null
) : BaseSearchResponse(id, name, year, url, apiName, type, posterUrl, quality, posterHeaders) {
) : BaseSearchResponse(id, name, year, url, apiName, type, posterUrl, imdbId, quality, posterHeaders) {
fun toLibraryItem(): SyncAPI.LibraryItem? {
return SyncAPI.LibraryItem(
name,
Expand All @@ -402,9 +404,10 @@ object DataStoreHelper {
override val apiName: String,
override var type: TvType?,
override var posterUrl: String?,
override var imdbId: String? = null,
override var quality: SearchQuality? = null,
override var posterHeaders: Map<String, String>? = null
) : BaseSearchResponse(id, name, year, url, apiName, type, posterUrl, quality, posterHeaders) {
) : BaseSearchResponse(id, name, year, url, apiName, type, posterUrl, imdbId, quality, posterHeaders) {
fun toLibraryItem(id: String): SyncAPI.LibraryItem {
return SyncAPI.LibraryItem(
name,
Expand All @@ -429,9 +432,10 @@ object DataStoreHelper {
override val apiName: String,
override var type: TvType?,
override var posterUrl: String?,
override var imdbId: String? = null,
override var quality: SearchQuality? = null,
override var posterHeaders: Map<String, String>? = null
) : BaseSearchResponse(id, name, year, url, apiName, type, posterUrl, quality, posterHeaders) {
) : BaseSearchResponse(id, name, year, url, apiName, type, posterUrl, imdbId, quality, posterHeaders) {
fun toLibraryItem(): SyncAPI.LibraryItem? {
return SyncAPI.LibraryItem(
name,
Expand Down Expand Up @@ -460,7 +464,7 @@ object DataStoreHelper {
override var posterUrl: String?,
override var quality: SearchQuality? = null,
override var posterHeaders: Map<String, String>? = null
) : BaseSearchResponse(id, name, null, url, apiName, type, posterUrl, quality, posterHeaders)
) : BaseSearchResponse(id, name, null, url, apiName, type, posterUrl, null, quality, posterHeaders)

/**
* A datastore wide account for future implementations of a multiple account system
Expand Down

0 comments on commit da47f09

Please sign in to comment.