Added RepositoryParser

This commit is contained in:
Blatzar 2022-08-06 20:27:56 +02:00
parent b42c0b905b
commit e10cb7b0a3
3 changed files with 95 additions and 3 deletions

View File

@ -89,6 +89,7 @@ import java.io.File
import kotlin.concurrent.thread
import kotlin.reflect.KClass
import com.lagradost.cloudstream3.plugins.PluginManager
import com.lagradost.cloudstream3.plugins.RepositoryParser
const val VLC_PACKAGE = "org.videolan.vlc"
@ -399,7 +400,18 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
}
override fun onCreate(savedInstanceState: Bundle?) {
app.initClient(this)
PluginManager.loadAllPlugins(applicationContext)
// ioSafe {
// val plugins =
// RepositoryParser.getRepoPlugins("https://raw.githubusercontent.com/recloudstream/TestPlugin/master/repo.json")
// ?: emptyList()
// plugins.map {
// println("Load plugin: ${it.name} ${it.url}")
// RepositoryParser.loadSiteTemp(applicationContext, it.url, it.name)
// }
// }
// init accounts
for (api in accountManagers) {
@ -425,7 +437,6 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
loadThemes(this)
updateLocale()
app.initClient(this)
super.onCreate(savedInstanceState)
try {
if (isCastApiAvailable()) {

View File

@ -22,7 +22,6 @@ object PluginManager {
var loadedPlugins = false
private val gson = Gson()
fun loadAllPlugins(context: Context) {
println("LOAD PLUGINS")
val dir = File(PLUGINS_PATH)
if (!dir.exists()) {
val res = dir.mkdirs()
@ -53,7 +52,7 @@ object PluginManager {
//Utils.showToast("Some plugins failed to load.");
}
private fun loadPlugin(context: Context, file: File) {
fun loadPlugin(context: Context, file: File) {
val fileName = file.nameWithoutExtension
//logger.info("Loading plugin: " + fileName);
try {

View File

@ -0,0 +1,82 @@
package com.lagradost.cloudstream3.plugins
import android.content.Context
import com.fasterxml.jackson.annotation.JsonProperty
import com.lagradost.cloudstream3.apmap
import com.lagradost.cloudstream3.app
import com.lagradost.cloudstream3.mvvm.suspendSafeApiCall
import com.lagradost.cloudstream3.utils.AppUtils.tryParseJson
import java.io.BufferedInputStream
import java.io.File
import java.io.InputStream
import java.io.OutputStream
data class Repository(
@JsonProperty("name") val name: String,
@JsonProperty("description") val description: String?,
@JsonProperty("manifestVersion") val manifestVersion: Int,
@JsonProperty("pluginLists") val pluginLists: List<String>
)
data class SitePlugin(
@JsonProperty("url") val url: String,
@JsonProperty("tvTypes") val tvTypes: List<String>?,
@JsonProperty("version") val version: Int,
@JsonProperty("apiVersion") val apiVersion: Int,
@JsonProperty("name") val name: String,
@JsonProperty("authors") val authors: List<String>,
@JsonProperty("description") val description: String?,
@JsonProperty("repositoryUrl") val repositoryUrl: String?,
@JsonProperty("language") val language: String?,
@JsonProperty("iconUrl") val iconUrl: String?
)
object RepositoryParser {
private suspend fun parseRepository(url: String): Repository? {
return suspendSafeApiCall {
// Take manifestVersion and such into account later
app.get(url).parsedSafe()
}
}
private suspend fun parsePlugins(pluginUrls: String): ArrayList<SitePlugin>? {
// Take manifestVersion and such into account later
val response = app.get(pluginUrls)
// Normal parsed function not working?
// return response.parsedSafe()
return tryParseJson<ArrayList<SitePlugin>>(response.text)
}
suspend fun getRepoPlugins(repositoryUrl: String): List<SitePlugin>? {
val repo = parseRepository(repositoryUrl) ?: return null
return repo.pluginLists.apmap {
parsePlugins(it)
}.filterNotNull().flatten()
}
private suspend fun downloadSiteTemp(context: Context, pluginUrl: String, name: String): File? {
return suspendSafeApiCall {
val dir = context.cacheDir
val file = File.createTempFile(name, ".cs3", dir)
val body = app.get(pluginUrl).okhttpResponse.body
write(body.byteStream(), file.outputStream())
file
}
}
suspend fun loadSiteTemp(context: Context, pluginUrl: String, name: String) {
val file = downloadSiteTemp(context, pluginUrl, name)
PluginManager.loadPlugin(context, file ?: return)
}
private fun write(stream: InputStream, output: OutputStream) {
val input = BufferedInputStream(stream)
val dataBuffer = ByteArray(512)
var readBytes: Int
while (input.read(dataBuffer).also { readBytes = it } != -1) {
output.write(dataBuffer, 0, readBytes)
}
}
}