Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix image RPC #123

Open
wants to merge 1 commit into
base: kuukiyomi
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.connections.ConnectionsManager
import eu.kanade.tachiyomi.data.notification.NotificationReceiver
import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.NetworkHelper
import eu.kanade.tachiyomi.ui.player.viewer.PipState
import eu.kanade.tachiyomi.util.system.notificationBuilder
Expand All @@ -33,6 +32,7 @@ import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
import kotlin.math.ceil
import kotlin.math.floor
import kotlinx.serialization.json.Json

class DiscordRPCService : Service() {

Expand Down Expand Up @@ -239,24 +239,27 @@ class DiscordRPCService : Service() {
}

withIOContext {
val connectionsManager: ConnectionsManager by injectLazy()
val networkService: NetworkHelper by injectLazy()
val client = networkService.client
val response = if (!discordIncognito) {
val json = Json {
encodeDefaults = true
allowStructuredMapKeys = true
ignoreUnknownKeys = true
}
val rpcExternalAsset = RPCExternalAsset(applicationId = RICH_PRESENCE_APPLICATION_ID, token = connectionsPreferences.connectionsToken(connectionsManager.discord).get(), client = client, json = json)

val discordUri = if (!discordIncognito) {
try {
client.newCall(
GET("https://kizzy-api.vercel.app/image?url=${playerData.thumbnailUrl}"),
).execute()
rpcExternalAsset.getDiscordUri(playerData.thumbnailUrl)
} catch (e: Throwable) {
null
}
} else {
null
}

val animeThumbnail = response?.body?.string()
?.takeIf { !it.contains("external/Not Found") }?.substringAfter("\"id\": \"")?.substringBefore(
"\"}",
)
val animeThumbnail = discordUri?.takeIf { !it.contains("external/Not Found") }
?.substringAfter("\"id\": \"")?.substringBefore("\"}")
?.split("external/")?.getOrNull(1)?.let { "external/$it" }

setAnimeScreen(
Expand All @@ -271,6 +274,7 @@ class DiscordRPCService : Service() {
}
}


@Suppress("SwallowedException", "TooGenericExceptionCaught", "CyclomaticComplexMethod")
internal suspend fun setReaderActivity(
context: Context,
Expand Down Expand Up @@ -306,24 +310,23 @@ class DiscordRPCService : Service() {
}

withIOContext {
val connectionsManager: ConnectionsManager by injectLazy()
val networkService: NetworkHelper by injectLazy()
val client = networkService.client
val response = if (!discordIncognito) {
val json = Json { ignoreUnknownKeys = true } // Configura el JSON parser si es necesario
val rpcExternalAsset = RPCExternalAsset(applicationId = RICH_PRESENCE_APPLICATION_ID , token = connectionsPreferences.connectionsToken(connectionsManager.discord).get(), client = client, json = json)

val discordUri = if (!discordIncognito) {
try {
client.newCall(
GET("https://kizzy-api.vercel.app/image?url=${readerData.thumbnailUrl}"),
).execute()
rpcExternalAsset.getDiscordUri(readerData.thumbnailUrl)
} catch (e: Throwable) {
null
}
} else {
null
}

val mangaThumbnail = response?.body?.string()
?.takeIf { !it.contains("external/Not Found") }?.substringAfter("\"id\": \"")?.substringBefore(
"\"}",
)
val mangaThumbnail = discordUri?.takeIf { !it.contains("external/Not Found") }
?.substringAfter("\"id\": \"")?.substringBefore("\"}")
?.split("external/")?.getOrNull(1)?.let { "external/$it" }

setMangaScreen(
Expand All @@ -337,6 +340,8 @@ class DiscordRPCService : Service() {
)
}
}

private const val RICH_PRESENCE_APPLICATION_ID = "952899285983326208"
}
}
// <-- AM (DISCORD)
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package eu.kanade.tachiyomi.data.connections.discord

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import okhttp3.Call
import okhttp3.Callback
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import okio.IOException
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

class RPCExternalAsset(
applicationId: String,
private val token: String,
private val client: OkHttpClient,
private val json: Json
) {

@Serializable
data class ExternalAsset(
val url: String? = null,
@SerialName("external_asset_path")
val externalAssetPath: String? = null
)

private val api = "https://discord.com/api/v9/applications/$applicationId/external-assets"
suspend fun getDiscordUri(imageUrl: String): String? {
if (imageUrl.startsWith("mp:")) return imageUrl
val request = Request.Builder().url(api).header("Authorization", token)
.post("{\"urls\":[\"$imageUrl\"]}".toRequestBody("application/json".toMediaType()))
.build()
return runCatching {
val res = client.newCall(request).await()
json.decodeFromString<List<ExternalAsset>>(res.body.string())
.firstOrNull()?.externalAssetPath?.let { "mp:$it" }
}.getOrNull()
}

private suspend inline fun Call.await(): Response {
return suspendCoroutine {
enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
it.resumeWithException(e)
}

override fun onResponse(call: Call, response: Response) {
it.resume(response)
}
})
}
}
}