This commit is contained in:
Antony 2022-08-24 02:38:49 +02:00
parent d5f6f38ca8
commit 6ab90504f4
2 changed files with 60 additions and 53 deletions

View file

@ -3,6 +3,7 @@ package com.lagradost.cloudstream3.syncproviders.providers
import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.annotation.JsonProperty
import com.lagradost.cloudstream3.* import com.lagradost.cloudstream3.*
import com.lagradost.cloudstream3.AcraApplication.Companion.getKey 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.AcraApplication.Companion.setKey
import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.syncproviders.AuthAPI 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{ private suspend fun initLogin(githubToken: String): Boolean{
val response = app.post("https://api.github.com/user/repos", val response = app.post("https://api.github.com/user/repos",
headers= mapOf( headers= mapOf(
Pair("Accept" , "application/vnd.github+json"), "Accept" to "application/vnd.github+json",
Pair("Authorization", "token $githubToken"), "Authorization" to "token $githubToken"
), ),
requestBody = """{"name":"sync data for Cloudstream", "description": "Private repo for cloudstream Account", "private": true}""".toRequestBody( requestBody = """{"name":"sync data for Cloudstream", "description": "Private repo for cloudstream Account", "private": true}""".toRequestBody(
RequestBodyTypes.JSON.toMediaTypeOrNull())) RequestBodyTypes.JSON.toMediaTypeOrNull()))
if (response.isSuccessful) { if (!response.isSuccessful) {return false}
val repoUrl = tryParseJson<repodata>(response.text).let {
setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity( val repoUrl = tryParseJson<repodata>(response.text).let {
token = githubToken, setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity(
repoUrl = it?.repoUrl?: run { token = githubToken,
return false repoUrl = it?.repoUrl?: run {
})) return false
it.repoUrl }))
} 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 commitFile(repoUrl, githubToken)
return true
} }
override suspend fun login(data: InAppAuthAPI.LoginData): Boolean { 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) return InAppAuthAPI.LoginData(username = current.repoUrl, password = current.token)
} }
override suspend fun initialize() { override suspend fun initialize() {
currentSession = getAuthKey() ?: return // just in case the following fails currentSession = getAuthKey()
setKey(currentSession!!.repoUrl, currentSession!!.token) val repoUrl = currentSession?.repoUrl ?: return
val token = currentSession?.token ?: return
setKey(repoUrl, token)
} }
override fun logOut() { override fun logOut() {
AcraApplication.removeKey(accountId, GITHUB_USER_KEY) removeKey(accountId, GITHUB_USER_KEY)
removeAccountKeys() removeAccountKeys()
currentSession = getAuthKey() currentSession = getAuthKey()
} }
override fun loginInfo(): AuthAPI.LoginInfo? { override fun loginInfo(): AuthAPI.LoginInfo? {
getAuthKey()?.let { user -> return getAuthKey()?.let { user ->
return AuthAPI.LoginInfo( AuthAPI.LoginInfo(
profilePicture = null, profilePicture = null,
name = user.repoUrl, name = user.repoUrl,
accountIndex = accountIndex, accountIndex = accountIndex,
) )
} }
return null
} }
} }

View file

@ -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_UNIXTIME_KEY
import com.lagradost.cloudstream3.syncproviders.providers.AniListApi.Companion.ANILIST_USER_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
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_CACHED_LIST
import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_REFRESH_TOKEN_KEY import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_REFRESH_TOKEN_KEY
import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_SHOULD_UPDATE_LIST 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 // The plugins themselves are not backed up
PLUGINS_KEY, PLUGINS_KEY,
PLUGINS_KEY_LOCAL, PLUGINS_KEY_LOCAL,
GITHUB_USER_KEY,
OPEN_SUBTITLES_USER_KEY, OPEN_SUBTITLES_USER_KEY,
"nginx_user", // Nginx user key "nginx_user", // Nginx user key
@ -335,7 +336,7 @@ object BackupUtils {
} }
fun FragmentActivity.restorePromptGithub(){ fun FragmentActivity.restorePromptGithub() =
ioSafe { ioSafe {
val tmpDir = createTempDir() val tmpDir = createTempDir()
val repoUrl = githubApi.getLatestLoginData()?.username ?: throw IllegalArgumentException ("Requires Username") val repoUrl = githubApi.getLatestLoginData()?.username ?: throw IllegalArgumentException ("Requires Username")
@ -348,8 +349,7 @@ object BackupUtils {
UsernamePasswordCredentialsProvider("$token", "") UsernamePasswordCredentialsProvider("$token", "")
) )
.call() .call()
val jsondata = tmpDir.listFiles()?.first { it.name != ".git" }?.readLines() val jsondata = tmpDir.listFiles()?.first { it.name != ".git" }?.readText().toString()
?.joinToString()
val data = parseJson<BackupFile>(jsondata?: "") val data = parseJson<BackupFile>(jsondata?: "")
this@restorePromptGithub.restore( this@restorePromptGithub.restore(
data, data,
@ -357,5 +357,4 @@ object BackupUtils {
restoreDataStore = true restoreDataStore = true
) )
} }
}
} }