mirror of
https://github.com/recloudstream/cloudstream.git
synced 2024-08-15 01:53:11 +00:00
switch on gist.github
This commit is contained in:
parent
aa3b94d186
commit
409f8f743d
2 changed files with 57 additions and 53 deletions
|
@ -33,7 +33,9 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){
|
||||||
|
|
||||||
data class GithubOAuthEntity(
|
data class GithubOAuthEntity(
|
||||||
var repoUrl: String,
|
var repoUrl: String,
|
||||||
var token: String
|
var token: String,
|
||||||
|
var userName: String,
|
||||||
|
var userAvatar: String
|
||||||
)
|
)
|
||||||
companion object {
|
companion object {
|
||||||
const val GITHUB_USER_KEY: String = "github_user" // user data like profile
|
const val GITHUB_USER_KEY: String = "github_user" // user data like profile
|
||||||
|
@ -42,9 +44,7 @@ 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 fun commitFile(repoUrl: String, githubToken: String){
|
private fun commitFile(repoUrl: String, githubToken: String){
|
||||||
|
@ -78,66 +78,70 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){
|
||||||
.call();
|
.call();
|
||||||
tmpDir.deleteRecursively()
|
tmpDir.deleteRecursively()
|
||||||
}
|
}
|
||||||
private class reposElements (
|
|
||||||
@JsonProperty("full_name") var repoName: String,
|
|
||||||
@JsonProperty("name") var shortRepoName: String
|
|
||||||
|
|
||||||
|
private data class gistsElements (
|
||||||
|
@JsonProperty("git_pull_url") val gitUrl: String,
|
||||||
|
@JsonProperty("files") val files: Map<String, File>,
|
||||||
|
@JsonProperty("owner") val owner: OwnerData
|
||||||
|
)
|
||||||
|
private data class OwnerData(
|
||||||
|
@JsonProperty("login") val userName: String,
|
||||||
|
@JsonProperty("avatar_url") val userAvatar : String
|
||||||
|
)
|
||||||
|
data class File (
|
||||||
|
@JsonProperty("filename") val filename: String
|
||||||
)
|
)
|
||||||
|
|
||||||
private suspend fun initLogin(githubToken: String): Boolean{
|
private suspend fun initLogin(githubToken: String): Boolean{
|
||||||
val repoResponse = app.get("https://api.github.com/user/repos",
|
val response = app.get("https://api.github.com/gists",
|
||||||
headers= mapOf(
|
headers= mapOf(
|
||||||
"Accept" to "application/vnd.github+json",
|
Pair("Accept" , "application/vnd.github+json"),
|
||||||
"Authorization" to "token $githubToken"
|
Pair("Authorization", "token $githubToken"),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
if (!repoResponse.isSuccessful) {
|
|
||||||
return false
|
if (!response.isSuccessful) { return false }
|
||||||
}
|
|
||||||
val repo = tryParseJson<List<reposElements>>(repoResponse.text)?.filter {
|
val repo = tryParseJson<List<gistsElements>>(response.text)?.filter {
|
||||||
it.shortRepoName == "sync-data-for-Cloudstream"
|
it.files.keys.first() == "Cloudstream_Backup_data.txt"
|
||||||
}
|
}
|
||||||
|
|
||||||
if (repo?.isEmpty() == true){
|
if (repo?.isEmpty() == true){
|
||||||
val response = app.post(
|
val gitresponse = app.post("https://api.github.com/gists",
|
||||||
"https://api.github.com/user/repos",
|
|
||||||
headers= mapOf(
|
headers= mapOf(
|
||||||
"Accept" to "application/vnd.github+json",
|
Pair("Accept" , "application/vnd.github+json"),
|
||||||
"Authorization" to "token $githubToken"
|
Pair("Authorization", "token $githubToken"),
|
||||||
),
|
),
|
||||||
requestBody = """{"name":"sync data for Cloudstream", "description": "Private repo for cloudstream Account", "private": true}""".toRequestBody(
|
requestBody = """{"description":"Cloudstream private backup gist","public":false,"files":{"Cloudstream_Backup_data.txt":{"content":"initialization"}}}""".toRequestBody(
|
||||||
RequestBodyTypes.JSON.toMediaTypeOrNull()
|
RequestBodyTypes.JSON.toMediaTypeOrNull()))
|
||||||
)
|
if (!gitresponse.isSuccessful) {return false}
|
||||||
)
|
tryParseJson<gistsElements>(gitresponse.text).let {
|
||||||
|
setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity(
|
||||||
if (!response.isSuccessful) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
val repoUrl = tryParseJson<repodata>(response.text).let {
|
|
||||||
setKey(
|
|
||||||
accountId, GITHUB_USER_KEY, GithubOAuthEntity(
|
|
||||||
token = githubToken,
|
token = githubToken,
|
||||||
repoUrl = it?.repoUrl ?: run {
|
repoUrl = it?.gitUrl?: run {
|
||||||
return false
|
return false
|
||||||
})
|
},
|
||||||
)
|
userName = it.owner.userName,
|
||||||
it.repoUrl
|
userAvatar = it.owner.userAvatar
|
||||||
|
))
|
||||||
}
|
}
|
||||||
commitFile(repoUrl, githubToken)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
repo?.first().let {
|
repo?.first().let {
|
||||||
setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity(
|
setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity(
|
||||||
token = githubToken,
|
token = githubToken,
|
||||||
repoUrl = it?.repoName?: run {
|
repoUrl = it?.gitUrl?: run {
|
||||||
return false
|
return false
|
||||||
}))
|
},
|
||||||
|
userName = it.owner.userName,
|
||||||
|
userAvatar = it.owner.userAvatar
|
||||||
|
))
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
override suspend fun login(data: InAppAuthAPI.LoginData): Boolean {
|
override suspend fun login(data: InAppAuthAPI.LoginData): Boolean {
|
||||||
switchToNewAccount()
|
switchToNewAccount()
|
||||||
val githubToken = data.password ?: throw IllegalArgumentException ("Requires Password")
|
val githubToken = data.password ?: throw IllegalArgumentException ("Requires Password")
|
||||||
|
@ -155,7 +159,7 @@ 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.repoUrl, password = current.token)
|
return InAppAuthAPI.LoginData(email = current.repoUrl, password = current.token, username = current.userName)
|
||||||
}
|
}
|
||||||
override suspend fun initialize() {
|
override suspend fun initialize() {
|
||||||
currentSession = getAuthKey()
|
currentSession = getAuthKey()
|
||||||
|
@ -172,8 +176,8 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){
|
||||||
override fun loginInfo(): AuthAPI.LoginInfo? {
|
override fun loginInfo(): AuthAPI.LoginInfo? {
|
||||||
return getAuthKey()?.let { user ->
|
return getAuthKey()?.let { user ->
|
||||||
AuthAPI.LoginInfo(
|
AuthAPI.LoginInfo(
|
||||||
profilePicture = null,
|
profilePicture = user.userAvatar,
|
||||||
name = user.repoUrl,
|
name = user.userName,
|
||||||
accountIndex = accountIndex,
|
accountIndex = accountIndex,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -301,10 +301,10 @@ object BackupUtils {
|
||||||
val backup = this.getBackup()
|
val backup = this.getBackup()
|
||||||
ioSafe {
|
ioSafe {
|
||||||
val tmpDir = createTempDir()
|
val tmpDir = createTempDir()
|
||||||
val repoUrl = githubApi.getLatestLoginData()?.username ?: throw IllegalArgumentException ("Requires Username")
|
val gitUrl = githubApi.getLatestLoginData()?.email ?: 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("https://github.com/$repoUrl.git")
|
.setURI(gitUrl)
|
||||||
.setDirectory(tmpDir)
|
.setDirectory(tmpDir)
|
||||||
.setTimeout(30)
|
.setTimeout(30)
|
||||||
.setCredentialsProvider(
|
.setCredentialsProvider(
|
||||||
|
@ -321,10 +321,10 @@ object BackupUtils {
|
||||||
.call()
|
.call()
|
||||||
git.remoteAdd()
|
git.remoteAdd()
|
||||||
.setName("origin")
|
.setName("origin")
|
||||||
.setUri(URIish("https://github.com/$repoUrl.git"))
|
.setUri(URIish(gitUrl))
|
||||||
.call()
|
.call()
|
||||||
git.push()
|
git.push()
|
||||||
.setRemote("https://github.com/$repoUrl.git")
|
.setRemote(gitUrl)
|
||||||
.setTimeout(30)
|
.setTimeout(30)
|
||||||
.setCredentialsProvider(
|
.setCredentialsProvider(
|
||||||
UsernamePasswordCredentialsProvider(token, "")
|
UsernamePasswordCredentialsProvider(token, "")
|
||||||
|
@ -340,14 +340,14 @@ 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 gitUrl = githubApi.getLatestLoginData()?.email ?: 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("https://github.com/$repoUrl.git")
|
.setURI(gitUrl)
|
||||||
.setDirectory(tmpDir)
|
.setDirectory(tmpDir)
|
||||||
.setTimeout(30)
|
.setTimeout(30)
|
||||||
.setCredentialsProvider(
|
.setCredentialsProvider(
|
||||||
UsernamePasswordCredentialsProvider("$token", "")
|
UsernamePasswordCredentialsProvider(token, "")
|
||||||
)
|
)
|
||||||
.call()
|
.call()
|
||||||
val jsondata = tmpDir.listFiles()?.first { it.name != ".git" }?.readText().toString()
|
val jsondata = tmpDir.listFiles()?.first { it.name != ".git" }?.readText().toString()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue