This commit is contained in:
Jannik 2022-03-19 20:14:16 +01:00
parent ab660d0ae7
commit a092c5b8be
Signed by: Seil0
GPG Key ID: E8459F3723C52C24
3 changed files with 21 additions and 3 deletions

View File

@ -68,8 +68,8 @@ dependencies {
implementation 'com.github.bumptech.glide:glide:4.12.0'
implementation 'jp.wasabeef:glide-transformations:4.3.0'
implementation 'com.afollestad.material-dialogs:core:3.3.0'
implementation 'com.afollestad.material-dialogs:bottomsheets:3.3.0'
implementation 'com.afollestad.material-dialogs:core:3.3.0' // TODO remove once unused
implementation 'com.afollestad.material-dialogs:bottomsheets:3.3.0' // TODO remove once unused
implementation "io.ktor:ktor-client-core:$ktor_version"
implementation "io.ktor:ktor-client-android:$ktor_version"

View File

@ -52,6 +52,8 @@ object Crunchyroll {
}
}
private const val baseUrl = "https://beta-api.crunchyroll.com"
private const val basicApiTokenUrl = "https://gitlab.com/-/snippets/2274956/raw/main/snippetfile1.txt"
private var basicApiToken: String = ""
private lateinit var token: Token
private var tokenValidUntil: Long = 0
@ -64,6 +66,19 @@ object Crunchyroll {
private val browsingCache = arrayListOf<Item>()
/**
* Load the pai token, see:
* https://git.mosad.xyz/mosad/NonePublicIssues/issues/1
*
* TODO handle empty file
*/
fun initBasicApiToken() = runBlocking {
withContext(Dispatchers.IO) {
basicApiToken = (client.get(basicApiTokenUrl) as HttpResponse).readText()
Log.i(TAG, "basic auth token: $basicApiToken")
}
}
/**
* Login to the crunchyroll API.
*
@ -85,7 +100,7 @@ object Crunchyroll {
withContext(Dispatchers.IO) {
// TODO handle exceptions
val response: HttpResponse = client.submitForm("$baseUrl$tokenEndpoint", formParameters = formData) {
header("Authorization", "Basic ")
header("Authorization", "Basic $basicApiToken")
}
token = response.receive()
tokenValidUntil = System.currentTimeMillis() + (token.expiresIn * 1000)

View File

@ -137,6 +137,9 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
Preferences.load(this)
EncryptedPreferences.readCredentials(this)
// always initialize the api token
Crunchyroll.initBasicApiToken()
// show onboarding if no password is set, or login fails
if (EncryptedPreferences.password.isEmpty() || !Crunchyroll.login(
EncryptedPreferences.login,