Create new repo

This commit is contained in:
Antony 2022-08-23 18:54:22 +02:00
parent 465daad705
commit e12efc13ff
3 changed files with 85 additions and 17 deletions

View file

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

View file

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

View file

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