This commit is contained in:
reduplicated 2023-02-09 00:37:29 +01:00
parent eb47f372b2
commit eac7c2b2e3
1 changed files with 28 additions and 16 deletions

View File

@ -15,12 +15,9 @@ import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.aniList
import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.malApi import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.malApi
import com.lagradost.cloudstream3.syncproviders.SyncIdName import com.lagradost.cloudstream3.syncproviders.SyncIdName
import com.lagradost.cloudstream3.ui.player.SubtitleData import com.lagradost.cloudstream3.ui.player.SubtitleData
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.isTvSettings
import com.lagradost.cloudstream3.utils.* import com.lagradost.cloudstream3.utils.*
import com.lagradost.cloudstream3.ui.result.UiText
import com.lagradost.cloudstream3.utils.AppUtils.toJson import com.lagradost.cloudstream3.utils.AppUtils.toJson
import com.lagradost.cloudstream3.utils.Coroutines.threadSafeListOf import com.lagradost.cloudstream3.utils.Coroutines.threadSafeListOf
import com.lagradost.cloudstream3.utils.ExtractorLink
import okhttp3.Interceptor import okhttp3.Interceptor
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
@ -163,35 +160,50 @@ object APIHolder {
return null return null
} }
private var trackerCache: HashMap<String, AniSearch> = hashMapOf()
/** /**
* Get anime tracker information based on title, year and type. * Get anime tracker information based on title, year and type.
* Both titles are attempted to be matched with both Romaji and English title. * Both titles are attempted to be matched with both Romaji and English title.
* Uses the consumet api. * Uses the consumet api.
* *
* @param mainTitle Title to search by and match * @param titles uses first index to search, but if you have multiple titles and want extra guarantee to match you can also have that
* @param secondaryTitle Optional extra title if you have multiple titles and want extra guarantee to match.
* @param types Optional parameter to narrow down the scope to Movies, TV, etc. See TrackerType.getTypes() * @param types Optional parameter to narrow down the scope to Movies, TV, etc. See TrackerType.getTypes()
* @param year Optional parameter to only get anime with a specific year * @param year Optional parameter to only get anime with a specific year
**/ **/
suspend fun getTracker( suspend fun getTracker(
mainTitle: String, titles: List<String>,
secondaryTitle: String?,
types: Set<TrackerType>?, types: Set<TrackerType>?,
year: Int? year: Int?
): Tracker { ): Tracker? {
val res = app.get("https://api.consumet.org/meta/anilist/$mainTitle") return try {
.parsedSafe<AniSearch>()?.results?.find { media -> require(titles.isNotEmpty()) { "titles must no be empty when calling getTracker" }
val mainTitle = titles[0]
val search =
trackerCache[mainTitle]
?: app.get("https://api.consumet.org/meta/anilist/$mainTitle")
.parsedSafe<AniSearch>()?.also {
trackerCache[mainTitle] = it
} ?: return null
val res = search.results?.find { media ->
val matchingYears = year == null || media.releaseDate == year val matchingYears = year == null || media.releaseDate == year
val matchingTitles = val matchingTitles = media.title?.let { title ->
media.title?.isMatchingTitles(mainTitle) == true || media.title?.isMatchingTitles( titles.any { userTitle ->
secondaryTitle title.isMatchingTitles(userTitle)
) == true }
} ?: false
val matchingTypes = types?.any { it.name.equals(media.type, true) } == true val matchingTypes = types?.any { it.name.equals(media.type, true) } == true
matchingTitles && matchingTypes && matchingYears matchingTitles && matchingTypes && matchingYears
} } ?: return null
return Tracker(res?.malId, res?.aniId, res?.image, res?.cover) Tracker(res.malId, res.aniId, res.image, res.cover)
} catch (t: Throwable) {
logError(t)
null
}
} }