Request gzipped version of extensions repo

This commit is contained in:
arkon 2020-08-02 22:40:31 -04:00
parent 12ff37d052
commit 40a9595012
2 changed files with 50 additions and 26 deletions

View file

@ -1,43 +1,29 @@
package eu.kanade.tachiyomi.extension.api
import android.content.Context
import com.github.salomonbrys.kotson.fromJson
import com.github.salomonbrys.kotson.get
import com.github.salomonbrys.kotson.int
import com.github.salomonbrys.kotson.string
import com.google.gson.Gson
import com.google.gson.JsonArray
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.extension.model.Extension
import eu.kanade.tachiyomi.extension.model.LoadResult
import eu.kanade.tachiyomi.extension.util.ExtensionLoader
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.NetworkHelper
import eu.kanade.tachiyomi.network.await
import java.util.Date
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.Response
import uy.kohesive.injekt.injectLazy
internal class ExtensionGithubApi {
private val network: NetworkHelper by injectLazy()
private val preferences: PreferencesHelper by injectLazy()
private val gson: Gson by injectLazy()
suspend fun findExtensions(): List<Extension.Available> {
val call = GET(EXT_URL)
val service: ExtensionGithubService = ExtensionGithubService.create()
return withContext(Dispatchers.IO) {
val response = network.client.newCall(call).await()
if (response.isSuccessful) {
parseResponse(response)
} else {
response.close()
throw Exception("Failed to get extensions")
}
val response = service.getRepo()
parseResponse(response)
}
}
@ -64,11 +50,7 @@ internal class ExtensionGithubApi {
return extensionsWithUpdate
}
private fun parseResponse(response: Response): List<Extension.Available> {
val text = response.body?.use { it.string() } ?: return emptyList()
val json = gson.fromJson<JsonArray>(text)
private fun parseResponse(json: JsonArray): List<Extension.Available> {
return json
.filter { element ->
val versionName = element["version"].string
@ -82,18 +64,18 @@ internal class ExtensionGithubApi {
val versionName = element["version"].string
val versionCode = element["code"].int
val lang = element["lang"].string
val icon = "$REPO_URL/icon/${apkName.replace(".apk", ".png")}"
val icon = "$REPO_URL_PREFIX/icon/${apkName.replace(".apk", ".png")}"
Extension.Available(name, pkgName, versionName, versionCode, lang, apkName, icon)
}
}
fun getApkUrl(extension: Extension.Available): String {
return "$REPO_URL/apk/${extension.apkName}"
return "$REPO_URL_PREFIX/apk/${extension.apkName}"
}
companion object {
private const val REPO_URL = "https://raw.githubusercontent.com/inorichi/tachiyomi-extensions/repo"
private const val EXT_URL = "$REPO_URL/index.json"
const val BASE_URL = "https://raw.githubusercontent.com/"
private const val REPO_URL_PREFIX = "${BASE_URL}inorichi/tachiyomi-extensions/repo/"
}
}

View file

@ -0,0 +1,42 @@
package eu.kanade.tachiyomi.extension.api
import com.google.gson.JsonArray
import eu.kanade.tachiyomi.network.NetworkHelper
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import uy.kohesive.injekt.injectLazy
/**
* Used to get the extension repo listing from GitHub.
*/
interface ExtensionGithubService {
companion object {
private val client by lazy {
val network: NetworkHelper by injectLazy()
network.client.newBuilder()
.addNetworkInterceptor { chain ->
val originalResponse = chain.proceed(chain.request())
originalResponse.newBuilder()
.header("Content-Encoding", "gzip")
.header("Content-Type", "application/json")
.build()
}
.build()
}
fun create(): ExtensionGithubService {
val adapter = Retrofit.Builder()
.baseUrl(ExtensionGithubApi.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
return adapter.create(ExtensionGithubService::class.java)
}
}
@GET("/inorichi/tachiyomi-extensions/repo/index.json.gz")
suspend fun getRepo(): JsonArray
}