AquaStream/app/src/main/java/com/lagradost/cloudstream3/plugins/VotingApi.kt

109 lines
3.4 KiB
Kotlin
Raw Normal View History

2022-09-05 16:45:13 +00:00
package com.lagradost.cloudstream3.plugins
import android.util.Log
import android.widget.Toast
import com.lagradost.cloudstream3.AcraApplication.Companion.context
2022-09-05 16:45:13 +00:00
import com.lagradost.cloudstream3.AcraApplication.Companion.getKey
import com.lagradost.cloudstream3.AcraApplication.Companion.setKey
import com.lagradost.cloudstream3.R
2022-09-05 16:45:13 +00:00
import java.security.MessageDigest
import com.lagradost.cloudstream3.app
import com.lagradost.cloudstream3.utils.Coroutines.main
2022-09-17 15:41:44 +00:00
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
2022-09-05 16:45:13 +00:00
object VotingApi { // please do not cheat the votes lol
private const val LOGKEY = "VotingApi"
2023-08-09 21:44:17 +00:00
private const val apiDomain = "https://counterapi.com/api"
2022-09-05 16:45:13 +00:00
private fun transformUrl(url: String): String = // dont touch or all votes get reset
2022-09-17 15:41:44 +00:00
MessageDigest
.getInstance("SHA-256")
.digest("${url}#funny-salt".toByteArray())
.fold("") { str, it -> str + "%02x".format(it) }
2022-09-05 16:45:13 +00:00
suspend fun SitePlugin.getVotes(): Int {
return getVotes(url)
2022-09-05 16:45:13 +00:00
}
2023-08-09 21:44:17 +00:00
fun SitePlugin.hasVoted(): Boolean {
return hasVoted(url)
2022-09-05 16:45:13 +00:00
}
2023-08-09 21:44:17 +00:00
suspend fun SitePlugin.vote(): Int {
return vote(url)
2022-09-05 16:45:13 +00:00
}
fun SitePlugin.canVote(): Boolean {
return canVote(this.url)
}
2022-09-06 22:40:31 +00:00
// Plugin url to Int
private val votesCache = mutableMapOf<String, Int>()
2023-08-09 21:44:17 +00:00
private fun getRepository(pluginUrl: String) = pluginUrl
.split("/")
.drop(2)
.take(3)
.joinToString("-")
2022-09-05 16:45:13 +00:00
2023-08-09 21:44:17 +00:00
private suspend fun readVote(pluginUrl: String): Int {
var url = "${apiDomain}/cs-${getRepository(pluginUrl)}/vote/${transformUrl(pluginUrl)}?readOnly=true"
Log.d(LOGKEY, "Requesting: $url")
return app.get(url).parsedSafe<Result>()?.value ?: 0
2022-09-05 16:45:13 +00:00
}
2023-08-09 21:44:17 +00:00
private suspend fun writeVote(pluginUrl: String): Boolean {
var url = "${apiDomain}/cs-${getRepository(pluginUrl)}/vote/${transformUrl(pluginUrl)}"
2022-09-05 16:45:13 +00:00
Log.d(LOGKEY, "Requesting: $url")
2023-08-09 21:44:17 +00:00
return app.get(url).parsedSafe<Result>()?.value != null
2022-09-05 16:45:13 +00:00
}
2023-08-09 21:44:17 +00:00
suspend fun getVotes(pluginUrl: String): Int =
votesCache[pluginUrl] ?: readVote(pluginUrl).also {
votesCache[pluginUrl] = it
}
fun hasVoted(pluginUrl: String) =
getKey("cs3-votes/${transformUrl(pluginUrl)}") ?: false
fun canVote(pluginUrl: String): Boolean {
if (!PluginManager.urlPlugins.contains(pluginUrl)) return false
return true
}
2022-09-17 15:41:44 +00:00
private val voteLock = Mutex()
2023-08-09 21:44:17 +00:00
suspend fun vote(pluginUrl: String): Int {
2022-09-17 15:41:44 +00:00
// Prevent multiple requests at the same time.
voteLock.withLock {
if (!canVote(pluginUrl)) {
main {
Toast.makeText(context, R.string.extension_install_first, Toast.LENGTH_SHORT)
.show()
}
return getVotes(pluginUrl)
}
2022-09-17 15:41:44 +00:00
2023-08-09 21:44:17 +00:00
if (hasVoted(pluginUrl)) {
main {
Toast.makeText(context, R.string.already_voted, Toast.LENGTH_SHORT)
.show()
}
return getVotes(pluginUrl)
2022-09-17 15:41:44 +00:00
}
2023-08-09 21:44:17 +00:00
if (writeVote(pluginUrl)) {
setKey("cs3-votes/${transformUrl(pluginUrl)}", true)
votesCache[pluginUrl] = votesCache[pluginUrl]?.plus(1) ?: 1
}
return getVotes(pluginUrl)
2022-09-05 16:45:13 +00:00
}
}
private data class Result(
val value: Int?
)
}