cloudstream-extensions-hexated/StremioX/src/main/kotlin/com/hexated/Utils.kt

73 lines
2.0 KiB
Kotlin
Raw Normal View History

2023-04-01 18:27:43 +00:00
package com.hexated
2023-09-21 07:50:29 +00:00
import com.lagradost.cloudstream3.APIHolder.unixTimeMS
2023-09-21 07:43:38 +00:00
import com.lagradost.cloudstream3.mvvm.logError
2024-01-16 08:56:30 +00:00
import com.lagradost.cloudstream3.utils.getQualityFromName
import okhttp3.Interceptor
2023-04-15 23:15:07 +00:00
import okhttp3.Response
2023-08-11 13:22:36 +00:00
import java.text.SimpleDateFormat
import java.util.Locale
2023-04-15 23:15:07 +00:00
import java.util.concurrent.TimeUnit
fun String.fixSourceUrl(): String {
2023-04-01 18:27:43 +00:00
return this.replace("/manifest.json", "").replace("stremio://", "https://")
}
2024-01-16 08:56:30 +00:00
fun fixSourceName(name: String?, title: String?): String {
2023-04-03 21:27:03 +00:00
return when {
name?.contains("[RD+]", true) == true -> "[RD+] $title"
2024-01-16 08:56:30 +00:00
name?.contains("[RD download]", true) == true -> "[RD download] $title"
2023-04-03 21:27:03 +00:00
!name.isNullOrEmpty() && !title.isNullOrEmpty() -> "$name $title"
else -> title ?: name ?: ""
}
2023-04-02 04:33:33 +00:00
}
2024-01-16 08:56:30 +00:00
fun getQuality(qualities: List<String?>): Int {
fun String.getQuality(): String? {
return Regex("(\\d{3,4}[pP])").find(this)?.groupValues?.getOrNull(1)
}
val quality = qualities.firstNotNullOfOrNull { it?.getQuality() }
return getQualityFromName(quality)
}
2023-04-01 18:27:43 +00:00
fun getEpisodeSlug(
season: Int? = null,
episode: Int? = null,
): Pair<String, String> {
return if (season == null && episode == null) {
"" to ""
} else {
(if (season!! < 10) "0$season" else "$season") to (if (episode!! < 10) "0$episode" else "$episode")
}
}
2023-08-11 13:22:36 +00:00
fun isUpcoming(dateString: String?): Boolean {
2023-09-21 07:43:38 +00:00
return try {
val format = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
val dateTime = dateString?.let { format.parse(it)?.time } ?: return false
2023-09-21 07:50:29 +00:00
unixTimeMS < dateTime
2023-09-21 07:43:38 +00:00
} catch (t: Throwable) {
logError(t)
false
}
2023-08-11 13:22:36 +00:00
}
2023-04-01 18:27:43 +00:00
fun fixUrl(url: String, domain: String): String {
if (url.startsWith("http")) {
return url
}
if (url.isEmpty()) {
return ""
}
val startsWithNoHttp = url.startsWith("//")
if (startsWithNoHttp) {
return "https:$url"
} else {
if (url.startsWith('/')) {
return domain + url
}
return "$domain/$url"
}
}