komikku/app/src/main/java/exh/uconfig/EHConfigurator.kt

164 lines
5.5 KiB
Kotlin
Raw Normal View History

2018-02-12 22:22:54 +01:00
package exh.uconfig
import android.content.Context
import eu.kanade.tachiyomi.R
2018-02-12 22:22:54 +01:00
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.online.all.EHentai
2020-04-05 01:11:31 +02:00
import eu.kanade.tachiyomi.util.asJsoup
2018-02-12 22:22:54 +01:00
import exh.EH_SOURCE_ID
import exh.EXH_SOURCE_ID
2019-04-14 16:46:06 +02:00
import exh.log.maybeInjectEHLogger
2018-02-12 22:22:54 +01:00
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import timber.log.Timber
import uy.kohesive.injekt.injectLazy
class EHConfigurator(val context: Context) {
2018-02-12 22:22:54 +01:00
private val prefs: PreferencesHelper by injectLazy()
private val sources: SourceManager by injectLazy()
2019-04-14 16:46:06 +02:00
private val configuratorClient = OkHttpClient.Builder()
2020-05-02 06:46:24 +02:00
.maybeInjectEHLogger()
.build()
2018-02-12 22:22:54 +01:00
private fun EHentai.requestWithCreds(sp: Int = 1) = Request.Builder()
2020-05-02 06:46:24 +02:00
.addHeader("Cookie", cookiesHeader(sp))
2018-02-12 22:22:54 +01:00
private fun EHentai.execProfileActions(
action: String,
name: String,
set: String,
sp: Int
) =
2020-05-02 06:46:24 +02:00
configuratorClient.newCall(
requestWithCreds(sp)
2018-02-12 22:22:54 +01:00
.url(uconfigUrl)
2020-05-02 06:46:24 +02:00
.post(
FormBody.Builder()
2018-02-12 22:22:54 +01:00
.add("profile_action", action)
.add("profile_name", name)
.add("profile_set", set)
2020-05-02 06:46:24 +02:00
.build()
)
.build()
)
.execute()
2018-02-12 22:22:54 +01:00
private val EHentai.uconfigUrl get() = baseUrl + UCONFIG_URL
fun configureAll() {
val ehSource = sources.get(EH_SOURCE_ID) as EHentai
val exhSource = sources.get(EXH_SOURCE_ID) as EHentai
// Get hath perks
2020-05-02 06:46:24 +02:00
val perksPage = configuratorClient.newCall(
ehSource.requestWithCreds()
2018-02-12 22:22:54 +01:00
.url(HATH_PERKS_URL)
2020-05-02 06:46:24 +02:00
.build()
)
.execute().asJsoup()
2018-02-12 22:22:54 +01:00
val hathPerks = EHHathPerksResponse()
perksPage.select(".stuffbox tr").forEach {
val name = it.child(0).text().toLowerCase()
val purchased = it.child(2).getElementsByTag("form").isEmpty()
when (name) {
// Thumbnail rows
2018-02-12 22:22:54 +01:00
"more thumbs" -> hathPerks.moreThumbs = purchased
"thumbs up" -> hathPerks.thumbsUp = purchased
"all thumbs" -> hathPerks.allThumbs = purchased
// Pagination sizing
2018-02-12 22:22:54 +01:00
"paging enlargement i" -> hathPerks.pagingEnlargementI = purchased
"paging enlargement ii" -> hathPerks.pagingEnlargementII = purchased
"paging enlargement iii" -> hathPerks.pagingEnlargementIII = purchased
}
}
Timber.d("Hath perks: $hathPerks")
configure(ehSource, hathPerks)
configure(exhSource, hathPerks)
}
2020-08-05 01:32:36 +02:00
private fun configure(source: EHentai, hathPerks: EHHathPerksResponse) {
// Delete old app profiles
2018-02-12 22:22:54 +01:00
val scanReq = source.requestWithCreds().url(source.uconfigUrl).build()
val resp = configuratorClient.newCall(scanReq).execute().asJsoup()
var lastDoc = resp
resp.select(PROFILE_SELECTOR).forEach {
if (it.text() == PROFILE_NAME) {
2018-02-12 22:22:54 +01:00
val id = it.attr("value")
// Delete old profile
2018-02-14 23:21:29 +01:00
lastDoc = source.execProfileActions("delete", "", id, id.toInt()).asJsoup()
2018-02-12 22:22:54 +01:00
}
}
// Find available profile slot
val availableProfiles = (1..3).toMutableList()
2018-02-12 22:22:54 +01:00
lastDoc.select(PROFILE_SELECTOR).forEach {
availableProfiles.remove(it.attr("value").toInt())
}
// No profile slots left :(
2020-05-02 06:46:24 +02:00
if (availableProfiles.isEmpty()) {
throw IllegalStateException(context.getString(R.string.eh_settings_out_of_slots_error, source.name))
2020-05-02 06:46:24 +02:00
}
// Create profile in available slot
2020-05-02 06:46:24 +02:00
2018-02-12 22:22:54 +01:00
val slot = availableProfiles.first()
2020-05-02 06:46:24 +02:00
val response = source.execProfileActions(
"create",
PROFILE_NAME,
slot.toString(),
1
)
2018-02-12 22:22:54 +01:00
// Build new profile
2018-02-12 22:22:54 +01:00
val form = EhUConfigBuilder().build(hathPerks)
// Send new profile to server
2020-05-02 06:46:24 +02:00
configuratorClient.newCall(
source.requestWithCreds(sp = slot)
2018-02-12 22:22:54 +01:00
.url(source.uconfigUrl)
.post(form)
2020-05-02 06:46:24 +02:00
.build()
).execute()
2018-02-12 22:22:54 +01:00
// Persist slot + sk
2018-02-12 22:22:54 +01:00
source.spPref().set(slot)
2018-02-14 23:21:29 +01:00
val keyCookie = response.headers.toMultimap()["Set-Cookie"]?.find {
2018-02-14 23:21:29 +01:00
it.startsWith("sk=")
}?.removePrefix("sk=")?.substringBefore(';')
val sessionCookie = response.headers.toMultimap()["Set-Cookie"]?.find {
it.startsWith("s=")
}?.removePrefix("s=")?.substringBefore(';')
val hathPerksCookie = response.headers.toMultimap()["Set-Cookie"]?.find {
it.startsWith("hath_perks=")
}?.removePrefix("hath_perks=")?.substringBefore(';')
2018-02-14 23:21:29 +01:00
2020-05-02 06:46:24 +02:00
if (keyCookie != null) {
2018-02-14 23:21:29 +01:00
prefs.eh_settingsKey().set(keyCookie)
2020-05-02 06:46:24 +02:00
}
if (sessionCookie != null) {
prefs.eh_sessionCookie().set(sessionCookie)
2020-05-02 06:46:24 +02:00
}
if (hathPerksCookie != null) {
prefs.eh_hathPerksCookies().set(hathPerksCookie)
2020-05-02 06:46:24 +02:00
}
2018-02-12 22:22:54 +01:00
}
companion object {
private const val PROFILE_NAME = "TachiyomiEH App"
private const val UCONFIG_URL = "/uconfig.php"
// Always use E-H here as EXH does not have a perks page
2018-02-12 22:22:54 +01:00
private const val HATH_PERKS_URL = "https://e-hentai.org/hathperks.php"
private const val PROFILE_SELECTOR = "[name=profile_set] > option"
}
}