BackupGithub and restorePromptGithub in BackupUtils.kt

added GithubApi in SettingsAccount.kt
This commit is contained in:
Antony 2022-08-21 00:21:30 +02:00
parent 10f4d33c59
commit 2de76ddd34
7 changed files with 141 additions and 20 deletions

View file

@ -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) {

View file

@ -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

View file

@ -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)
}
}

View file

@ -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) {

View file

@ -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<BackupFile>(jsondata?: "")
this@restorePromptGithub.restore(
data,
restoreSettings = true,
restoreDataStore = true
)
}
}
}

View file

@ -459,6 +459,7 @@
<string name="anilist_key" translatable="false">anilist_key</string>
<string name="mal_key" translatable="false">mal_key</string>
<string name="opensubtitles_key" translatable="false">opensubtitles_key</string>
<string name="github_key" translatable="false">github_key</string>
<string name="nginx_key" translatable="false">nginx_key</string>
<string name="example_password">password123</string>
<string name="example_username">MyCoolUsername</string>

View file

@ -11,17 +11,20 @@
<Preference
android:key="@string/opensubtitles_key"
android:icon="@drawable/open_subtitles_icon" />
<!-- <Preference-->
<!-- android:key="@string/nginx_key"-->
<!-- android:icon="@drawable/nginx" />-->
<Preference
android:key="@string/github_key"
android:icon="@drawable/ic_github_logo" />
<!-- <Preference-->
<!-- android:key="@string/nginx_key"-->
<!-- android:icon="@drawable/nginx" />-->
<!-- <Preference-->
<!-- android:title="@string/nginx_info_title"-->
<!-- android:icon="@drawable/nginx_question"-->
<!-- android:summary="@string/nginx_info_summary">-->
<!-- <intent-->
<!-- android:action="android.intent.action.VIEW"-->
<!-- android:data="https://www.sarlays.com/use-nginx-with-cloudstream/" />-->
<!-- </Preference>-->
<!-- <Preference-->
<!-- android:title="@string/nginx_info_title"-->
<!-- android:icon="@drawable/nginx_question"-->
<!-- android:summary="@string/nginx_info_summary">-->
<!-- <intent-->
<!-- android:action="android.intent.action.VIEW"-->
<!-- android:data="https://www.sarlays.com/use-nginx-with-cloudstream/" />-->
<!-- </Preference>-->
</PreferenceScreen>