Added a sort by codec feature to animetorrentio extension #961

Open
worldInColors wants to merge 3 commits from worldInColors/feature/sort-by-codec into main
2 changed files with 68 additions and 8 deletions

View file

@ -1,7 +1,7 @@
ext {
extName = 'Torrentio Anime (Torrent / Debrid)'
extClass = '.Torrentio'
extVersionCode = 16
extVersionCode = 17
containsNsfw = false
}

View file

@ -456,18 +456,50 @@ class Torrentio : ConfigurableAnimeSource, AnimeHttpSource() {
}.orEmpty()
}
override fun List<Video>.sort(): List<Video> {
val isDub = preferences.getBoolean(IS_DUB_KEY, IS_DUB_DEFAULT)
val isEfficient = preferences.getBoolean(IS_EFFICIENT_KEY, IS_EFFICIENT_DEFAULT)
override fun List<Video>.sort(): List<Video> {
val isDub = preferences.getBoolean(IS_DUB_KEY, IS_DUB_DEFAULT)
val isEfficient = preferences.getBoolean(IS_EFFICIENT_KEY, IS_EFFICIENT_DEFAULT)
val codecPreferences = preferences.getStringSet(PREF_CODEC_KEY, PREF_CODEC_DEFAULT) ?: setOf()
return sortedWith(
return if (codecPreferences.isNotEmpty()) {
// Filter to only show videos matching selected codecs
filter { video ->
codecPreferences.any { codec ->
when (codec) {
"x264" -> video.quality.contains("264", true) ||
(!video.quality.contains("265", true) &&
!video.quality.contains("hevc", true) &&
!video.quality.contains("av1", true) &&
!video.quality.contains("vp9", true))
"x265" -> video.quality.contains("265", true) ||
video.quality.contains("hevc", true)
"av1" -> video.quality.contains("av1", true)
"vp9" -> video.quality.contains("vp9", true)
"other" -> !video.quality.contains("264", true) &&
!video.quality.contains("265", true) &&
!video.quality.contains("hevc", true) &&
!video.quality.contains("av1", true) &&
!video.quality.contains("vp9", true)
else -> false
}
}
}.sortedWith(
compareBy(
{ Regex("\\[(.+?) download]").containsMatchIn(it.quality) },
{ isDub && !it.quality.contains("dubbed", true) }
)
)
} else {
// If no codec preferences, use old sorting logic
sortedWith(
compareBy(
{ Regex("\\[(.+?) download]").containsMatchIn(it.quality) },
{ isDub && !it.quality.contains("dubbed", true) },
{ isEfficient && !arrayOf("hevc", "265", "av1").any { q -> it.quality.contains(q, true) } },
),
)
)
}
}
private fun fetchTrackers(): String {
val request = Request.Builder()
@ -616,6 +648,18 @@ class Torrentio : ConfigurableAnimeSource, AnimeHttpSource() {
}
summary = "Codec: (HEVC / x265) & AV1. High-quality video with less data usage."
}.also(screen::addPreference)
MultiSelectListPreference(screen.context).apply {
key = PREF_CODEC_KEY
title = "Preferred Codecs"
entries = PREF_CODEC
entryValues = PREF_CODEC_VALUE
setDefaultValue(PREF_CODEC_DEFAULT)
setOnPreferenceChangeListener { _, newValue ->
@Suppress("UNCHECKED_CAST")
preferences.edit().putStringSet(key, newValue as Set<String>).commit()
}
}.also(screen::addPreference)
}
companion object {
@ -897,7 +941,22 @@ class Torrentio : ConfigurableAnimeSource, AnimeHttpSource() {
private const val IS_EFFICIENT_KEY = "efficient"
private const val IS_EFFICIENT_DEFAULT = false
private const val PREF_CODEC_KEY = "codec_selection"
private val PREF_CODEC = arrayOf(
"x264",
"x265/HEVC",
"AV1",
"VP9",
"Other"
)
private val PREF_CODEC_VALUE = arrayOf(
"x264",
"x265",
"av1",
"vp9",
"other"
)
private val PREF_CODEC_DEFAULT = setOf<String>() // Empty by default to show al
private val DATE_TIME_FORMATTER by lazy {
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH)
}
@ -905,5 +964,6 @@ class Torrentio : ConfigurableAnimeSource, AnimeHttpSource() {
private val DATE_FORMATTER by lazy {
SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH)
}
}
}