From 2de76ddd341b94eac594af95a332b18dd6a33d75 Mon Sep 17 00:00:00 2001 From: Antony Date: Sun, 21 Aug 2022 00:21:30 +0200 Subject: [PATCH] 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