From 970186e65397eec597a7e5dd257fc7f87c8689c9 Mon Sep 17 00:00:00 2001 From: hexated Date: Sun, 16 Apr 2023 06:15:07 +0700 Subject: [PATCH] stremioX: added custom request --- StremioX/build.gradle.kts | 2 +- .../src/main/kotlin/com/hexated/StremioC.kt | 11 +++--- .../src/main/kotlin/com/hexated/StremioX.kt | 6 +--- StremioX/src/main/kotlin/com/hexated/Utils.kt | 34 +++++++++++++++++-- 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/StremioX/build.gradle.kts b/StremioX/build.gradle.kts index d90416ae..7edaa107 100644 --- a/StremioX/build.gradle.kts +++ b/StremioX/build.gradle.kts @@ -1,5 +1,5 @@ // use an integer for version numbers -version = 8 +version = 9 cloudstream { diff --git a/StremioX/src/main/kotlin/com/hexated/StremioC.kt b/StremioX/src/main/kotlin/com/hexated/StremioC.kt index 90fcceef..4c2df753 100644 --- a/StremioX/src/main/kotlin/com/hexated/StremioC.kt +++ b/StremioX/src/main/kotlin/com/hexated/StremioC.kt @@ -1,6 +1,5 @@ package com.hexated -import android.util.Log import com.fasterxml.jackson.annotation.JsonProperty import com.hexated.SubsExtractors.invokeOpenSubs import com.hexated.SubsExtractors.invokeWatchsomuch @@ -11,7 +10,6 @@ import com.lagradost.cloudstream3.utils.* import com.lagradost.cloudstream3.utils.AppUtils.parseJson import com.lagradost.cloudstream3.utils.AppUtils.toJson import com.lagradost.cloudstream3.utils.AppUtils.tryParseJson -import org.json.JSONObject import java.net.URI private const val TRACKER_LIST_URL = @@ -65,9 +63,9 @@ class StremioC : MainAPI() { callback: (ExtractorLink) -> Unit ): Boolean { val loadData = parseJson(data) - val request = app.get("${mainUrl}/stream/${loadData.type}/${loadData.id}.json") - if (request.isSuccessful) { - val res = tryParseJson(request.text) ?: return false + val request = request("${mainUrl}/stream/${loadData.type}/${loadData.id}.json") + if (request.code.isSuccessful()) { + val res = tryParseJson(request.body.string()) ?: return false res.streams.forEach { stream -> stream.runCallback(subtitleCallback, callback) } @@ -108,8 +106,9 @@ class StremioC : MainAPI() { AcraApplication.getKey>(USER_PROVIDER_API)?.toMutableList() ?: mutableListOf() sites.filter { it.parentJavaClass == "StremioX" }.apmap { site -> + val request = request("${site.url.fixSourceUrl()}/stream/${type}/${id}.json").body.string() val res = - tryParseJson(app.get("${site.url.fixSourceUrl()}/stream/${type}/${id}.json").text) + tryParseJson(request) ?: return@apmap res.streams.forEach { stream -> stream.runCallback(subtitleCallback, callback) diff --git a/StremioX/src/main/kotlin/com/hexated/StremioX.kt b/StremioX/src/main/kotlin/com/hexated/StremioX.kt index 8d6a00a1..1da80f3b 100644 --- a/StremioX/src/main/kotlin/com/hexated/StremioX.kt +++ b/StremioX/src/main/kotlin/com/hexated/StremioX.kt @@ -1,6 +1,5 @@ package com.hexated -import android.util.Log import com.fasterxml.jackson.annotation.JsonProperty import com.hexated.SubsExtractors.invokeOpenSubs import com.hexated.SubsExtractors.invokeWatchsomuch @@ -9,7 +8,6 @@ import com.lagradost.cloudstream3.LoadResponse.Companion.addTrailer import com.lagradost.cloudstream3.utils.* import com.lagradost.cloudstream3.utils.AppUtils.parseJson import com.lagradost.cloudstream3.utils.AppUtils.toJson -import org.json.JSONObject import java.net.URI import java.util.ArrayList import kotlin.math.roundToInt @@ -26,8 +24,6 @@ open class StremioX : MainAPI() { companion object { const val TRACKER_LIST_URL = "https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_best.txt" - const val openSubAPI = "https://opensubtitles.strem.io/stremio/v1" - const val watchSomuchAPI = "https://watchsomuch.tv" private const val tmdbAPI = "https://api.themoviedb.org/3" private val apiKey = base64DecodeAPI("ZTM=NTg=MjM=MjM=ODc=MzI=OGQ=MmE=Nzk=Nzk=ZjI=NTA=NDY=NDA=MzA=YjA=") // PLEASE DON'T STEAL @@ -241,7 +237,7 @@ open class StremioX : MainAPI() { } else { "$fixMainUrl/stream/series/$imdbId:$season:$episode.json" } - val res = AppUtils.tryParseJson(app.get(url).text) ?: return + val res = AppUtils.tryParseJson(request(url).body.string()) ?: return res.streams.forEach { stream -> stream.runCallback(subtitleCallback, callback) } diff --git a/StremioX/src/main/kotlin/com/hexated/Utils.kt b/StremioX/src/main/kotlin/com/hexated/Utils.kt index fb0dcac5..e75201fd 100644 --- a/StremioX/src/main/kotlin/com/hexated/Utils.kt +++ b/StremioX/src/main/kotlin/com/hexated/Utils.kt @@ -1,10 +1,40 @@ package com.hexated -fun String.fixSourceUrl() : String { +import com.lagradost.nicehttp.Requests.Companion.await +import okhttp3.OkHttpClient +import okhttp3.Request +import okhttp3.Response +import java.util.concurrent.TimeUnit + +const val defaultTimeOut = 30L +suspend fun request( + url: String, + allowRedirects: Boolean = true, + timeout: Long = defaultTimeOut +): Response { + val client = OkHttpClient().newBuilder() + .connectTimeout(timeout, TimeUnit.SECONDS) + .readTimeout(timeout, TimeUnit.SECONDS) + .writeTimeout(timeout, TimeUnit.SECONDS) + .followRedirects(allowRedirects) + .followSslRedirects(allowRedirects) + .build() + + val request: Request = Request.Builder() + .url(url) + .build() + return client.newCall(request).await() +} + +fun Int.isSuccessful() : Boolean { + return this in 200..299 +} + +fun String.fixSourceUrl(): String { return this.replace("/manifest.json", "").replace("stremio://", "https://") } -fun fixRDSourceName(name: String?, title: String?) : String { +fun fixRDSourceName(name: String?, title: String?): String { return when { name?.contains("[RD+]", true) == true -> "[RD+] $title" name?.contains("[RD download]", true) == true -> "[RD] $title"