From 2de76ddd341b94eac594af95a332b18dd6a33d75 Mon Sep 17 00:00:00 2001 From: Antony Date: Sun, 21 Aug 2022 00:21:30 +0200 Subject: [PATCH 01/20] BackupGithub and restorePromptGithub in BackupUtils.kt added GithubApi in SettingsAccount.kt --- app/build.gradle | 3 + .../syncproviders/AccountManager.kt | 3 +- .../syncproviders/providers/GithubApi.kt | 41 +++++++++++ .../ui/settings/SettingsAccount.kt | 3 + .../cloudstream3/utils/BackupUtils.kt | 71 ++++++++++++++++++- app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/settings_account.xml | 39 +++++----- 7 files changed, 141 insertions(+), 20 deletions(-) create mode 100644 app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt diff --git a/app/build.gradle b/app/build.gradle index 1aa62378..5fecb182 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -199,6 +199,9 @@ dependencies { // Library/extensions searching with Levenshtein distance implementation 'me.xdrop:fuzzywuzzy:1.4.0' + + // Git + implementation 'org.openl.jgit:org.eclipse.jgit:6.2.0.202206071550-openl' } task androidSourcesJar(type: Jar) { diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/AccountManager.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/AccountManager.kt index 2bc39b54..daffa17d 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/AccountManager.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/AccountManager.kt @@ -11,6 +11,7 @@ abstract class AccountManager(private val defIndex: Int) : AuthAPI { val malApi = MALApi(0) val aniListApi = AniListApi(0) val openSubtitlesApi = OpenSubtitlesApi(0) + val githubApi = GithubApi(0) val indexSubtitlesApi = IndexSubtitleApi() // used to login via app intent @@ -22,7 +23,7 @@ abstract class AccountManager(private val defIndex: Int) : AuthAPI { // this needs init with context and can be accessed in settings val accountManagers get() = listOf( - malApi, aniListApi, openSubtitlesApi, //nginxApi + malApi, aniListApi, openSubtitlesApi, githubApi //nginxApi ) // used for active syncing diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt new file mode 100644 index 00000000..b0b8b070 --- /dev/null +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt @@ -0,0 +1,41 @@ +package com.lagradost.cloudstream3.syncproviders.providers + +import com.lagradost.cloudstream3.* +import com.lagradost.cloudstream3.AcraApplication.Companion.getKey +import com.lagradost.cloudstream3.subtitles.AbstractSubApi +import com.lagradost.cloudstream3.syncproviders.AuthAPI +import com.lagradost.cloudstream3.syncproviders.InAppAuthAPI +import com.lagradost.cloudstream3.syncproviders.InAppAuthAPIManager + + +class GithubApi(index: Int) : InAppAuthAPIManager(index), AbstractSubApi { + override val idPrefix = "Github" + override val name = "Github" + override val icon = R.drawable.ic_github_logo + override val requiresPassword = true + override val requiresUsername = true + override val createAccountUrl = "https://github.com/settings/tokens/new" + + data class GithubOAuthEntity( + var repository: String, + var access_token: String, + ) + private fun getAuthKey(): GithubOAuthEntity? { + return getKey(accountId, OpenSubtitlesApi.OPEN_SUBTITLES_USER_KEY) + } + override fun loginInfo(): AuthAPI.LoginInfo? { + getAuthKey()?.let { user -> + return AuthAPI.LoginInfo( + profilePicture = null, + name = user.repository, + accountIndex = accountIndex + ) + } + return null + } + override fun getLatestLoginData(): InAppAuthAPI.LoginData? { + val current = getAuthKey() ?: return null + return InAppAuthAPI.LoginData(username = current.repository, current.access_token) + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsAccount.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsAccount.kt index 7d809187..b56ce338 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsAccount.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsAccount.kt @@ -21,6 +21,7 @@ import com.lagradost.cloudstream3.syncproviders.AccountManager import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.aniListApi import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.malApi import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.openSubtitlesApi +import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.githubApi import com.lagradost.cloudstream3.syncproviders.AuthAPI import com.lagradost.cloudstream3.syncproviders.InAppAuthAPI import com.lagradost.cloudstream3.syncproviders.OAuth2API @@ -219,6 +220,8 @@ class SettingsAccount : PreferenceFragmentCompat() { R.string.mal_key to malApi, R.string.anilist_key to aniListApi, R.string.opensubtitles_key to openSubtitlesApi, + R.string.github_key to githubApi + ) for ((key, api) in syncApis) { diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt index 48d96bb5..cbbe9231 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt @@ -13,7 +13,6 @@ import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.module.kotlin.readValue import com.lagradost.cloudstream3.CommonActivity.showToast import com.lagradost.cloudstream3.R -import com.lagradost.cloudstream3.apmap import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.plugins.PLUGINS_KEY import com.lagradost.cloudstream3.plugins.PLUGINS_KEY_LOCAL @@ -28,6 +27,12 @@ import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_S import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_TOKEN_KEY import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_UNIXTIME_KEY import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_USER_KEY +import com.lagradost.cloudstream3.utils.AppUtils.parseJson +import com.lagradost.cloudstream3.utils.AppUtils.toJson +import com.lagradost.cloudstream3.utils.BackupUtils.restore +import com.lagradost.cloudstream3.utils.BackupUtils.restorePromptGithub + +import com.lagradost.cloudstream3.utils.Coroutines.ioSafe import com.lagradost.cloudstream3.utils.DataStore.getDefaultSharedPrefs import com.lagradost.cloudstream3.utils.DataStore.getSharedPrefs import com.lagradost.cloudstream3.utils.DataStore.mapper @@ -36,6 +41,9 @@ import com.lagradost.cloudstream3.utils.UIHelper.checkWrite import com.lagradost.cloudstream3.utils.UIHelper.requestRW import com.lagradost.cloudstream3.utils.VideoDownloadManager.getBasePath import com.lagradost.cloudstream3.utils.VideoDownloadManager.isDownloadDir +import org.eclipse.jgit.api.Git +import org.eclipse.jgit.transport.URIish +import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider import java.io.IOException import java.io.PrintWriter import java.lang.System.currentTimeMillis @@ -278,4 +286,65 @@ object BackupUtils { setKeyRaw(it.key, it.value, isEditingAppSettings) } } + + + fun FragmentActivity.BackupGithub(){ + val backup = this.getBackup() + ioSafe { + val tmpDir = createTempDir() + val git = Git.cloneRepository() + .setURI("https://github.com/Github_USERNAME/Repo_NAME.git") + .setDirectory(tmpDir) + .setTimeout(30) + .setCredentialsProvider( + UsernamePasswordCredentialsProvider("HERE GOES GITHUB TOKEN", "") + ) + .call() + + tmpDir.listFiles()?.first { it.name != ".git" }?.writeText(backup.toJson()) + git.add() + .addFilepattern(".") + .call() + git.commit() + .setAll(true) + .setMessage("Update results") + .call() + git.remoteAdd() + .setName("origin") + .setUri(URIish("https://github.com/Github_USERNAME/Repo_NAME.git")) + .call() + git.push() + .setRemote("https://github.com/Github_USERNAME/Repo_NAME.git") + .setTimeout(30) + .setCredentialsProvider( + UsernamePasswordCredentialsProvider("HERE GOES GITHUB TOKEN", "") + ) + .call(); + } + + + + } + + fun FragmentActivity.restorePromptGithub(){ + ioSafe { + val tmpDir = createTempDir() + Git.cloneRepository() + .setURI("https://github.com/Github_USERNAME/Repo_NAME.git") + .setDirectory(tmpDir) + .setTimeout(30) + .setCredentialsProvider( + UsernamePasswordCredentialsProvider("", "") + ) + .call() + val jsondata = tmpDir.listFiles()?.first { it.name != ".git" }?.readLines() + ?.joinToString() + val data = parseJson(jsondata?: "") + this@restorePromptGithub.restore( + data, + restoreSettings = true, + restoreDataStore = true + ) + } + } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 873de287..9ee4189d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -459,6 +459,7 @@ anilist_key mal_key opensubtitles_key + github_key nginx_key password123 MyCoolUsername diff --git a/app/src/main/res/xml/settings_account.xml b/app/src/main/res/xml/settings_account.xml index d4bae8c4..754f895c 100644 --- a/app/src/main/res/xml/settings_account.xml +++ b/app/src/main/res/xml/settings_account.xml @@ -1,27 +1,30 @@ + xmlns:app="http://schemas.android.com/apk/res-auto"> + android:key="@string/mal_key" + android:icon="@drawable/mal_logo" /> + android:key="@string/anilist_key" + android:icon="@drawable/ic_anilist_icon" /> - - - + android:key="@string/opensubtitles_key" + android:icon="@drawable/open_subtitles_icon" /> + + + + - - - - - - - - + + + + + + + + \ No newline at end of file From 798ff9a436e5b12722a86f20ad415b06b074c1ef Mon Sep 17 00:00:00 2001 From: Antony Date: Sun, 21 Aug 2022 00:38:34 +0200 Subject: [PATCH 02/20] remove AbstractSubApi from GithubApi.kt --- .../lagradost/cloudstream3/syncproviders/providers/GithubApi.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt index b0b8b070..cfdbd24f 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt @@ -8,7 +8,7 @@ import com.lagradost.cloudstream3.syncproviders.InAppAuthAPI import com.lagradost.cloudstream3.syncproviders.InAppAuthAPIManager -class GithubApi(index: Int) : InAppAuthAPIManager(index), AbstractSubApi { +class GithubApi(index: Int) : InAppAuthAPIManager(index){ override val idPrefix = "Github" override val name = "Github" override val icon = R.drawable.ic_github_logo From 9c6465c26e4f10a3f5d8ba100742de02b3ce66b4 Mon Sep 17 00:00:00 2001 From: Antony Date: Mon, 22 Aug 2022 00:22:54 +0200 Subject: [PATCH 03/20] GithubApi.kt fixes --- .../syncproviders/providers/GithubApi.kt | 52 +++++++++++++++---- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt index cfdbd24f..08b3fdc2 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt @@ -2,7 +2,8 @@ package com.lagradost.cloudstream3.syncproviders.providers import com.lagradost.cloudstream3.* import com.lagradost.cloudstream3.AcraApplication.Companion.getKey -import com.lagradost.cloudstream3.subtitles.AbstractSubApi +import com.lagradost.cloudstream3.AcraApplication.Companion.setKey +import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.syncproviders.AuthAPI import com.lagradost.cloudstream3.syncproviders.InAppAuthAPI import com.lagradost.cloudstream3.syncproviders.InAppAuthAPIManager @@ -17,25 +18,54 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ override val createAccountUrl = "https://github.com/settings/tokens/new" data class GithubOAuthEntity( - var repository: String, - var access_token: String, + var user: String, + var pass: String ) - private fun getAuthKey(): GithubOAuthEntity? { - return getKey(accountId, OpenSubtitlesApi.OPEN_SUBTITLES_USER_KEY) + companion object { + const val GITHUB_USER_KEY: String = "github_user" // user data like profile + var currentSession: GithubOAuthEntity? = null } + private fun getAuthKey(): GithubOAuthEntity? { + return getKey(accountId, GITHUB_USER_KEY) + } + override suspend fun login(data: InAppAuthAPI.LoginData): Boolean { + switchToNewAccount() + val username = data.username ?: throw ErrorLoadingException("Requires Username") + val password = data.password ?: throw ErrorLoadingException("Requires Password") + try { + setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity(username, password)) + registerAccount() + return true + } catch (e: Exception) { + logError(e) + switchToOldAccount() + } + switchToOldAccount() + return false + } + + override fun getLatestLoginData(): InAppAuthAPI.LoginData? { + val current = getAuthKey() ?: return null + return InAppAuthAPI.LoginData(username = current.user, current.pass) + } + override suspend fun initialize() { + currentSession = getAuthKey() ?: return // just in case the following fails + setKey(currentSession!!.user, currentSession!!.pass) + } + override fun logOut() { + AcraApplication.removeKey(accountId, GITHUB_USER_KEY) + removeAccountKeys() + currentSession = getAuthKey() + } + override fun loginInfo(): AuthAPI.LoginInfo? { getAuthKey()?.let { user -> return AuthAPI.LoginInfo( profilePicture = null, - name = user.repository, + name = user.user, accountIndex = accountIndex ) } return null } - override fun getLatestLoginData(): InAppAuthAPI.LoginData? { - val current = getAuthKey() ?: return null - return InAppAuthAPI.LoginData(username = current.repository, current.access_token) - } - } \ No newline at end of file From 24009c5caa2173a998eab111288dec152f19ff89 Mon Sep 17 00:00:00 2001 From: Antony Date: Mon, 22 Aug 2022 00:33:39 +0200 Subject: [PATCH 04/20] IllegalArgumentException --- .../cloudstream3/syncproviders/providers/GithubApi.kt | 4 ++-- .../syncproviders/providers/OpenSubtitlesApi.kt | 4 ++-- .../java/com/lagradost/cloudstream3/utils/BackupUtils.kt | 8 +++++++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt index 08b3fdc2..9666dc34 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt @@ -30,8 +30,8 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ } override suspend fun login(data: InAppAuthAPI.LoginData): Boolean { switchToNewAccount() - val username = data.username ?: throw ErrorLoadingException("Requires Username") - val password = data.password ?: throw ErrorLoadingException("Requires Password") + val username = data.username ?: throw IllegalArgumentException ("Requires Username") + val password = data.password ?: throw IllegalArgumentException ("Requires Password") try { setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity(username, password)) registerAccount() diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/OpenSubtitlesApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/OpenSubtitlesApi.kt index bfa65f62..e764f240 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/OpenSubtitlesApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/OpenSubtitlesApi.kt @@ -124,8 +124,8 @@ class OpenSubtitlesApi(index: Int) : InAppAuthAPIManager(index), AbstractSubApi } override suspend fun login(data: InAppAuthAPI.LoginData): Boolean { - val username = data.username ?: throw ErrorLoadingException("Requires Username") - val password = data.password ?: throw ErrorLoadingException("Requires Password") + val username = data.username ?: throw IllegalArgumentException ("Requires Username") + val password = data.password ?: throw IllegalArgumentException ("Requires Password") switchToNewAccount() try { if (initLogin(username, password)) { diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt index cbbe9231..57ca32b4 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt @@ -27,6 +27,8 @@ import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_S import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_TOKEN_KEY import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_UNIXTIME_KEY import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_USER_KEY +import com.lagradost.cloudstream3.syncproviders.providers.OpenSubtitlesApi +import com.lagradost.cloudstream3.syncproviders.providers.OpenSubtitlesApi.Companion.OPEN_SUBTITLES_USER_KEY import com.lagradost.cloudstream3.utils.AppUtils.parseJson import com.lagradost.cloudstream3.utils.AppUtils.toJson import com.lagradost.cloudstream3.utils.BackupUtils.restore @@ -71,7 +73,11 @@ object BackupUtils { // The plugins themselves are not backed up PLUGINS_KEY, - PLUGINS_KEY_LOCAL + PLUGINS_KEY_LOCAL, + + OPEN_SUBTITLES_USER_KEY, + "nginx_user", // Nginx user key + ) /** false if blacklisted key */ From 2e5e5020df3c4d22ecba23721ba758410ec7447c Mon Sep 17 00:00:00 2001 From: Antony Date: Mon, 22 Aug 2022 02:24:42 +0200 Subject: [PATCH 05/20] BackupGithub fix --- .../syncproviders/providers/GithubApi.kt | 12 +++++------ .../ui/settings/SettingsUpdates.kt | 3 ++- .../cloudstream3/utils/BackupUtils.kt | 20 ++++++++++++------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt index 9666dc34..c7c7e10b 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt @@ -19,7 +19,7 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ data class GithubOAuthEntity( var user: String, - var pass: String + var token: String ) companion object { const val GITHUB_USER_KEY: String = "github_user" // user data like profile @@ -30,10 +30,10 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ } override suspend fun login(data: InAppAuthAPI.LoginData): Boolean { switchToNewAccount() - val username = data.username ?: throw IllegalArgumentException ("Requires Username") + val repoUrl = data.username ?: throw IllegalArgumentException ("Requires Username") val password = data.password ?: throw IllegalArgumentException ("Requires Password") try { - setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity(username, password)) + setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity(repoUrl, password)) registerAccount() return true } catch (e: Exception) { @@ -46,11 +46,11 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ override fun getLatestLoginData(): InAppAuthAPI.LoginData? { val current = getAuthKey() ?: return null - return InAppAuthAPI.LoginData(username = current.user, current.pass) + return InAppAuthAPI.LoginData(username = current.user, password = current.token) } override suspend fun initialize() { currentSession = getAuthKey() ?: return // just in case the following fails - setKey(currentSession!!.user, currentSession!!.pass) + setKey(currentSession!!.user, currentSession!!.token) } override fun logOut() { AcraApplication.removeKey(accountId, GITHUB_USER_KEY) @@ -63,7 +63,7 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ return AuthAPI.LoginInfo( profilePicture = null, name = user.user, - accountIndex = accountIndex + accountIndex = accountIndex, ) } return null diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt index 6b19042a..9cabd473 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt @@ -15,6 +15,7 @@ import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.getPref import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setPaddingBottom import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setUpToolbar +import com.lagradost.cloudstream3.utils.BackupUtils.BackupGithub import com.lagradost.cloudstream3.utils.BackupUtils.backup import com.lagradost.cloudstream3.utils.BackupUtils.restorePrompt import com.lagradost.cloudstream3.utils.Coroutines.ioSafe @@ -41,7 +42,7 @@ class SettingsUpdates : PreferenceFragmentCompat() { //val settingsManager = PreferenceManager.getDefaultSharedPreferences(requireContext()) getPref(R.string.backup_key)?.setOnPreferenceClickListener { - activity?.backup() + activity?.BackupGithub() return@setOnPreferenceClickListener true } diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt index 57ca32b4..7257da58 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt @@ -16,11 +16,13 @@ import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.plugins.PLUGINS_KEY import com.lagradost.cloudstream3.plugins.PLUGINS_KEY_LOCAL +import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.githubApi import com.lagradost.cloudstream3.syncproviders.providers.AniListApi.Companion.ANILIST_CACHED_LIST import com.lagradost.cloudstream3.syncproviders.providers.AniListApi.Companion.ANILIST_SHOULD_UPDATE_LIST import com.lagradost.cloudstream3.syncproviders.providers.AniListApi.Companion.ANILIST_TOKEN_KEY import com.lagradost.cloudstream3.syncproviders.providers.AniListApi.Companion.ANILIST_UNIXTIME_KEY import com.lagradost.cloudstream3.syncproviders.providers.AniListApi.Companion.ANILIST_USER_KEY +import com.lagradost.cloudstream3.syncproviders.providers.GithubApi import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_CACHED_LIST import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_REFRESH_TOKEN_KEY import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_SHOULD_UPDATE_LIST @@ -298,12 +300,14 @@ object BackupUtils { val backup = this.getBackup() ioSafe { val tmpDir = createTempDir() + val repoUrl = githubApi.getLatestLoginData()?.username ?: throw IllegalArgumentException ("Requires Username") + val token = githubApi.getLatestLoginData()?.password ?: throw IllegalArgumentException ("Requires Username") val git = Git.cloneRepository() - .setURI("https://github.com/Github_USERNAME/Repo_NAME.git") + .setURI("$repoUrl.git") .setDirectory(tmpDir) .setTimeout(30) .setCredentialsProvider( - UsernamePasswordCredentialsProvider("HERE GOES GITHUB TOKEN", "") + UsernamePasswordCredentialsProvider(token, "") ) .call() @@ -317,13 +321,13 @@ object BackupUtils { .call() git.remoteAdd() .setName("origin") - .setUri(URIish("https://github.com/Github_USERNAME/Repo_NAME.git")) + .setUri(URIish("$repoUrl.git")) .call() git.push() - .setRemote("https://github.com/Github_USERNAME/Repo_NAME.git") + .setRemote("$repoUrl.git") .setTimeout(30) .setCredentialsProvider( - UsernamePasswordCredentialsProvider("HERE GOES GITHUB TOKEN", "") + UsernamePasswordCredentialsProvider(token, "") ) .call(); } @@ -335,12 +339,14 @@ object BackupUtils { fun FragmentActivity.restorePromptGithub(){ ioSafe { val tmpDir = createTempDir() + val repoUrl = githubApi.getLatestLoginData()?.username ?: throw IllegalArgumentException ("Requires Username") + val token = githubApi.getLatestLoginData()?.password ?: throw IllegalArgumentException ("Requires Username") Git.cloneRepository() - .setURI("https://github.com/Github_USERNAME/Repo_NAME.git") + .setURI("$repoUrl.git") .setDirectory(tmpDir) .setTimeout(30) .setCredentialsProvider( - UsernamePasswordCredentialsProvider("", "") + UsernamePasswordCredentialsProvider("$token", "") ) .call() val jsondata = tmpDir.listFiles()?.first { it.name != ".git" }?.readLines() From e12efc13ffe85299700d1be8980682cb48f8ac01 Mon Sep 17 00:00:00 2001 From: Antony Date: Tue, 23 Aug 2022 18:54:22 +0200 Subject: [PATCH 06/20] Create new repo --- .../syncproviders/providers/GithubApi.kt | 89 ++++++++++++++++--- .../ui/settings/SettingsUpdates.kt | 2 +- .../cloudstream3/utils/BackupUtils.kt | 11 ++- 3 files changed, 85 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt index c7c7e10b..316bd927 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt @@ -1,5 +1,6 @@ package com.lagradost.cloudstream3.syncproviders.providers +import com.fasterxml.jackson.annotation.JsonProperty import com.lagradost.cloudstream3.* import com.lagradost.cloudstream3.AcraApplication.Companion.getKey import com.lagradost.cloudstream3.AcraApplication.Companion.setKey @@ -7,6 +8,19 @@ import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.syncproviders.AuthAPI import com.lagradost.cloudstream3.syncproviders.InAppAuthAPI import com.lagradost.cloudstream3.syncproviders.InAppAuthAPIManager +import com.lagradost.cloudstream3.utils.AppUtils.parseJson +import com.lagradost.cloudstream3.utils.AppUtils.toJson +import com.lagradost.cloudstream3.utils.AppUtils.tryParseJson +import com.lagradost.cloudstream3.utils.Coroutines.ioSafe +import com.lagradost.nicehttp.RequestBodyTypes +import okhttp3.MediaType.Companion.toMediaTypeOrNull +import okhttp3.RequestBody.Companion.toRequestBody +import org.eclipse.jgit.api.Git +import org.eclipse.jgit.transport.URIish +import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.attribute.FileAttribute class GithubApi(index: Int) : InAppAuthAPIManager(index){ @@ -14,11 +28,10 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ override val name = "Github" override val icon = R.drawable.ic_github_logo override val requiresPassword = true - override val requiresUsername = true override val createAccountUrl = "https://github.com/settings/tokens/new" data class GithubOAuthEntity( - var user: String, + var repoUrl: String, var token: String ) companion object { @@ -28,14 +41,70 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ private fun getAuthKey(): GithubOAuthEntity? { return getKey(accountId, GITHUB_USER_KEY) } + private class repodata ( + @JsonProperty("full_name") val repoUrl: String + ) + + + private suspend fun initLogin(githubToken: String): Boolean{ + val response = app.post("https://api.github.com/user/repos", + headers= mapOf( + Pair("Accept" , "application/vnd.github+json"), + Pair("Authorization", "token $githubToken"), + ), + requestBody = """{"name":"sync data for Cloudstream", "description": "Private repo for cloudstream Account", "private": true}""".toRequestBody( + RequestBodyTypes.JSON.toMediaTypeOrNull())) + + if (response.isSuccessful) { + val repoUrl = tryParseJson(response.text).let { + setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity( + token = githubToken, + repoUrl = it?.repoUrl?: run { + return false + })) + it.repoUrl + } + val tmpDir = createTempDir() + val git = Git.cloneRepository() + .setURI("https://github.com/$repoUrl.git") + .setDirectory(tmpDir) + .setTimeout(30) + .setCredentialsProvider( + UsernamePasswordCredentialsProvider(githubToken, "") + ) + .call() + createTempFile("backup", "txt", tmpDir) + git.add() + .addFilepattern(".") + .call() + git.commit() + .setAll(true) + .setMessage("Update backup") + .call() + git.remoteAdd() + .setName("origin") + .setUri(URIish("https://github.com/$repoUrl.git")) + .call() + git.push() + .setRemote("https://github.com/$repoUrl.git") + .setTimeout(30) + .setCredentialsProvider( + UsernamePasswordCredentialsProvider(githubToken, "") + ) + .call(); + return true + } + return false + } + override suspend fun login(data: InAppAuthAPI.LoginData): Boolean { switchToNewAccount() - val repoUrl = data.username ?: throw IllegalArgumentException ("Requires Username") - val password = data.password ?: throw IllegalArgumentException ("Requires Password") + val githubToken = data.password ?: throw IllegalArgumentException ("Requires Password") try { - setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity(repoUrl, password)) - registerAccount() - return true + if (initLogin(githubToken)) { + registerAccount() + return true + } } catch (e: Exception) { logError(e) switchToOldAccount() @@ -46,11 +115,11 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ override fun getLatestLoginData(): InAppAuthAPI.LoginData? { val current = getAuthKey() ?: return null - return InAppAuthAPI.LoginData(username = current.user, password = current.token) + return InAppAuthAPI.LoginData(username = current.repoUrl, password = current.token) } override suspend fun initialize() { currentSession = getAuthKey() ?: return // just in case the following fails - setKey(currentSession!!.user, currentSession!!.token) + setKey(currentSession!!.repoUrl, currentSession!!.token) } override fun logOut() { AcraApplication.removeKey(accountId, GITHUB_USER_KEY) @@ -62,7 +131,7 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ getAuthKey()?.let { user -> return AuthAPI.LoginInfo( profilePicture = null, - name = user.user, + name = user.repoUrl, accountIndex = accountIndex, ) } diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt index 9cabd473..9700961b 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt @@ -42,7 +42,7 @@ class SettingsUpdates : PreferenceFragmentCompat() { //val settingsManager = PreferenceManager.getDefaultSharedPreferences(requireContext()) getPref(R.string.backup_key)?.setOnPreferenceClickListener { - activity?.BackupGithub() + activity?.backup() return@setOnPreferenceClickListener true } diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt index 7257da58..5fcd5cf0 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt @@ -303,28 +303,27 @@ object BackupUtils { val repoUrl = githubApi.getLatestLoginData()?.username ?: throw IllegalArgumentException ("Requires Username") val token = githubApi.getLatestLoginData()?.password ?: throw IllegalArgumentException ("Requires Username") val git = Git.cloneRepository() - .setURI("$repoUrl.git") + .setURI("https://github.com/$repoUrl.git") .setDirectory(tmpDir) .setTimeout(30) .setCredentialsProvider( UsernamePasswordCredentialsProvider(token, "") ) .call() - tmpDir.listFiles()?.first { it.name != ".git" }?.writeText(backup.toJson()) git.add() .addFilepattern(".") .call() git.commit() .setAll(true) - .setMessage("Update results") + .setMessage("Update backup") .call() git.remoteAdd() .setName("origin") - .setUri(URIish("$repoUrl.git")) + .setUri(URIish("https://github.com/$repoUrl.git")) .call() git.push() - .setRemote("$repoUrl.git") + .setRemote("https://github.com/$repoUrl.git") .setTimeout(30) .setCredentialsProvider( UsernamePasswordCredentialsProvider(token, "") @@ -342,7 +341,7 @@ object BackupUtils { val repoUrl = githubApi.getLatestLoginData()?.username ?: throw IllegalArgumentException ("Requires Username") val token = githubApi.getLatestLoginData()?.password ?: throw IllegalArgumentException ("Requires Username") Git.cloneRepository() - .setURI("$repoUrl.git") + .setURI("https://github.com/$repoUrl.git") .setDirectory(tmpDir) .setTimeout(30) .setCredentialsProvider( From 6ab90504f4a1a1ef7ec987fdd10a11ecc5d291e3 Mon Sep 17 00:00:00 2001 From: Antony Date: Wed, 24 Aug 2022 02:38:49 +0200 Subject: [PATCH 07/20] fixes --- .../syncproviders/providers/GithubApi.kt | 102 ++++++++++-------- .../cloudstream3/utils/BackupUtils.kt | 11 +- 2 files changed, 60 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt index 316bd927..86148a27 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt @@ -3,6 +3,7 @@ package com.lagradost.cloudstream3.syncproviders.providers import com.fasterxml.jackson.annotation.JsonProperty import com.lagradost.cloudstream3.* import com.lagradost.cloudstream3.AcraApplication.Companion.getKey +import com.lagradost.cloudstream3.AcraApplication.Companion.removeKey import com.lagradost.cloudstream3.AcraApplication.Companion.setKey import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.syncproviders.AuthAPI @@ -46,55 +47,61 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ ) + private fun commitFile(repoUrl: String, githubToken: String){ + val tmpDir = createTempDir() + val git = Git.cloneRepository() + .setURI("https://github.com/$repoUrl.git") + .setDirectory(tmpDir) + .setTimeout(30) + .setCredentialsProvider( + UsernamePasswordCredentialsProvider(githubToken, "") + ) + .call() + createTempFile("backup", "txt", tmpDir) + git.add() + .addFilepattern(".") + .call() + git.commit() + .setAll(true) + .setMessage("Update backup") + .call() + git.remoteAdd() + .setName("origin") + .setUri(URIish("https://github.com/$repoUrl.git")) + .call() + git.push() + .setRemote("https://github.com/$repoUrl.git") + .setTimeout(30) + .setCredentialsProvider( + UsernamePasswordCredentialsProvider(githubToken, "") + ) + .call(); + tmpDir.delete() + } +private class repos{ + +} private suspend fun initLogin(githubToken: String): Boolean{ val response = app.post("https://api.github.com/user/repos", headers= mapOf( - Pair("Accept" , "application/vnd.github+json"), - Pair("Authorization", "token $githubToken"), + "Accept" to "application/vnd.github+json", + "Authorization" to "token $githubToken" ), requestBody = """{"name":"sync data for Cloudstream", "description": "Private repo for cloudstream Account", "private": true}""".toRequestBody( RequestBodyTypes.JSON.toMediaTypeOrNull())) - if (response.isSuccessful) { - val repoUrl = tryParseJson(response.text).let { - setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity( - token = githubToken, - repoUrl = it?.repoUrl?: run { - return false - })) - it.repoUrl - } - val tmpDir = createTempDir() - val git = Git.cloneRepository() - .setURI("https://github.com/$repoUrl.git") - .setDirectory(tmpDir) - .setTimeout(30) - .setCredentialsProvider( - UsernamePasswordCredentialsProvider(githubToken, "") - ) - .call() - createTempFile("backup", "txt", tmpDir) - git.add() - .addFilepattern(".") - .call() - git.commit() - .setAll(true) - .setMessage("Update backup") - .call() - git.remoteAdd() - .setName("origin") - .setUri(URIish("https://github.com/$repoUrl.git")) - .call() - git.push() - .setRemote("https://github.com/$repoUrl.git") - .setTimeout(30) - .setCredentialsProvider( - UsernamePasswordCredentialsProvider(githubToken, "") - ) - .call(); - return true + if (!response.isSuccessful) {return false} + + val repoUrl = tryParseJson(response.text).let { + setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity( + token = githubToken, + repoUrl = it?.repoUrl?: run { + return false + })) + it.repoUrl } - return false + commitFile(repoUrl, githubToken) + return true } override suspend fun login(data: InAppAuthAPI.LoginData): Boolean { @@ -118,23 +125,24 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ return InAppAuthAPI.LoginData(username = current.repoUrl, password = current.token) } override suspend fun initialize() { - currentSession = getAuthKey() ?: return // just in case the following fails - setKey(currentSession!!.repoUrl, currentSession!!.token) + currentSession = getAuthKey() + val repoUrl = currentSession?.repoUrl ?: return + val token = currentSession?.token ?: return + setKey(repoUrl, token) } override fun logOut() { - AcraApplication.removeKey(accountId, GITHUB_USER_KEY) + removeKey(accountId, GITHUB_USER_KEY) removeAccountKeys() currentSession = getAuthKey() } override fun loginInfo(): AuthAPI.LoginInfo? { - getAuthKey()?.let { user -> - return AuthAPI.LoginInfo( + return getAuthKey()?.let { user -> + AuthAPI.LoginInfo( profilePicture = null, name = user.repoUrl, accountIndex = accountIndex, ) } - return null } } \ No newline at end of file diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt index 5fcd5cf0..363ca804 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt @@ -23,6 +23,7 @@ import com.lagradost.cloudstream3.syncproviders.providers.AniListApi.Companion.A import com.lagradost.cloudstream3.syncproviders.providers.AniListApi.Companion.ANILIST_UNIXTIME_KEY import com.lagradost.cloudstream3.syncproviders.providers.AniListApi.Companion.ANILIST_USER_KEY import com.lagradost.cloudstream3.syncproviders.providers.GithubApi +import com.lagradost.cloudstream3.syncproviders.providers.GithubApi.Companion.GITHUB_USER_KEY import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_CACHED_LIST import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_REFRESH_TOKEN_KEY import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_SHOULD_UPDATE_LIST @@ -76,7 +77,7 @@ object BackupUtils { // The plugins themselves are not backed up PLUGINS_KEY, PLUGINS_KEY_LOCAL, - + GITHUB_USER_KEY, OPEN_SUBTITLES_USER_KEY, "nginx_user", // Nginx user key @@ -335,7 +336,7 @@ object BackupUtils { } - fun FragmentActivity.restorePromptGithub(){ + fun FragmentActivity.restorePromptGithub() = ioSafe { val tmpDir = createTempDir() val repoUrl = githubApi.getLatestLoginData()?.username ?: throw IllegalArgumentException ("Requires Username") @@ -348,8 +349,7 @@ object BackupUtils { UsernamePasswordCredentialsProvider("$token", "") ) .call() - val jsondata = tmpDir.listFiles()?.first { it.name != ".git" }?.readLines() - ?.joinToString() + val jsondata = tmpDir.listFiles()?.first { it.name != ".git" }?.readText().toString() val data = parseJson(jsondata?: "") this@restorePromptGithub.restore( data, @@ -357,5 +357,4 @@ object BackupUtils { restoreDataStore = true ) } - } -} \ No newline at end of file +} From df54745294fc9539224174e9b32fe23da851ae45 Mon Sep 17 00:00:00 2001 From: Antony Date: Wed, 24 Aug 2022 02:42:41 +0200 Subject: [PATCH 08/20] fixes --- .../syncproviders/providers/GithubApi.kt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt index 86148a27..bea24033 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt @@ -78,10 +78,18 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ .call(); tmpDir.delete() } -private class repos{ + private class repos ( + @JsonProperty("full_name") var repoName: String + ) -} private suspend fun initLogin(githubToken: String): Boolean{ + val repoResponse = app.post("https://api.github.com/user/repos", + headers= mapOf( + "Accept" to "application/vnd.github+json", + "Authorization" to "token $githubToken" + ) + ) + val val response = app.post("https://api.github.com/user/repos", headers= mapOf( "Accept" to "application/vnd.github+json", From d9824b5b8ac91d1086efe9be251f92484cf4705e Mon Sep 17 00:00:00 2001 From: Antony Date: Wed, 24 Aug 2022 02:48:13 +0200 Subject: [PATCH 09/20] fixes --- .../syncproviders/providers/GithubApi.kt | 62 +++++++++++++------ 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt index bea24033..2189f6b8 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt @@ -78,7 +78,7 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ .call(); tmpDir.delete() } - private class repos ( + private class reposElements ( @JsonProperty("full_name") var repoName: String ) @@ -89,27 +89,49 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ "Authorization" to "token $githubToken" ) ) - val - val response = app.post("https://api.github.com/user/repos", - headers= mapOf( - "Accept" to "application/vnd.github+json", - "Authorization" to "token $githubToken" - ), - requestBody = """{"name":"sync data for Cloudstream", "description": "Private repo for cloudstream Account", "private": true}""".toRequestBody( - RequestBodyTypes.JSON.toMediaTypeOrNull())) + if (repoResponse.isSuccessful) { + val repo = tryParseJson>(repoResponse.text)?.filter { + it.repoName == "sync-data-for-Cloudstream" + } + if (repo?.isEmpty() == true) { + val response = app.post( + "https://api.github.com/user/repos", + headers = mapOf( + "Accept" to "application/vnd.github+json", + "Authorization" to "token $githubToken" + ), + requestBody = """{"name":"sync data for Cloudstream", "description": "Private repo for cloudstream Account", "private": true}""".toRequestBody( + RequestBodyTypes.JSON.toMediaTypeOrNull() + ) + ) - if (!response.isSuccessful) {return false} - - val repoUrl = tryParseJson(response.text).let { - setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity( - token = githubToken, - repoUrl = it?.repoUrl?: run { + if (!response.isSuccessful) { return false - })) - it.repoUrl - } - commitFile(repoUrl, githubToken) - return true + } + + val repoUrl = tryParseJson(response.text).let { + setKey( + accountId, GITHUB_USER_KEY, GithubOAuthEntity( + token = githubToken, + repoUrl = it?.repoUrl ?: run { + return false + }) + ) + it.repoUrl + } + commitFile(repoUrl, githubToken) + return true + } + else{ + repo.first().let { { + setKey( + accountId, GITHUB_USER_KEY, GithubOAuthEntity( + token = githubToken, + repoUrl = it?.repoName ?: run { + return false + }) + ) + } } override suspend fun login(data: InAppAuthAPI.LoginData): Boolean { From 98c85d7eb262f60248228b4cc4c67be1ad44a060 Mon Sep 17 00:00:00 2001 From: Antony Date: Wed, 24 Aug 2022 03:00:21 +0200 Subject: [PATCH 10/20] added check to previously made backups --- .../syncproviders/providers/GithubApi.kt | 84 ++++++++++--------- 1 file changed, 44 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt index 2189f6b8..bf66c86e 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt @@ -79,59 +79,63 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ tmpDir.delete() } private class reposElements ( - @JsonProperty("full_name") var repoName: String + @JsonProperty("full_name") var repoName: String, + @JsonProperty("name") var shortRepoName: String + ) private suspend fun initLogin(githubToken: String): Boolean{ - val repoResponse = app.post("https://api.github.com/user/repos", + val repoResponse = app.get("https://api.github.com/user/repos", headers= mapOf( "Accept" to "application/vnd.github+json", "Authorization" to "token $githubToken" ) ) - if (repoResponse.isSuccessful) { - val repo = tryParseJson>(repoResponse.text)?.filter { - it.repoName == "sync-data-for-Cloudstream" - } - if (repo?.isEmpty() == true) { - val response = app.post( - "https://api.github.com/user/repos", - headers = mapOf( - "Accept" to "application/vnd.github+json", - "Authorization" to "token $githubToken" - ), - requestBody = """{"name":"sync data for Cloudstream", "description": "Private repo for cloudstream Account", "private": true}""".toRequestBody( - RequestBodyTypes.JSON.toMediaTypeOrNull() - ) + if (!repoResponse.isSuccessful) { + return false + } + val repo = tryParseJson>(repoResponse.text)?.filter { + it.shortRepoName == "sync-data-for-Cloudstream" + } + if (repo?.isEmpty() == true) { + val response = app.post( + "https://api.github.com/user/repos", + headers = mapOf( + "Accept" to "application/vnd.github+json", + "Authorization" to "token $githubToken" + ), + requestBody = """{"name":"sync data for Cloudstream", "description": "Private repo for cloudstream Account", "private": true}""".toRequestBody( + RequestBodyTypes.JSON.toMediaTypeOrNull() ) + ) - if (!response.isSuccessful) { - return false - } + if (!response.isSuccessful) { + return false + } - val repoUrl = tryParseJson(response.text).let { - setKey( - accountId, GITHUB_USER_KEY, GithubOAuthEntity( - token = githubToken, - repoUrl = it?.repoUrl ?: run { - return false - }) - ) - it.repoUrl - } - commitFile(repoUrl, githubToken) + val repoUrl = tryParseJson(response.text).let { + setKey( + accountId, GITHUB_USER_KEY, GithubOAuthEntity( + token = githubToken, + repoUrl = it?.repoUrl ?: run { + return false + }) + ) + it.repoUrl + } + commitFile(repoUrl, githubToken) + return true + } + else{ + repo?.first().let { + setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity( + token = githubToken, + repoUrl = it?.repoName?: run { + return false + })) return true } - else{ - repo.first().let { { - setKey( - accountId, GITHUB_USER_KEY, GithubOAuthEntity( - token = githubToken, - repoUrl = it?.repoName ?: run { - return false - }) - ) - } + } } override suspend fun login(data: InAppAuthAPI.LoginData): Boolean { From c99c620e9803af513a4c7ab04796c2d6738feff9 Mon Sep 17 00:00:00 2001 From: antonydp <38143733+antonydp@users.noreply.github.com> Date: Wed, 24 Aug 2022 13:14:25 +0200 Subject: [PATCH 11/20] Update GithubApi.kt --- .../cloudstream3/syncproviders/providers/GithubApi.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt index bf66c86e..12dc3bae 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt @@ -148,7 +148,6 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ } } catch (e: Exception) { logError(e) - switchToOldAccount() } switchToOldAccount() return false @@ -179,4 +178,4 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ ) } } -} \ No newline at end of file +} From bdb6d78f97e966802e0330a16ab484204700dad7 Mon Sep 17 00:00:00 2001 From: Antony Date: Wed, 24 Aug 2022 22:49:08 +0200 Subject: [PATCH 12/20] added tmpDir.deleteRecursively() --- .../lagradost/cloudstream3/syncproviders/providers/GithubApi.kt | 2 +- .../main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt index bf66c86e..73160db4 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt @@ -76,7 +76,7 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ UsernamePasswordCredentialsProvider(githubToken, "") ) .call(); - tmpDir.delete() + tmpDir.deleteRecursively() } private class reposElements ( @JsonProperty("full_name") var repoName: String, diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt index 363ca804..01a807bf 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt @@ -330,6 +330,7 @@ object BackupUtils { UsernamePasswordCredentialsProvider(token, "") ) .call(); + tmpDir.deleteRecursively() } @@ -351,6 +352,7 @@ object BackupUtils { .call() val jsondata = tmpDir.listFiles()?.first { it.name != ".git" }?.readText().toString() val data = parseJson(jsondata?: "") + tmpDir.deleteRecursively() this@restorePromptGithub.restore( data, restoreSettings = true, From 409f8f743d2fa17b2e78f1d5808fc61432dd6d97 Mon Sep 17 00:00:00 2001 From: Antony Date: Thu, 25 Aug 2022 13:32:26 +0200 Subject: [PATCH 13/20] switch on gist.github --- .../syncproviders/providers/GithubApi.kt | 96 ++++++++++--------- .../cloudstream3/utils/BackupUtils.kt | 14 +-- 2 files changed, 57 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt index bbce21e9..72701cdd 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt @@ -33,7 +33,9 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ data class GithubOAuthEntity( var repoUrl: String, - var token: String + var token: String, + var userName: String, + var userAvatar: String ) companion object { const val GITHUB_USER_KEY: String = "github_user" // user data like profile @@ -42,9 +44,7 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ private fun getAuthKey(): GithubOAuthEntity? { return getKey(accountId, GITHUB_USER_KEY) } - private class repodata ( - @JsonProperty("full_name") val repoUrl: String - ) + private fun commitFile(repoUrl: String, githubToken: String){ @@ -78,66 +78,70 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ .call(); tmpDir.deleteRecursively() } - private class reposElements ( - @JsonProperty("full_name") var repoName: String, - @JsonProperty("name") var shortRepoName: String + private data class gistsElements ( + @JsonProperty("git_pull_url") val gitUrl: String, + @JsonProperty("files") val files: Map, + @JsonProperty("owner") val owner: OwnerData + ) + private data class OwnerData( + @JsonProperty("login") val userName: String, + @JsonProperty("avatar_url") val userAvatar : String + ) + data class File ( + @JsonProperty("filename") val filename: String ) private suspend fun initLogin(githubToken: String): Boolean{ - val repoResponse = app.get("https://api.github.com/user/repos", + val response = app.get("https://api.github.com/gists", headers= mapOf( - "Accept" to "application/vnd.github+json", - "Authorization" to "token $githubToken" + Pair("Accept" , "application/vnd.github+json"), + Pair("Authorization", "token $githubToken"), ) ) - if (!repoResponse.isSuccessful) { - return false + + if (!response.isSuccessful) { return false } + + val repo = tryParseJson>(response.text)?.filter { + it.files.keys.first() == "Cloudstream_Backup_data.txt" } - val repo = tryParseJson>(repoResponse.text)?.filter { - it.shortRepoName == "sync-data-for-Cloudstream" - } - if (repo?.isEmpty() == true) { - val response = app.post( - "https://api.github.com/user/repos", - headers = mapOf( - "Accept" to "application/vnd.github+json", - "Authorization" to "token $githubToken" + + if (repo?.isEmpty() == true){ + val gitresponse = app.post("https://api.github.com/gists", + headers= mapOf( + Pair("Accept" , "application/vnd.github+json"), + Pair("Authorization", "token $githubToken"), ), - requestBody = """{"name":"sync data for Cloudstream", "description": "Private repo for cloudstream Account", "private": true}""".toRequestBody( - RequestBodyTypes.JSON.toMediaTypeOrNull() - ) - ) - - if (!response.isSuccessful) { - return false + requestBody = """{"description":"Cloudstream private backup gist","public":false,"files":{"Cloudstream_Backup_data.txt":{"content":"initialization"}}}""".toRequestBody( + RequestBodyTypes.JSON.toMediaTypeOrNull())) + if (!gitresponse.isSuccessful) {return false} + tryParseJson(gitresponse.text).let { + setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity( + token = githubToken, + repoUrl = it?.gitUrl?: run { + return false + }, + userName = it.owner.userName, + userAvatar = it.owner.userAvatar + )) } - - val repoUrl = tryParseJson(response.text).let { - setKey( - accountId, GITHUB_USER_KEY, GithubOAuthEntity( - token = githubToken, - repoUrl = it?.repoUrl ?: run { - return false - }) - ) - it.repoUrl - } - commitFile(repoUrl, githubToken) return true } else{ repo?.first().let { setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity( token = githubToken, - repoUrl = it?.repoName?: run { + repoUrl = it?.gitUrl?: run { return false - })) + }, + userName = it.owner.userName, + userAvatar = it.owner.userAvatar + )) return true } } - } + } override suspend fun login(data: InAppAuthAPI.LoginData): Boolean { switchToNewAccount() val githubToken = data.password ?: throw IllegalArgumentException ("Requires Password") @@ -155,7 +159,7 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ override fun getLatestLoginData(): InAppAuthAPI.LoginData? { val current = getAuthKey() ?: return null - return InAppAuthAPI.LoginData(username = current.repoUrl, password = current.token) + return InAppAuthAPI.LoginData(email = current.repoUrl, password = current.token, username = current.userName) } override suspend fun initialize() { currentSession = getAuthKey() @@ -172,8 +176,8 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ override fun loginInfo(): AuthAPI.LoginInfo? { return getAuthKey()?.let { user -> AuthAPI.LoginInfo( - profilePicture = null, - name = user.repoUrl, + profilePicture = user.userAvatar, + name = user.userName, accountIndex = accountIndex, ) } diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt index 01a807bf..63ff071a 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt @@ -301,10 +301,10 @@ object BackupUtils { val backup = this.getBackup() ioSafe { val tmpDir = createTempDir() - val repoUrl = githubApi.getLatestLoginData()?.username ?: throw IllegalArgumentException ("Requires Username") + val gitUrl = githubApi.getLatestLoginData()?.email ?: throw IllegalArgumentException ("Requires Username") val token = githubApi.getLatestLoginData()?.password ?: throw IllegalArgumentException ("Requires Username") val git = Git.cloneRepository() - .setURI("https://github.com/$repoUrl.git") + .setURI(gitUrl) .setDirectory(tmpDir) .setTimeout(30) .setCredentialsProvider( @@ -321,10 +321,10 @@ object BackupUtils { .call() git.remoteAdd() .setName("origin") - .setUri(URIish("https://github.com/$repoUrl.git")) + .setUri(URIish(gitUrl)) .call() git.push() - .setRemote("https://github.com/$repoUrl.git") + .setRemote(gitUrl) .setTimeout(30) .setCredentialsProvider( UsernamePasswordCredentialsProvider(token, "") @@ -340,14 +340,14 @@ object BackupUtils { fun FragmentActivity.restorePromptGithub() = ioSafe { val tmpDir = createTempDir() - val repoUrl = githubApi.getLatestLoginData()?.username ?: throw IllegalArgumentException ("Requires Username") + val gitUrl = githubApi.getLatestLoginData()?.email ?: throw IllegalArgumentException ("Requires Username") val token = githubApi.getLatestLoginData()?.password ?: throw IllegalArgumentException ("Requires Username") Git.cloneRepository() - .setURI("https://github.com/$repoUrl.git") + .setURI(gitUrl) .setDirectory(tmpDir) .setTimeout(30) .setCredentialsProvider( - UsernamePasswordCredentialsProvider("$token", "") + UsernamePasswordCredentialsProvider(token, "") ) .call() val jsondata = tmpDir.listFiles()?.first { it.name != ".git" }?.readText().toString() From 801d608beba30dc2ba3937deeb0f300a71e510dd Mon Sep 17 00:00:00 2001 From: Antony Date: Thu, 25 Aug 2022 14:04:15 +0200 Subject: [PATCH 14/20] switch on gist.github --- .../syncproviders/providers/GithubApi.kt | 18 ++++++++----- .../cloudstream3/utils/BackupUtils.kt | 26 +++++-------------- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt index 72701cdd..dad84266 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt @@ -35,7 +35,8 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ var repoUrl: String, var token: String, var userName: String, - var userAvatar: String + var userAvatar: String, + var gistUrl: String ) companion object { const val GITHUB_USER_KEY: String = "github_user" // user data like profile @@ -79,17 +80,18 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ tmpDir.deleteRecursively() } - private data class gistsElements ( + data class gistsElements ( @JsonProperty("git_pull_url") val gitUrl: String, + @JsonProperty("url") val gistUrl:String, @JsonProperty("files") val files: Map, @JsonProperty("owner") val owner: OwnerData ) - private data class OwnerData( + data class OwnerData( @JsonProperty("login") val userName: String, @JsonProperty("avatar_url") val userAvatar : String ) data class File ( - @JsonProperty("filename") val filename: String + @JsonProperty("raw_url") val rawUrl: String ) private suspend fun initLogin(githubToken: String): Boolean{ @@ -122,7 +124,8 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ return false }, userName = it.owner.userName, - userAvatar = it.owner.userAvatar + userAvatar = it.owner.userAvatar, + gistUrl = it.gistUrl )) } return true @@ -135,7 +138,8 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ return false }, userName = it.owner.userName, - userAvatar = it.owner.userAvatar + userAvatar = it.owner.userAvatar, + gistUrl = it.gistUrl )) return true } @@ -159,7 +163,7 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ override fun getLatestLoginData(): InAppAuthAPI.LoginData? { val current = getAuthKey() ?: return null - return InAppAuthAPI.LoginData(email = current.repoUrl, password = current.token, username = current.userName) + return InAppAuthAPI.LoginData(email = current.repoUrl, password = current.token, username = current.userName, server = current.gistUrl) } override suspend fun initialize() { currentSession = getAuthKey() diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt index 63ff071a..9ef6bde1 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt @@ -13,6 +13,7 @@ import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.module.kotlin.readValue import com.lagradost.cloudstream3.CommonActivity.showToast import com.lagradost.cloudstream3.R +import com.lagradost.cloudstream3.app import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.plugins.PLUGINS_KEY import com.lagradost.cloudstream3.plugins.PLUGINS_KEY_LOCAL @@ -30,12 +31,9 @@ import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_S import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_TOKEN_KEY import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_UNIXTIME_KEY import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_USER_KEY -import com.lagradost.cloudstream3.syncproviders.providers.OpenSubtitlesApi import com.lagradost.cloudstream3.syncproviders.providers.OpenSubtitlesApi.Companion.OPEN_SUBTITLES_USER_KEY import com.lagradost.cloudstream3.utils.AppUtils.parseJson import com.lagradost.cloudstream3.utils.AppUtils.toJson -import com.lagradost.cloudstream3.utils.BackupUtils.restore -import com.lagradost.cloudstream3.utils.BackupUtils.restorePromptGithub import com.lagradost.cloudstream3.utils.Coroutines.ioSafe import com.lagradost.cloudstream3.utils.DataStore.getDefaultSharedPrefs @@ -54,7 +52,6 @@ import java.io.PrintWriter import java.lang.System.currentTimeMillis import java.text.SimpleDateFormat import java.util.* - object BackupUtils { /** @@ -297,7 +294,7 @@ object BackupUtils { } - fun FragmentActivity.BackupGithub(){ + fun FragmentActivity.backupGithub(){ val backup = this.getBackup() ioSafe { val tmpDir = createTempDir() @@ -339,20 +336,10 @@ object BackupUtils { fun FragmentActivity.restorePromptGithub() = ioSafe { - val tmpDir = createTempDir() - val gitUrl = githubApi.getLatestLoginData()?.email ?: throw IllegalArgumentException ("Requires Username") - val token = githubApi.getLatestLoginData()?.password ?: throw IllegalArgumentException ("Requires Username") - Git.cloneRepository() - .setURI(gitUrl) - .setDirectory(tmpDir) - .setTimeout(30) - .setCredentialsProvider( - UsernamePasswordCredentialsProvider(token, "") - ) - .call() - val jsondata = tmpDir.listFiles()?.first { it.name != ".git" }?.readText().toString() - val data = parseJson(jsondata?: "") - tmpDir.deleteRecursively() + val gitUrl = githubApi.getLatestLoginData()?.server ?: throw IllegalAccessException() + val jsondata = app.get(gitUrl).text + val dataurl = parseJson(jsondata ?: "").files.values.first().rawUrl + val data = parseJson(app.get(dataurl).text) this@restorePromptGithub.restore( data, restoreSettings = true, @@ -360,3 +347,4 @@ object BackupUtils { ) } } + From 8e5198753f367a027e8b6b239614edb18a89bbc1 Mon Sep 17 00:00:00 2001 From: Antony Date: Thu, 25 Aug 2022 14:15:26 +0200 Subject: [PATCH 15/20] switch on gist.github --- .../syncproviders/providers/GithubApi.kt | 35 +------------------ .../cloudstream3/utils/BackupUtils.kt | 8 ++--- 2 files changed, 5 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt index dad84266..5acd8563 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt @@ -47,39 +47,6 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ } - - private fun commitFile(repoUrl: String, githubToken: String){ - val tmpDir = createTempDir() - val git = Git.cloneRepository() - .setURI("https://github.com/$repoUrl.git") - .setDirectory(tmpDir) - .setTimeout(30) - .setCredentialsProvider( - UsernamePasswordCredentialsProvider(githubToken, "") - ) - .call() - createTempFile("backup", "txt", tmpDir) - git.add() - .addFilepattern(".") - .call() - git.commit() - .setAll(true) - .setMessage("Update backup") - .call() - git.remoteAdd() - .setName("origin") - .setUri(URIish("https://github.com/$repoUrl.git")) - .call() - git.push() - .setRemote("https://github.com/$repoUrl.git") - .setTimeout(30) - .setCredentialsProvider( - UsernamePasswordCredentialsProvider(githubToken, "") - ) - .call(); - tmpDir.deleteRecursively() - } - data class gistsElements ( @JsonProperty("git_pull_url") val gitUrl: String, @JsonProperty("url") val gistUrl:String, @@ -91,7 +58,7 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ @JsonProperty("avatar_url") val userAvatar : String ) data class File ( - @JsonProperty("raw_url") val rawUrl: String + @JsonProperty("content") val dataRaw: String ) private suspend fun initLogin(githubToken: String): Boolean{ diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt index 9ef6bde1..1e4686ad 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt @@ -336,10 +336,10 @@ object BackupUtils { fun FragmentActivity.restorePromptGithub() = ioSafe { - val gitUrl = githubApi.getLatestLoginData()?.server ?: throw IllegalAccessException() - val jsondata = app.get(gitUrl).text - val dataurl = parseJson(jsondata ?: "").files.values.first().rawUrl - val data = parseJson(app.get(dataurl).text) + val gistUrl = githubApi.getLatestLoginData()?.server ?: throw IllegalAccessException() + val jsondata = app.get(gistUrl).text + val dataraw = parseJson(jsondata ?: "").files.values.first().dataRaw + val data = parseJson(dataraw) this@restorePromptGithub.restore( data, restoreSettings = true, From 0e029cadef689f20e16e3a6887fad0506e7a50c4 Mon Sep 17 00:00:00 2001 From: Antony Date: Thu, 25 Aug 2022 14:17:29 +0200 Subject: [PATCH 16/20] switch on gist.github --- .../com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt index 9700961b..6b19042a 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt @@ -15,7 +15,6 @@ import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.getPref import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setPaddingBottom import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setUpToolbar -import com.lagradost.cloudstream3.utils.BackupUtils.BackupGithub import com.lagradost.cloudstream3.utils.BackupUtils.backup import com.lagradost.cloudstream3.utils.BackupUtils.restorePrompt import com.lagradost.cloudstream3.utils.Coroutines.ioSafe From ff05ad897df3c1563e1df38add9daa3d0f8a329b Mon Sep 17 00:00:00 2001 From: Antony Date: Thu, 25 Aug 2022 14:30:03 +0200 Subject: [PATCH 17/20] switch on gist.github --- .../lagradost/cloudstream3/syncproviders/providers/GithubApi.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt index 5acd8563..bcb892a3 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt @@ -58,7 +58,7 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ @JsonProperty("avatar_url") val userAvatar : String ) data class File ( - @JsonProperty("content") val dataRaw: String + @JsonProperty("content") val dataRaw: String? ) private suspend fun initLogin(githubToken: String): Boolean{ From be3f00d12665a87b50cb410129678761d5dd0430 Mon Sep 17 00:00:00 2001 From: Antony Date: Thu, 25 Aug 2022 14:32:03 +0200 Subject: [PATCH 18/20] switch on gist.github --- .../main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt index 1e4686ad..50466403 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt @@ -338,7 +338,7 @@ object BackupUtils { ioSafe { val gistUrl = githubApi.getLatestLoginData()?.server ?: throw IllegalAccessException() val jsondata = app.get(gistUrl).text - val dataraw = parseJson(jsondata ?: "").files.values.first().dataRaw + val dataraw = parseJson(jsondata ?: "").files.values.first().dataRaw?: throw IllegalAccessException() val data = parseJson(dataraw) this@restorePromptGithub.restore( data, From 79833fafcbec8df5a951dc72311c7cb7569259f0 Mon Sep 17 00:00:00 2001 From: Antony Date: Thu, 25 Aug 2022 15:02:49 +0200 Subject: [PATCH 19/20] switch on gist.github --- .../java/com/lagradost/cloudstream3/utils/BackupUtils.kt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt index 50466403..a7770fef 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt @@ -329,9 +329,11 @@ object BackupUtils { .call(); tmpDir.deleteRecursively() } - - - + showToast( + this, + R.string.backup_success, + Toast.LENGTH_LONG + ) } fun FragmentActivity.restorePromptGithub() = From e15aba2d63d1bdd6b43d6233c766798b8ef4b663 Mon Sep 17 00:00:00 2001 From: Antony Date: Thu, 25 Aug 2022 15:45:08 +0200 Subject: [PATCH 20/20] createAccountUrl fix --- .../lagradost/cloudstream3/syncproviders/providers/GithubApi.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt index bcb892a3..53669aca 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt @@ -29,7 +29,7 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ override val name = "Github" override val icon = R.drawable.ic_github_logo override val requiresPassword = true - override val createAccountUrl = "https://github.com/settings/tokens/new" + override val createAccountUrl = "https://github.com/settings/tokens/new?description=Cloudstream+Backup&scopes=gist" data class GithubOAuthEntity( var repoUrl: String,