From e10cb7b0a36456ff0652429e6592af5879b2912c Mon Sep 17 00:00:00 2001 From: Blatzar <46196380+Blatzar@users.noreply.github.com> Date: Sat, 6 Aug 2022 20:27:56 +0200 Subject: [PATCH] Added RepositoryParser --- .../lagradost/cloudstream3/MainActivity.kt | 13 ++- .../cloudstream3/plugins/PluginManager.kt | 3 +- .../cloudstream3/plugins/RepositoryParser.kt | 82 +++++++++++++++++++ 3 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/com/lagradost/cloudstream3/plugins/RepositoryParser.kt diff --git a/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt b/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt index d691bd02..d61e8e8e 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt @@ -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()) { diff --git a/app/src/main/java/com/lagradost/cloudstream3/plugins/PluginManager.kt b/app/src/main/java/com/lagradost/cloudstream3/plugins/PluginManager.kt index 3a8c34b0..4d9ed158 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/plugins/PluginManager.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/plugins/PluginManager.kt @@ -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 { diff --git a/app/src/main/java/com/lagradost/cloudstream3/plugins/RepositoryParser.kt b/app/src/main/java/com/lagradost/cloudstream3/plugins/RepositoryParser.kt new file mode 100644 index 00000000..37a20bd0 --- /dev/null +++ b/app/src/main/java/com/lagradost/cloudstream3/plugins/RepositoryParser.kt @@ -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 +) + +data class SitePlugin( + @JsonProperty("url") val url: String, + @JsonProperty("tvTypes") val tvTypes: List?, + @JsonProperty("version") val version: Int, + @JsonProperty("apiVersion") val apiVersion: Int, + @JsonProperty("name") val name: String, + @JsonProperty("authors") val authors: List, + @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? { + // Take manifestVersion and such into account later + val response = app.get(pluginUrls) + // Normal parsed function not working? +// return response.parsedSafe() + return tryParseJson>(response.text) + } + + suspend fun getRepoPlugins(repositoryUrl: String): List? { + 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) + } + } +} \ No newline at end of file