mirror of
https://github.com/recloudstream/cloudstream.git
synced 2024-08-15 01:53:11 +00:00
Create new repo
This commit is contained in:
parent
465daad705
commit
e12efc13ff
3 changed files with 85 additions and 17 deletions
|
@ -1,5 +1,6 @@
|
||||||
package com.lagradost.cloudstream3.syncproviders.providers
|
package com.lagradost.cloudstream3.syncproviders.providers
|
||||||
|
|
||||||
|
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.setKey
|
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.AuthAPI
|
||||||
import com.lagradost.cloudstream3.syncproviders.InAppAuthAPI
|
import com.lagradost.cloudstream3.syncproviders.InAppAuthAPI
|
||||||
import com.lagradost.cloudstream3.syncproviders.InAppAuthAPIManager
|
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){
|
class GithubApi(index: Int) : InAppAuthAPIManager(index){
|
||||||
|
@ -14,11 +28,10 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){
|
||||||
override val name = "Github"
|
override val name = "Github"
|
||||||
override val icon = R.drawable.ic_github_logo
|
override val icon = R.drawable.ic_github_logo
|
||||||
override val requiresPassword = true
|
override val requiresPassword = true
|
||||||
override val requiresUsername = true
|
|
||||||
override val createAccountUrl = "https://github.com/settings/tokens/new"
|
override val createAccountUrl = "https://github.com/settings/tokens/new"
|
||||||
|
|
||||||
data class GithubOAuthEntity(
|
data class GithubOAuthEntity(
|
||||||
var user: String,
|
var repoUrl: String,
|
||||||
var token: String
|
var token: String
|
||||||
)
|
)
|
||||||
companion object {
|
companion object {
|
||||||
|
@ -28,14 +41,70 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){
|
||||||
private fun getAuthKey(): GithubOAuthEntity? {
|
private fun getAuthKey(): GithubOAuthEntity? {
|
||||||
return getKey(accountId, GITHUB_USER_KEY)
|
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 {
|
override suspend fun login(data: InAppAuthAPI.LoginData): Boolean {
|
||||||
switchToNewAccount()
|
switchToNewAccount()
|
||||||
val repoUrl = data.username ?: throw IllegalArgumentException ("Requires Username")
|
val githubToken = data.password ?: throw IllegalArgumentException ("Requires Password")
|
||||||
val password = data.password ?: throw IllegalArgumentException ("Requires Password")
|
|
||||||
try {
|
try {
|
||||||
setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity(repoUrl, password))
|
if (initLogin(githubToken)) {
|
||||||
registerAccount()
|
registerAccount()
|
||||||
return true
|
return true
|
||||||
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
logError(e)
|
logError(e)
|
||||||
switchToOldAccount()
|
switchToOldAccount()
|
||||||
|
@ -46,11 +115,11 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){
|
||||||
|
|
||||||
override fun getLatestLoginData(): InAppAuthAPI.LoginData? {
|
override fun getLatestLoginData(): InAppAuthAPI.LoginData? {
|
||||||
val current = getAuthKey() ?: return null
|
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() {
|
override suspend fun initialize() {
|
||||||
currentSession = getAuthKey() ?: return // just in case the following fails
|
currentSession = getAuthKey() ?: return // just in case the following fails
|
||||||
setKey(currentSession!!.user, currentSession!!.token)
|
setKey(currentSession!!.repoUrl, currentSession!!.token)
|
||||||
}
|
}
|
||||||
override fun logOut() {
|
override fun logOut() {
|
||||||
AcraApplication.removeKey(accountId, GITHUB_USER_KEY)
|
AcraApplication.removeKey(accountId, GITHUB_USER_KEY)
|
||||||
|
@ -62,7 +131,7 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){
|
||||||
getAuthKey()?.let { user ->
|
getAuthKey()?.let { user ->
|
||||||
return AuthAPI.LoginInfo(
|
return AuthAPI.LoginInfo(
|
||||||
profilePicture = null,
|
profilePicture = null,
|
||||||
name = user.user,
|
name = user.repoUrl,
|
||||||
accountIndex = accountIndex,
|
accountIndex = accountIndex,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ class SettingsUpdates : PreferenceFragmentCompat() {
|
||||||
//val settingsManager = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
//val settingsManager = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||||
|
|
||||||
getPref(R.string.backup_key)?.setOnPreferenceClickListener {
|
getPref(R.string.backup_key)?.setOnPreferenceClickListener {
|
||||||
activity?.BackupGithub()
|
activity?.backup()
|
||||||
return@setOnPreferenceClickListener true
|
return@setOnPreferenceClickListener true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -303,28 +303,27 @@ object BackupUtils {
|
||||||
val repoUrl = githubApi.getLatestLoginData()?.username ?: throw IllegalArgumentException ("Requires Username")
|
val repoUrl = githubApi.getLatestLoginData()?.username ?: throw IllegalArgumentException ("Requires Username")
|
||||||
val token = githubApi.getLatestLoginData()?.password ?: throw IllegalArgumentException ("Requires Username")
|
val token = githubApi.getLatestLoginData()?.password ?: throw IllegalArgumentException ("Requires Username")
|
||||||
val git = Git.cloneRepository()
|
val git = Git.cloneRepository()
|
||||||
.setURI("$repoUrl.git")
|
.setURI("https://github.com/$repoUrl.git")
|
||||||
.setDirectory(tmpDir)
|
.setDirectory(tmpDir)
|
||||||
.setTimeout(30)
|
.setTimeout(30)
|
||||||
.setCredentialsProvider(
|
.setCredentialsProvider(
|
||||||
UsernamePasswordCredentialsProvider(token, "")
|
UsernamePasswordCredentialsProvider(token, "")
|
||||||
)
|
)
|
||||||
.call()
|
.call()
|
||||||
|
|
||||||
tmpDir.listFiles()?.first { it.name != ".git" }?.writeText(backup.toJson())
|
tmpDir.listFiles()?.first { it.name != ".git" }?.writeText(backup.toJson())
|
||||||
git.add()
|
git.add()
|
||||||
.addFilepattern(".")
|
.addFilepattern(".")
|
||||||
.call()
|
.call()
|
||||||
git.commit()
|
git.commit()
|
||||||
.setAll(true)
|
.setAll(true)
|
||||||
.setMessage("Update results")
|
.setMessage("Update backup")
|
||||||
.call()
|
.call()
|
||||||
git.remoteAdd()
|
git.remoteAdd()
|
||||||
.setName("origin")
|
.setName("origin")
|
||||||
.setUri(URIish("$repoUrl.git"))
|
.setUri(URIish("https://github.com/$repoUrl.git"))
|
||||||
.call()
|
.call()
|
||||||
git.push()
|
git.push()
|
||||||
.setRemote("$repoUrl.git")
|
.setRemote("https://github.com/$repoUrl.git")
|
||||||
.setTimeout(30)
|
.setTimeout(30)
|
||||||
.setCredentialsProvider(
|
.setCredentialsProvider(
|
||||||
UsernamePasswordCredentialsProvider(token, "")
|
UsernamePasswordCredentialsProvider(token, "")
|
||||||
|
@ -342,7 +341,7 @@ object BackupUtils {
|
||||||
val repoUrl = githubApi.getLatestLoginData()?.username ?: throw IllegalArgumentException ("Requires Username")
|
val repoUrl = githubApi.getLatestLoginData()?.username ?: throw IllegalArgumentException ("Requires Username")
|
||||||
val token = githubApi.getLatestLoginData()?.password ?: throw IllegalArgumentException ("Requires Username")
|
val token = githubApi.getLatestLoginData()?.password ?: throw IllegalArgumentException ("Requires Username")
|
||||||
Git.cloneRepository()
|
Git.cloneRepository()
|
||||||
.setURI("$repoUrl.git")
|
.setURI("https://github.com/$repoUrl.git")
|
||||||
.setDirectory(tmpDir)
|
.setDirectory(tmpDir)
|
||||||
.setTimeout(30)
|
.setTimeout(30)
|
||||||
.setCredentialsProvider(
|
.setCredentialsProvider(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue