Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub account #128

Closed
wants to merge 52 commits into from
Closed
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
2de76dd
BackupGithub and restorePromptGithub in BackupUtils.kt
antonydp Aug 20, 2022
798ff9a
remove AbstractSubApi from GithubApi.kt
antonydp Aug 20, 2022
9c6465c
GithubApi.kt fixes
antonydp Aug 21, 2022
af35ddf
Merge remote-tracking branch 'upstream/master'
antonydp Aug 21, 2022
24009c5
IllegalArgumentException
antonydp Aug 21, 2022
2e5e502
BackupGithub fix
antonydp Aug 22, 2022
465daad
Merge remote-tracking branch 'upstream/master'
antonydp Aug 23, 2022
e12efc1
Create new repo
antonydp Aug 23, 2022
d5f6f38
Merge branch 'recloudstream:master' into master
antonydp Aug 23, 2022
6ab9050
fixes
antonydp Aug 24, 2022
df54745
fixes
antonydp Aug 24, 2022
d9824b5
fixes
antonydp Aug 24, 2022
98c85d7
added check to previously made backups
antonydp Aug 24, 2022
46bab40
Merge branch 'recloudstream:master' into master
antonydp Aug 24, 2022
c99c620
Update GithubApi.kt
antonydp Aug 24, 2022
bdb6d78
added tmpDir.deleteRecursively()
antonydp Aug 24, 2022
aa3b94d
Merge remote-tracking branch 'origin/master'
antonydp Aug 24, 2022
409f8f7
switch on gist.github
antonydp Aug 25, 2022
801d608
switch on gist.github
antonydp Aug 25, 2022
8e51987
switch on gist.github
antonydp Aug 25, 2022
0e029ca
switch on gist.github
antonydp Aug 25, 2022
ff05ad8
switch on gist.github
antonydp Aug 25, 2022
be3f00d
switch on gist.github
antonydp Aug 25, 2022
79833fa
switch on gist.github
antonydp Aug 25, 2022
e15aba2
createAccountUrl fix
antonydp Aug 25, 2022
eaa1171
Merge branch 'master' into githubAccount
AntonyMGdp Sep 30, 2022
100ff87
createAccountUrl fix
AntonyMGdp Sep 30, 2022
7ef9b18
onCreate, onDestroy
AntonyMGdp Oct 1, 2022
fcff6de
onCreate, onDestroy fix
AntonyMGdp Oct 1, 2022
fb99e92
set back activity?.restorePrompt() in SettingsUpdates.kt
AntonyMGdp Oct 1, 2022
5dc3807
Merge branch 'master' into githubAccount
C10udburst Oct 8, 2022
6aaca03
Merge branch 'recloudstream:master' into githubAccount
antonydp Oct 10, 2022
5be6422
use gist api to push backup informations
AntonyMGdp Oct 11, 2022
e4ed0d4
Merge remote-tracking branch 'origin/githubAccount' into githubAccount
AntonyMGdp Oct 11, 2022
22a04ba
removed unnecessary comments
AntonyMGdp Oct 11, 2022
28307ed
placed backupGithub on onPause
AntonyMGdp Oct 11, 2022
69bc132
Merge branch 'recloudstream:master' into githubAccount
antonydp Oct 11, 2022
645d251
Merge branch 'recloudstream:master' into githubAccount
antonydp Oct 19, 2022
9d733ee
update
antonydp Oct 31, 2022
dc073b9
Merge remote-tracking branch 'upstream/master' into githubAccount
antonydp Oct 31, 2022
6012ffb
rename variables and String.isTransferable fix
antonydp Oct 31, 2022
3f4d28b
Merge branch 'recloudstream:master' into githubAccount
antonydp Nov 2, 2022
105b03f
reloadStoredDataEvent and fixes
antonydp Nov 5, 2022
9b835f6
restorePromptGithub on onStart()
antonydp Nov 5, 2022
b686821
Merge branch 'master' into githubAccount
C10udburst Nov 6, 2022
59d957d
Merge branch 'recloudstream:master' into githubAccount
antonydp Nov 7, 2022
2374ba2
accountsLoginLock.withLock{}
antonydp Nov 7, 2022
337f33d
Merge remote-tracking branch 'origin/githubAccount' into githubAccount
antonydp Nov 7, 2022
c8fe808
Merge branch 'master' into githubAccount
C10udburst Nov 13, 2022
fcdb418
Merge remote-tracking branch 'upstream/master' into githubAccount
antonydp Jan 23, 2023
4dd205c
Merge branch 'githubAccount' of https://github.com/antonydp/cloudstre…
antonydp Jan 23, 2023
2363aad
reloadStoredEvent
antonydp Jan 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.lagradost.cloudstream3.APIHolder.apis
import com.lagradost.cloudstream3.APIHolder.getApiDubstatusSettings
import com.lagradost.cloudstream3.APIHolder.initAll
import com.lagradost.cloudstream3.APIHolder.updateHasTrailers
import com.lagradost.cloudstream3.AcraApplication.Companion.context
import com.lagradost.cloudstream3.AcraApplication.Companion.removeKey
import com.lagradost.cloudstream3.AcraApplication.Companion.setKey
import com.lagradost.cloudstream3.CommonActivity.loadThemes
Expand All @@ -52,6 +53,7 @@ import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.OAuth2A
import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.accountManagers
import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.appString
import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.appStringRepo
import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.githubApi
import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.inAppAuths
import com.lagradost.cloudstream3.ui.APIRepository
import com.lagradost.cloudstream3.ui.download.DOWNLOAD_NAVIGATE_TO
Expand All @@ -65,6 +67,8 @@ import com.lagradost.cloudstream3.utils.AppUtils.isCastApiAvailable
import com.lagradost.cloudstream3.utils.AppUtils.loadCache
import com.lagradost.cloudstream3.utils.AppUtils.loadRepository
import com.lagradost.cloudstream3.utils.AppUtils.loadResult
import com.lagradost.cloudstream3.utils.BackupUtils.backupGithub
import com.lagradost.cloudstream3.utils.BackupUtils.restorePromptGithub
import com.lagradost.cloudstream3.utils.BackupUtils.setUpBackup
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
import com.lagradost.cloudstream3.utils.DataStore.getKey
Expand Down Expand Up @@ -383,6 +387,10 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
} catch (e: Exception) {
logError(e)
}
val settingsManager = PreferenceManager.getDefaultSharedPreferences(this)
if (githubApi.getLatestLoginData() != null && settingsManager.getBoolean(getString(R.string.automatic_cloud_backups), true)) {
this@MainActivity.backupGithub()
}
}


