feat(lib/StreamSB): Get API endpoint dynamically (#1539)

This commit is contained in:
Claudemirovsky 2023-04-25 04:01:31 -03:00 committed by GitHub
parent c5999483bf
commit 23fb6cb2c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 16 deletions

View File

@ -19,5 +19,6 @@ dependencies {
compileOnly(libs.kotlin.json)
compileOnly(libs.okhttp)
compileOnly(libs.aniyomi.lib)
compileOnly(libs.injekt.core)
}
// BUMPS: 0

View File

@ -1,5 +1,6 @@
package eu.kanade.tachiyomi.lib.streamsbextractor
import android.app.Application
import eu.kanade.tachiyomi.animesource.model.Track
import eu.kanade.tachiyomi.animesource.model.Video
import eu.kanade.tachiyomi.network.GET
@ -8,9 +9,34 @@ import kotlinx.serialization.json.Json
import okhttp3.Headers
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
class StreamSBExtractor(private val client: OkHttpClient) {
companion object {
private const val PREF_ENDPOINT_KEY = "streamsb_api_endpoint"
private const val PREF_ENDPOINT_DEFAULT = "/sources16"
private const val ENDPOINT_URL = "https://raw.githubusercontent.com/Claudemirovsky/streamsb-endpoint/master/endpoint.txt"
}
private val json: Json by injectLazy()
private val preferences by lazy {
Injekt.get<Application>().getSharedPreferences(javaClass.simpleName, 0x0000)
}
private fun getEndpoint() = preferences.getString(PREF_ENDPOINT_KEY, PREF_ENDPOINT_DEFAULT)!!
private fun updateEndpoint() {
client.newCall(GET(ENDPOINT_URL)).execute()
.use { it.body.string() }
.let {
preferences.edit().putString(PREF_ENDPOINT_KEY, it).commit()
}
}
protected fun bytesToHex(bytes: ByteArray): String {
val hexArray = "0123456789ABCDEF".toCharArray()
val hexChars = CharArray(bytes.size * 2)
@ -26,7 +52,7 @@ class StreamSBExtractor(private val client: OkHttpClient) {
// animension, asianload and dramacool uses "common = false"
private fun fixUrl(url: String, common: Boolean): String {
val host = url.toHttpUrl().host
val sbUrl = "https://$host/sources16"
val sbUrl = "https://$host" + getEndpoint()
val id = url.substringAfter(host)
.substringAfter("/e/")
.substringAfter("/embed-")
@ -43,18 +69,30 @@ class StreamSBExtractor(private val client: OkHttpClient) {
fun videosFromUrl(url: String, headers: Headers, prefix: String = "", suffix: String = "", common: Boolean = true, manualData: Boolean = false): List<Video> {
val trimmedUrl = url.trim() // Prevents some crashes
val newHeaders = if(manualData) headers else headers.newBuilder()
val newHeaders = if (manualData) {
headers
} else {
headers.newBuilder()
.set("referer", trimmedUrl)
.set("watchsb", "sbstream")
.set("authority", "embedsb.com")
.build()
}
return try {
val master = if(manualData) trimmedUrl else fixUrl(trimmedUrl, common)
val json = Json { ignoreUnknownKeys = true }.decodeFromString<Response>(
client.newCall(GET(master, newHeaders))
val master = if (manualData) trimmedUrl else fixUrl(trimmedUrl, common)
val request = client.newCall(GET(master, newHeaders)).execute()
val json = json.decodeFromString<Response>(
if (request.code == 200) {
request.use { it.body.string() }
} else {
updateEndpoint()
client.newCall(GET(fixUrl(trimmedUrl, common), newHeaders))
.execute()
.use { it.body.string() }
},
)
val masterUrl = json.stream_data.file.trim('"')
val subtitleList = json.stream_data.subs?.let {
it.map { s -> Track(s.file, s.label) }
@ -69,7 +107,7 @@ class StreamSBExtractor(private val client: OkHttpClient) {
.map {
Track(
it.groupValues[2], // Url
it.groupValues[1] // Name
it.groupValues[1], // Name
)
}.toList()
@ -80,11 +118,17 @@ class StreamSBExtractor(private val client: OkHttpClient) {
.substringAfter("x")
.substringBefore(",") + "p"
val quality = ("StreamSB:" + resolution).let {
if(prefix.isNotBlank()) "$prefix $it"
else it
if (prefix.isNotBlank()) {
"$prefix $it"
} else {
it
}
}.let {
if(suffix.isNotBlank()) "$it $suffix"
else it
if (suffix.isNotBlank()) {
"$it $suffix"
} else {
it
}
}
val videoUrl = it.substringAfter("\n").substringBefore("\n")
if (audioList.isEmpty()) {