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.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<repodata>(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<repodata>(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
}
}

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_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<BackupFile>(jsondata?: "")
this@restorePromptGithub.restore(
data,
@ -357,5 +357,4 @@ object BackupUtils {
restoreDataStore = true
)
}
}
}
}