Skip to content

Commit 4169fd5

Browse files
authored
Fix: Migrate to authdata for better set/get key security, split up auth into files, added preliminary BackupAPI, use Result instead of Resource. (#1822)
1 parent d103884 commit 4169fd5

File tree

18 files changed

+819
-730
lines changed

18 files changed

+819
-730
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package com.lagradost.cloudstream3.syncproviders
2+
3+
import com.lagradost.cloudstream3.AcraApplication.Companion.getKey
4+
import com.lagradost.cloudstream3.AcraApplication.Companion.setKey
5+
import com.lagradost.cloudstream3.LoadResponse
6+
import com.lagradost.cloudstream3.syncproviders.providers.Addic7ed
7+
import com.lagradost.cloudstream3.syncproviders.providers.AniListApi
8+
import com.lagradost.cloudstream3.syncproviders.providers.LocalList
9+
import com.lagradost.cloudstream3.syncproviders.providers.MALApi
10+
import com.lagradost.cloudstream3.syncproviders.providers.OpenSubtitlesApi
11+
import com.lagradost.cloudstream3.syncproviders.providers.SimklApi
12+
import com.lagradost.cloudstream3.syncproviders.providers.SubDlApi
13+
import com.lagradost.cloudstream3.syncproviders.providers.SubSourceApi
14+
import com.lagradost.cloudstream3.utils.DataStoreHelper
15+
import java.util.concurrent.TimeUnit
16+
17+
abstract class AccountManager {
18+
companion object {
19+
const val NONE_ID: Int = -1
20+
val malApi = MALApi()
21+
val aniListApi = AniListApi()
22+
val simklApi = SimklApi()
23+
val localListApi = LocalList()
24+
25+
val openSubtitlesApi = OpenSubtitlesApi()
26+
val addic7ed = Addic7ed()
27+
val subDlApi = SubDlApi()
28+
val subSourceApi = SubSourceApi()
29+
30+
var cachedAccounts: MutableMap<String, Array<AuthData>>
31+
var cachedAccountIds: MutableMap<String, Int>
32+
33+
const val ACCOUNT_TOKEN = "auth_tokens"
34+
const val ACCOUNT_IDS = "auth_ids"
35+
36+
fun accounts(prefix: String): Array<AuthData> {
37+
require(prefix != "NONE")
38+
return getKey<Array<AuthData>>(
39+
ACCOUNT_TOKEN,
40+
"${prefix}/${DataStoreHelper.currentAccount}"
41+
) ?: arrayOf()
42+
}
43+
44+
fun updateAccounts(prefix: String, array: Array<AuthData>) {
45+
require(prefix != "NONE")
46+
setKey(ACCOUNT_TOKEN, "${prefix}/${DataStoreHelper.currentAccount}", array)
47+
synchronized(cachedAccounts) {
48+
cachedAccounts[prefix] = array
49+
}
50+
}
51+
52+
fun updateAccountsId(prefix: String, id: Int) {
53+
require(prefix != "NONE")
54+
setKey(ACCOUNT_IDS, "${prefix}/${DataStoreHelper.currentAccount}", id)
55+
synchronized(cachedAccountIds) {
56+
cachedAccountIds[prefix] = id
57+
}
58+
}
59+
60+
val allApis = arrayOf(
61+
SyncRepo(malApi),
62+
SyncRepo(aniListApi),
63+
SyncRepo(simklApi),
64+
SyncRepo(localListApi),
65+
66+
SubtitleRepo(openSubtitlesApi),
67+
SubtitleRepo(addic7ed),
68+
SubtitleRepo(subDlApi),
69+
SubtitleRepo(subSourceApi)
70+
)
71+
72+
fun updateAccountIds() {
73+
val ids = mutableMapOf<String, Int>()
74+
for (api in allApis) {
75+
ids.put(
76+
api.idPrefix,
77+
getKey<Int>(
78+
ACCOUNT_IDS,
79+
"${api.idPrefix}/${DataStoreHelper.currentAccount}",
80+
NONE_ID
81+
) ?: NONE_ID
82+
)
83+
}
84+
synchronized(cachedAccountIds) {
85+
cachedAccountIds = ids
86+
}
87+
}
88+
89+
init {
90+
val data = mutableMapOf<String, Array<AuthData>>()
91+
val ids = mutableMapOf<String, Int>()
92+
for (api in allApis) {
93+
data.put(api.idPrefix, accounts(api.idPrefix))
94+
ids.put(
95+
api.idPrefix,
96+
getKey<Int>(
97+
ACCOUNT_IDS,
98+
"${api.idPrefix}/${DataStoreHelper.currentAccount}",
99+
NONE_ID
100+
) ?: NONE_ID
101+
)
102+
}
103+
cachedAccounts = data
104+
cachedAccountIds = ids
105+
}
106+
107+
// I do not want to place this in the init block as JVM initialization order is weird, and it may cause exceptions
108+
// accessing other classes
109+
fun initMainAPI() {
110+
LoadResponse.malIdPrefix = malApi.idPrefix
111+
LoadResponse.aniListIdPrefix = aniListApi.idPrefix
112+
LoadResponse.simklIdPrefix = simklApi.idPrefix
113+
}
114+
115+
val subtitleProviders = arrayOf(
116+
SubtitleRepo(openSubtitlesApi),
117+
SubtitleRepo(addic7ed),
118+
SubtitleRepo(subDlApi),
119+
SubtitleRepo(subSourceApi)
120+
)
121+
val syncApis = arrayOf(
122+
SyncRepo(malApi),
123+
SyncRepo(aniListApi),
124+
SyncRepo(simklApi),
125+
SyncRepo(localListApi)
126+
)
127+
128+
const val APP_STRING = "cloudstreamapp"
129+
const val APP_STRING_REPO = "cloudstreamrepo"
130+
const val APP_STRING_PLAYER = "cloudstreamplayer"
131+
132+
// Instantly start the search given a query
133+
const val APP_STRING_SEARCH = "cloudstreamsearch"
134+
135+
// Instantly resume watching a show
136+
const val APP_STRING_RESUME_WATCHING = "cloudstreamcontinuewatching"
137+
138+
fun secondsToReadable(seconds: Int, completedValue: String): String {
139+
var secondsLong = seconds.toLong()
140+
val days = TimeUnit.SECONDS
141+
.toDays(secondsLong)
142+
secondsLong -= TimeUnit.DAYS.toSeconds(days)
143+
144+
val hours = TimeUnit.SECONDS
145+
.toHours(secondsLong)
146+
secondsLong -= TimeUnit.HOURS.toSeconds(hours)
147+
148+
val minutes = TimeUnit.SECONDS
149+
.toMinutes(secondsLong)
150+
secondsLong -= TimeUnit.MINUTES.toSeconds(minutes)
151+
if (minutes < 0) {
152+
return completedValue
153+
}
154+
//println("$days $hours $minutes")
155+
return "${if (days != 0L) "$days" + "d " else ""}${if (hours != 0L) "$hours" + "h " else ""}${minutes}m"
156+
}
157+
}
158+
}

0 commit comments

Comments
 (0)