fix(all/googledriveindex): fix file sizes & use parseAs function (#1828)

This commit is contained in:
Secozzi
2023-07-04 10:35:56 +02:00
committed by GitHub
parent 7db1afbeff
commit 80952ac072
2 changed files with 13 additions and 10 deletions

View File

@ -6,7 +6,7 @@ ext {
extName = 'GoogleDriveIndex'
pkgNameSuffix = 'all.googledriveindex'
extClass = '.GoogleDriveIndex'
extVersionCode = 6
extVersionCode = 7
libVersion = '13'
}

View File

@ -282,10 +282,9 @@ class GoogleDriveIndex : ConfigurableAnimeSource, AnimeHttpSource() {
val popBody = "password=&page_token=$newToken&page_index=$newPageIndex".toRequestBody("application/x-www-form-urlencoded".toMediaType())
val parsedBody = client.newCall(
val parsed = client.newCall(
POST(newParsed.url, body = popBody, headers = popHeaders),
).execute().body.string().decrypt()
val parsed = json.decodeFromString<ResponseData>(parsedBody)
).execute().parseAs<ResponseData> { it.decrypt() }
parsed.data.files.forEach { item ->
if (item.mimeType.startsWith("image/") && item.name.startsWith("cover", true)) {
@ -386,10 +385,9 @@ class GoogleDriveIndex : ConfigurableAnimeSource, AnimeHttpSource() {
val popBody = "password=&page_token=$newToken&page_index=$newPageIndex".toRequestBody("application/x-www-form-urlencoded".toMediaType())
val parsedBody = client.newCall(
val parsed = client.newCall(
POST(url, body = popBody, headers = popHeaders),
).execute().body.string().decrypt()
val parsed = json.decodeFromString<ResponseData>(parsedBody)
).execute().parseAs<ResponseData> { it.decrypt() }
parsed.data.files.forEach { item ->
if (item.mimeType.endsWith("folder")) {
@ -493,6 +491,11 @@ class GoogleDriveIndex : ConfigurableAnimeSource, AnimeHttpSource() {
// ============================= Utilities ==============================
private inline fun <reified T> Response.parseAs(transform: (String) -> String = { it }): T {
val responseBody = use { transform(it.body.string()) }
return json.decodeFromString(responseBody)
}
private fun HttpUrl.hostAndCred(): String {
return if (this.password.isNotBlank() && this.username.isNotBlank()) {
"${this.username}:${this.password}@${this.host}"
@ -532,9 +535,9 @@ class GoogleDriveIndex : ConfigurableAnimeSource, AnimeHttpSource() {
private fun formatFileSize(bytes: Long): String {
return when {
bytes >= 1073741824 -> "%.2f GB".format(bytes / 1073741824.0)
bytes >= 1048576 -> "%.2f MB".format(bytes / 1048576.0)
bytes >= 1024 -> "%.2f KB".format(bytes / 1024.0)
bytes >= 1_000_000_000 -> "%.2f GB".format(bytes / 1_000_000_000.0)
bytes >= 1_000_000 -> "%.2f MB".format(bytes / 1_000_000.0)
bytes >= 1_000 -> "%.2f KB".format(bytes / 1_000.0)
bytes > 1 -> "$bytes bytes"
bytes == 1L -> "$bytes byte"
else -> ""