Expand Down Expand Up @@ -498,6 +506,15 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
}
}

override fun onStart() {
val settingsManager = PreferenceManager.getDefaultSharedPreferences(this)
super.onStart()
ioSafe {
if (githubApi.getLatestLoginData() != null && settingsManager.getBoolean(getString(R.string.automatic_cloud_backups), true)){
antonydp marked this conversation as resolved.
Show resolved Hide resolved
context?.restorePromptGithub()
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
app.initClient(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ abstract class AccountManager(private val defIndex: Int) : AuthAPI {
val malApi = MALApi(0)
val aniListApi = AniListApi(0)
val openSubtitlesApi = OpenSubtitlesApi(0)
val githubApi = GithubApi(0)
val indexSubtitlesApi = IndexSubtitleApi()

// used to login via app intent
Expand All @@ -22,7 +23,7 @@ abstract class AccountManager(private val defIndex: Int) : AuthAPI {
// this needs init with context and can be accessed in settings
val accountManagers
get() = listOf(
malApi, aniListApi, openSubtitlesApi, //nginxApi
malApi, aniListApi, openSubtitlesApi, githubApi //nginxApi
)

// used for active syncing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package com.lagradost.cloudstream3.syncproviders.providers

import com.fasterxml.jackson.annotation.JsonProperty
import com.lagradost.cloudstream3.*
import com.lagradost.cloudstream3.AcraApplication.Companion.context
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
import com.lagradost.cloudstream3.syncproviders.InAppAuthAPI
import com.lagradost.cloudstream3.syncproviders.InAppAuthAPIManager
import com.lagradost.cloudstream3.utils.AppUtils.toJson
import com.lagradost.cloudstream3.utils.AppUtils.tryParseJson
import com.lagradost.cloudstream3.utils.BackupUtils.getBackup
import com.lagradost.cloudstream3.utils.BackupUtils.restorePromptGithub
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
import com.lagradost.nicehttp.RequestBodyTypes
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody


class GithubApi(index: Int) : InAppAuthAPIManager(index){
override val idPrefix = "Github"
override val name = "Github"
override val icon = R.drawable.ic_github_logo
override val requiresPassword = true
override val createAccountUrl = "https://github.com/settings/tokens/new?description=Cloudstream+Backup&scopes=gist"

data class GithubOAuthEntity(
var gistId: String,
var token: String,
var userName: String,
var userAvatar: String,
)

companion object {
const val GITHUB_USER_KEY: String = "github_user" // user data like profile
var currentSession: GithubOAuthEntity? = null
}

private fun getAuthKey(): GithubOAuthEntity? {
return getKey(accountId, GITHUB_USER_KEY)
}

data class GistsElements (
@JsonProperty("id") val gistId:String,
@JsonProperty("files") val files: Map<String, File>,
@JsonProperty("owner") val owner: OwnerData
)
data class OwnerData(
@JsonProperty("login") val userName: String,
@JsonProperty("avatar_url") val userAvatar : String
)
data class File (
@JsonProperty("content") val dataRaw: String?
)

data class GistRequestBody(
@JsonProperty("description") val description: String,
@JsonProperty("public") val public : Boolean,
@JsonProperty("files") val files: FilesGist?
)
data class FilesGist(
@JsonProperty("Cloudstream_Backup_data.txt") val description: ContentFilesGist?,
)
data class ContentFilesGist(
@JsonProperty("content") val description: String?,
)

private suspend fun initLogin(githubToken: String): Boolean{
val response = app.get("https://api.github.com/gists",
headers= mapOf(
Pair("Accept" , "application/vnd.github+json"),
Pair("Authorization", "token $githubToken"),
)
)

if (!response.isSuccessful) { return false }

val repo = tryParseJson<List<GistsElements>>(response.text)?.filter {
it.files.keys.first() == "Cloudstream_Backup_data.txt"
}

if (repo?.isEmpty() == true){
val backupData = context?.getBackup()
val gitResponse = app.post("https://api.github.com/gists",
headers= mapOf(
Pair("Accept" , "application/vnd.github+json"),
Pair("Authorization", "token $githubToken"),
),
requestBody = GistRequestBody("Cloudstream private backup gist", false, FilesGist(ContentFilesGist(backupData?.toJson()))).toJson().toRequestBody(
RequestBodyTypes.JSON.toMediaTypeOrNull()))

if (!gitResponse.isSuccessful) {return false}
tryParseJson<GistsElements>(gitResponse.text).let {
setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity(
token = githubToken,
gistId = it?.gistId?: run {
return false
},
userName = it.owner.userName,
userAvatar = it.owner.userAvatar
))
}
return true
}
else{
repo?.first().let {
setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity(
token = githubToken,
gistId = it?.gistId?: run {
return false
},
userName = it.owner.userName,
userAvatar = it.owner.userAvatar
))
ioSafe {
context?.restorePromptGithub()
}
return true
}
}

}
override suspend fun login(data: InAppAuthAPI.LoginData): Boolean {
switchToNewAccount()
val githubToken = data.password ?: throw IllegalArgumentException ("Requires Password")
try {
if (initLogin(githubToken)) {
registerAccount()
return true
}
} catch (e: Exception) {
logError(e)
}
switchToOldAccount()
return false
}

override fun getLatestLoginData(): InAppAuthAPI.LoginData? {
val current = getAuthKey() ?: return null
return InAppAuthAPI.LoginData(server = current.gistId, password = current.token, username = current.userName)
}
override suspend fun initialize() {
currentSession = getAuthKey()
val gistId = currentSession?.gistId ?: return
val token = currentSession?.token ?: return
setKey(gistId, token)
}
override fun logOut() {
removeKey(accountId, GITHUB_USER_KEY)
removeAccountKeys()
currentSession = getAuthKey()
}

override fun loginInfo(): AuthAPI.LoginInfo? {
return getAuthKey()?.let { user ->
AuthAPI.LoginInfo(
profilePicture = user.userAvatar,
name = user.userName,
accountIndex = accountIndex,
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ class OpenSubtitlesApi(index: Int) : InAppAuthAPIManager(index), AbstractSubApi
}

override suspend fun login(data: InAppAuthAPI.LoginData): Boolean {
val username = data.username ?: throw ErrorLoadingException("Requires Username")
val password = data.password ?: throw ErrorLoadingException("Requires Password")
val username = data.username ?: throw IllegalArgumentException ("Requires Username")
val password = data.password ?: throw IllegalArgumentException ("Requires Password")
switchToNewAccount()
try {
if (initLogin(username, password)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class HomeFragment : Fragment() {
val configEvent = Event<Int>()
var currentSpan = 1
val listHomepageItems = mutableListOf<SearchResponse>()
val reloadStoredDataEvent = Event<Unit>()

private val errorProfilePics = listOf(
R.drawable.monke_benene,
Expand Down Expand Up @@ -473,14 +474,20 @@ class HomeFragment : Fragment() {
reloadStored()
afterPluginsLoadedEvent += ::firstLoadHomePage
mainPluginsLoadedEvent += ::firstLoadHomePage
reloadStoredDataEvent += ::reloadStoredEvent
}

override fun onStop() {
afterPluginsLoadedEvent -= ::firstLoadHomePage
mainPluginsLoadedEvent -= ::firstLoadHomePage
reloadStoredDataEvent -= ::reloadStoredEvent
super.onStop()
}

private fun reloadStoredEvent(input: Unit) {
reloadStored()
}

private fun reloadStored() {
homeViewModel.loadResumeWatching()
val list = EnumSet.noneOf(WatchType::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.lagradost.cloudstream3.syncproviders.AccountManager
import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.aniListApi
import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.malApi
import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.openSubtitlesApi
import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.githubApi
import com.lagradost.cloudstream3.syncproviders.AuthAPI
import com.lagradost.cloudstream3.syncproviders.InAppAuthAPI
import com.lagradost.cloudstream3.syncproviders.OAuth2API
Expand Down Expand Up @@ -227,6 +228,8 @@ class SettingsAccount : PreferenceFragmentCompat() {
R.string.mal_key to malApi,
R.string.anilist_key to aniListApi,
R.string.opensubtitles_key to openSubtitlesApi,
R.string.github_key to githubApi

)

for ((key, api) in syncApis) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setPadd
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setUpToolbar
import com.lagradost.cloudstream3.utils.BackupUtils.backup
import com.lagradost.cloudstream3.utils.BackupUtils.restorePrompt
import com.lagradost.cloudstream3.utils.BackupUtils.restorePromptGithub
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
import com.lagradost.cloudstream3.utils.InAppUpdater.Companion.runAutoUpdate
import com.lagradost.cloudstream3.utils.UIHelper.dismissSafe
Expand All @@ -28,6 +29,7 @@ import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.OutputStream


class SettingsUpdates : PreferenceFragmentCompat() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Expand Down
Loading