Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
03c5cfa791 | ||
|
|
e8056072b8 | ||
|
|
d134639a5f | ||
|
|
b4745d76b8 | ||
|
|
c5fd674020 | ||
|
|
9b821dd7cb | ||
|
|
1b441f6aea | ||
|
|
213902c854 | ||
|
|
2054922586 | ||
|
|
a17b7355f5 | ||
|
|
066a1e1f3a | ||
|
|
b10cbfbd63 |
17
.idea/deploymentTargetDropDown.xml
generated
17
.idea/deploymentTargetDropDown.xml
generated
@@ -1,17 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="deploymentTargetDropDown">
|
|
||||||
<runningDeviceTargetSelectedWithDropDown>
|
|
||||||
<Target>
|
|
||||||
<type value="RUNNING_DEVICE_TARGET" />
|
|
||||||
<deviceKey>
|
|
||||||
<Key>
|
|
||||||
<type value="VIRTUAL_DEVICE_PATH" />
|
|
||||||
<value value="$USER_HOME$/.android/avd/Pixel_2_API_30.avd" />
|
|
||||||
</Key>
|
|
||||||
</deviceKey>
|
|
||||||
</Target>
|
|
||||||
</runningDeviceTargetSelectedWithDropDown>
|
|
||||||
<timeTargetWasSelectedWithDropDown value="2022-01-01T07:38:11.679673Z" />
|
|
||||||
</component>
|
|
||||||
</project>
|
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
*Pupil, Hitomi.la viewer for Android*
|
*Pupil, Hitomi.la viewer for Android*
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/tom5079/Pupil/releases/download/5.1.30/Pupil-v5.1.30.apk)
|
[](https://github.com/tom5079/Pupil/releases/download/5.2.0/Pupil-v5.2.0.apk)
|
||||||
[](https://discord.gg/Stj4b5v)
|
[](https://discord.gg/Stj4b5v)
|
||||||
|
|
||||||
# Features
|
# Features
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ android {
|
|||||||
minSdkVersion 16
|
minSdkVersion 16
|
||||||
targetSdkVersion 30
|
targetSdkVersion 30
|
||||||
versionCode 69
|
versionCode 69
|
||||||
versionName "5.1.30"
|
versionName "5.2.1"
|
||||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||||
vectorDrawables.useSupportLibrary = true
|
vectorDrawables.useSupportLibrary = true
|
||||||
}
|
}
|
||||||
@@ -126,7 +126,6 @@ dependencies {
|
|||||||
implementation "ru.noties.markwon:core:3.1.0"
|
implementation "ru.noties.markwon:core:3.1.0"
|
||||||
|
|
||||||
implementation "org.jsoup:jsoup:1.14.3"
|
implementation "org.jsoup:jsoup:1.14.3"
|
||||||
implementation "com.github.seven332:quickjs-android:0.1.0"
|
|
||||||
|
|
||||||
implementation "com.google.guava:guava:31.0.1-android"
|
implementation "com.google.guava:guava:31.0.1-android"
|
||||||
|
|
||||||
@@ -134,6 +133,7 @@ dependencies {
|
|||||||
implementation "xyz.quaver:floatingsearchview:1.1.7"
|
implementation "xyz.quaver:floatingsearchview:1.1.7"
|
||||||
|
|
||||||
testImplementation "junit:junit:4.13.1"
|
testImplementation "junit:junit:4.13.1"
|
||||||
|
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.0"
|
||||||
androidTestImplementation "androidx.test.ext:junit:1.1.2"
|
androidTestImplementation "androidx.test.ext:junit:1.1.2"
|
||||||
androidTestImplementation "androidx.test:rules:1.3.0"
|
androidTestImplementation "androidx.test:rules:1.3.0"
|
||||||
androidTestImplementation "androidx.test:runner:1.3.0"
|
androidTestImplementation "androidx.test:runner:1.3.0"
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
"type": "SINGLE",
|
"type": "SINGLE",
|
||||||
"filters": [],
|
"filters": [],
|
||||||
"versionCode": 69,
|
"versionCode": 69,
|
||||||
"versionName": "5.1.30",
|
"versionName": "5.2.1",
|
||||||
"outputFile": "app-release.apk"
|
"outputFile": "app-release.apk"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -20,10 +20,15 @@
|
|||||||
|
|
||||||
package xyz.quaver.pupil
|
package xyz.quaver.pupil
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
|
import android.webkit.*
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||||
import androidx.test.platform.app.InstrumentationRegistry
|
import androidx.test.platform.app.InstrumentationRegistry
|
||||||
|
import kotlinx.coroutines.*
|
||||||
|
import org.junit.Before
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.junit.runner.RunWith
|
import org.junit.runner.RunWith
|
||||||
|
import xyz.quaver.pupil.hitomi.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instrumented test, which will execute on an Android device.
|
* Instrumented test, which will execute on an Android device.
|
||||||
@@ -32,10 +37,95 @@ import org.junit.runner.RunWith
|
|||||||
*/
|
*/
|
||||||
@RunWith(AndroidJUnit4::class)
|
@RunWith(AndroidJUnit4::class)
|
||||||
class ExampleInstrumentedTest {
|
class ExampleInstrumentedTest {
|
||||||
|
@Before
|
||||||
|
fun init() {
|
||||||
|
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
|
||||||
|
|
||||||
|
runBlocking {
|
||||||
|
withContext(Dispatchers.Main) {
|
||||||
|
webView = WebView(appContext).apply {
|
||||||
|
settings.javaScriptEnabled = true
|
||||||
|
|
||||||
|
addJavascriptInterface(object {
|
||||||
|
@JavascriptInterface
|
||||||
|
fun onResult(uid: String, result: String) {
|
||||||
|
_webViewFlow.tryEmit(uid to result)
|
||||||
|
}
|
||||||
|
}, "Callback")
|
||||||
|
|
||||||
|
loadDataWithBaseURL(
|
||||||
|
"https://hitomi.la/",
|
||||||
|
"""
|
||||||
|
<script src="https://ltn.hitomi.la/jquery.min.js"></script>
|
||||||
|
<script src="https://ltn.hitomi.la/common.js"></script>
|
||||||
|
<script src="https://ltn.hitomi.la/search.js"></script>
|
||||||
|
<script src="https://ltn.hitomi.la/searchlib.js"></script>
|
||||||
|
<script src="https://ltn.hitomi.la/results.js></script>
|
||||||
|
""".trimIndent(),
|
||||||
|
"text/html",
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun useAppContext() {
|
fun test_getGalleryIDsFromNozomi() {
|
||||||
// Context of the app under test.
|
runBlocking {
|
||||||
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
|
val result = getGalleryIDsFromNozomi(null, "index", "all")
|
||||||
|
|
||||||
|
Log.d("PUPILD", "getGalleryIDsFromNozomi: ${result.size}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun test_getGalleryIDsForQuery() {
|
||||||
|
runBlocking {
|
||||||
|
val result = getGalleryIDsForQuery("female:crotch tattoo")
|
||||||
|
|
||||||
|
Log.d("PUPILD", "getGalleryIDsForQuery: ${result.size}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun test_getSuggestionsForQuery() {
|
||||||
|
runBlocking {
|
||||||
|
val result = getSuggestionsForQuery("fem")
|
||||||
|
|
||||||
|
Log.d("PUPILD", "getSuggestionsForQuery: ${result.size}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun test_urlFromUrlFromHash() {
|
||||||
|
runBlocking {
|
||||||
|
val galleryInfo = getGalleryInfo(2102416)
|
||||||
|
|
||||||
|
val result = galleryInfo.files.map {
|
||||||
|
imageUrlFromImage(2102416, it, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.d("PUPILD", result.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun test_getGalleryInfo() {
|
||||||
|
runBlocking {
|
||||||
|
val galleryInfo = getGalleryInfo(2102416)
|
||||||
|
|
||||||
|
Log.d("PUPILD", galleryInfo.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun test_getGalleryBlock() {
|
||||||
|
runBlocking {
|
||||||
|
val block = getGalleryBlock(2102731)
|
||||||
|
|
||||||
|
Log.d("PUPILD", block.toString())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
package xyz.quaver.pupil
|
package xyz.quaver.pupil
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.app.Notification
|
import android.app.Notification
|
||||||
import android.app.NotificationChannel
|
import android.app.NotificationChannel
|
||||||
@@ -26,6 +27,9 @@ import android.content.Context
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
|
import android.util.Log
|
||||||
|
import android.webkit.*
|
||||||
|
import android.widget.Toast
|
||||||
import androidx.appcompat.app.AppCompatDelegate
|
import androidx.appcompat.app.AppCompatDelegate
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
@@ -34,16 +38,22 @@ import com.github.piasy.biv.loader.fresco.FrescoImageLoader
|
|||||||
import com.google.android.gms.common.GooglePlayServicesNotAvailableException
|
import com.google.android.gms.common.GooglePlayServicesNotAvailableException
|
||||||
import com.google.android.gms.common.GooglePlayServicesRepairableException
|
import com.google.android.gms.common.GooglePlayServicesRepairableException
|
||||||
import com.google.android.gms.security.ProviderInstaller
|
import com.google.android.gms.security.ProviderInstaller
|
||||||
|
import com.google.android.material.snackbar.Snackbar
|
||||||
import com.google.firebase.crashlytics.FirebaseCrashlytics
|
import com.google.firebase.crashlytics.FirebaseCrashlytics
|
||||||
import okhttp3.Dispatcher
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import okhttp3.Interceptor
|
import kotlinx.coroutines.Dispatchers
|
||||||
import okhttp3.OkHttpClient
|
import kotlinx.coroutines.channels.BufferOverflow
|
||||||
import okhttp3.Response
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||||
|
import kotlinx.coroutines.flow.asSharedFlow
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import okhttp3.*
|
||||||
import xyz.quaver.io.FileX
|
import xyz.quaver.io.FileX
|
||||||
import xyz.quaver.pupil.types.Tag
|
import xyz.quaver.pupil.types.Tag
|
||||||
import xyz.quaver.pupil.util.*
|
import xyz.quaver.pupil.util.*
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.net.URL
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import java.util.concurrent.Executors
|
import java.util.concurrent.Executors
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
@@ -69,9 +79,64 @@ val client: OkHttpClient
|
|||||||
clientHolder = it
|
clientHolder = it
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressLint("StaticFieldLeak")
|
||||||
|
lateinit var webView: WebView
|
||||||
|
val _webViewFlow = MutableSharedFlow<Pair<String, String>>(
|
||||||
|
extraBufferCapacity = 2,
|
||||||
|
onBufferOverflow = BufferOverflow.DROP_OLDEST
|
||||||
|
)
|
||||||
|
val webViewFlow = _webViewFlow.asSharedFlow()
|
||||||
|
var webViewReady = false
|
||||||
|
|
||||||
|
private lateinit var userAgent: String
|
||||||
|
|
||||||
class Pupil : Application() {
|
class Pupil : Application() {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
lateinit var instance: Pupil
|
||||||
|
private set
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressLint("SetJavaScriptEnabled")
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
|
instance = this
|
||||||
|
|
||||||
|
webView = WebView(this).apply {
|
||||||
|
with (settings) {
|
||||||
|
javaScriptEnabled = true
|
||||||
|
domStorageEnabled = true
|
||||||
|
}
|
||||||
|
|
||||||
|
userAgent = settings.userAgentString
|
||||||
|
|
||||||
|
webViewClient = object: WebViewClient() {
|
||||||
|
override fun onPageFinished(view: WebView?, url: String?) {
|
||||||
|
webViewReady = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
addJavascriptInterface(object {
|
||||||
|
@JavascriptInterface
|
||||||
|
fun onResult(uid: String, result: String) {
|
||||||
|
_webViewFlow.tryEmit(uid to result)
|
||||||
|
}
|
||||||
|
}, "Callback")
|
||||||
|
|
||||||
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
|
val html = URL("https://tom5079.github.io/Pupil/hitomi.html").readText()
|
||||||
|
|
||||||
|
launch(Dispatchers.Main) {
|
||||||
|
loadDataWithBaseURL(
|
||||||
|
"https://hitomi.la/",
|
||||||
|
html,
|
||||||
|
"text/html",
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
|
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
|
||||||
|
|
||||||
preferences = PreferenceManager.getDefaultSharedPreferences(this)
|
preferences = PreferenceManager.getDefaultSharedPreferences(this)
|
||||||
@@ -90,7 +155,10 @@ class Pupil : Application() {
|
|||||||
.readTimeout(0, TimeUnit.SECONDS)
|
.readTimeout(0, TimeUnit.SECONDS)
|
||||||
.proxyInfo(proxyInfo)
|
.proxyInfo(proxyInfo)
|
||||||
.addInterceptor { chain ->
|
.addInterceptor { chain ->
|
||||||
val request = chain.request()
|
val request = chain.request().newBuilder()
|
||||||
|
.header("User-Agent", userAgent)
|
||||||
|
.build()
|
||||||
|
|
||||||
val tag = request.tag() ?: return@addInterceptor chain.proceed(request)
|
val tag = request.tag() ?: return@addInterceptor chain.proceed(request)
|
||||||
|
|
||||||
interceptors[tag::class]?.invoke(chain) ?: chain.proceed(request)
|
interceptors[tag::class]?.invoke(chain) ?: chain.proceed(request)
|
||||||
|
|||||||
@@ -54,12 +54,3 @@ fun URL.readText(settings: HeaderSetter? = null): String {
|
|||||||
|
|
||||||
return client.newCall(request).execute().also{ if (it.code() != 200) throw IOException() }.body()?.use { it.string() } ?: throw IOException()
|
return client.newCall(request).execute().also{ if (it.code() != 200) throw IOException() }.body()?.use { it.string() } ?: throw IOException()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun URL.readBytes(settings: HeaderSetter? = null): ByteArray {
|
|
||||||
val request = Request.Builder()
|
|
||||||
.url(this).let {
|
|
||||||
settings?.invoke(it) ?: it
|
|
||||||
}.build()
|
|
||||||
|
|
||||||
return client.newCall(request).execute().also { if (it.code() != 200) throw IOException() }.body()?.use { it.bytes() } ?: throw IOException()
|
|
||||||
}
|
|
||||||
@@ -16,126 +16,99 @@
|
|||||||
|
|
||||||
package xyz.quaver.pupil.hitomi
|
package xyz.quaver.pupil.hitomi
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import com.hippo.quickjs.android.QuickJS
|
import android.webkit.WebView
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import android.webkit.WebViewClient
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.sync.Mutex
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.coroutines.sync.withLock
|
import kotlinx.coroutines.flow.transformWhile
|
||||||
|
import kotlinx.serialization.ExperimentalSerializationApi
|
||||||
import kotlinx.serialization.decodeFromString
|
import kotlinx.serialization.decodeFromString
|
||||||
|
import kotlinx.serialization.encodeToString
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
import xyz.quaver.json
|
import xyz.quaver.json
|
||||||
|
import xyz.quaver.pupil.Pupil
|
||||||
|
import xyz.quaver.pupil.webView
|
||||||
|
import xyz.quaver.pupil.webViewFlow
|
||||||
|
import xyz.quaver.pupil.webViewReady
|
||||||
import xyz.quaver.readText
|
import xyz.quaver.readText
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
import java.nio.charset.Charset
|
import java.nio.charset.Charset
|
||||||
|
import java.util.*
|
||||||
|
import kotlin.coroutines.resume
|
||||||
|
import kotlin.coroutines.suspendCoroutine
|
||||||
|
|
||||||
const val protocol = "https:"
|
const val protocol = "https:"
|
||||||
|
|
||||||
|
suspend inline fun WebView.evaluate(script: String): String = withContext(Dispatchers.Main) {
|
||||||
|
while (!webViewReady) yield()
|
||||||
|
|
||||||
|
val result: String = suspendCoroutine { continuation ->
|
||||||
|
evaluateJavascript(script) {
|
||||||
|
continuation.resume(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
|
suspend inline fun WebView.evaluatePromise(script: String, then: String = ".then(result => Callback.onResult(%uid, JSON.stringify(result)))"): String = withContext(Dispatchers.Main) {
|
||||||
|
while (!webViewReady) yield()
|
||||||
|
|
||||||
|
val uid = UUID.randomUUID().toString()
|
||||||
|
|
||||||
|
evaluateJavascript((script+then).replace("%uid", "'$uid'"), null)
|
||||||
|
|
||||||
|
val flow: Flow<Pair<String, String>> = webViewFlow.transformWhile { (currentUid, result) ->
|
||||||
|
if (currentUid == uid) emit(currentUid to result)
|
||||||
|
currentUid != uid
|
||||||
|
}
|
||||||
|
|
||||||
|
flow.first().second
|
||||||
|
}
|
||||||
|
|
||||||
@Suppress("EXPERIMENTAL_API_USAGE")
|
@Suppress("EXPERIMENTAL_API_USAGE")
|
||||||
fun getGalleryInfo(galleryID: Int) =
|
suspend fun getGalleryInfo(galleryID: Int): GalleryInfo {
|
||||||
json.decodeFromString<GalleryInfo>(
|
val result = webView.evaluatePromise(
|
||||||
URL("$protocol//$domain/galleries/$galleryID.js").readText()
|
"""
|
||||||
.replace("var galleryinfo = ", "")
|
new Promise((resolve, reject) => {
|
||||||
|
$.getScript('https://$domain/galleries/$galleryID.js', () => {
|
||||||
|
resolve(galleryinfo)
|
||||||
|
});
|
||||||
|
})
|
||||||
|
""".trimIndent()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return json.decodeFromString(result)
|
||||||
|
}
|
||||||
|
|
||||||
//common.js
|
//common.js
|
||||||
const val domain = "ltn.hitomi.la"
|
const val domain = "ltn.hitomi.la"
|
||||||
const val galleryblockextension = ".html"
|
|
||||||
const val galleryblockdir = "galleryblock"
|
const val galleryblockdir = "galleryblock"
|
||||||
const val nozomiextension = ".nozomi"
|
const val nozomiextension = ".nozomi"
|
||||||
|
|
||||||
interface gg {
|
val String?.js: String
|
||||||
fun m(g: Int): Int
|
get() = if (this == null) "null" else "'$this'"
|
||||||
val b: String
|
|
||||||
fun s(h: String): String
|
|
||||||
|
|
||||||
companion object {
|
@OptIn(ExperimentalSerializationApi::class)
|
||||||
@Volatile private var instance: gg? = null
|
suspend fun urlFromUrlFromHash(galleryID: Int, image: GalleryFiles, dir: String? = null, ext: String? = null, base: String? = null): String {
|
||||||
|
val result = webView.evaluate(
|
||||||
|
"""
|
||||||
|
url_from_url_from_hash(
|
||||||
|
${galleryID.toString().js},
|
||||||
|
${Json.encodeToString(image)},
|
||||||
|
${dir.js}, ${ext.js}, ${base.js}
|
||||||
|
)
|
||||||
|
""".trimIndent()
|
||||||
|
)
|
||||||
|
|
||||||
fun getInstance(): gg =
|
return Json.decodeFromString(result)
|
||||||
instance ?: synchronized(this) {
|
|
||||||
instance ?: object: gg {
|
|
||||||
private val ggjs by lazy { URL("https://ltn.hitomi.la/gg.js").readText(Charset.defaultCharset()) }
|
|
||||||
private val quickJS = QuickJS.Builder().build()
|
|
||||||
|
|
||||||
override fun m(g: Int): Int =
|
|
||||||
quickJS.createJSRuntime().use { runtime ->
|
|
||||||
runtime.createJSContext().use { context ->
|
|
||||||
context.evaluate(ggjs, "gg.js")
|
|
||||||
context.evaluate("gg.m($g)", "gg.js", Int::class.java)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override val b: String
|
suspend fun imageUrlFromImage(galleryID: Int, image: GalleryFiles, noWebp: Boolean) : String {
|
||||||
get() =
|
|
||||||
quickJS.createJSRuntime().use { runtime ->
|
|
||||||
runtime.createJSContext().use { context ->
|
|
||||||
context.evaluate(ggjs, "gg.js")
|
|
||||||
context.evaluate("gg.b", "gg.js", String::class.java)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun s(h: String): String =
|
|
||||||
quickJS.createJSRuntime().use { runtime ->
|
|
||||||
runtime.createJSContext().use { context ->
|
|
||||||
context.evaluate(ggjs, "gg.js")
|
|
||||||
context.evaluate("gg.s('$h')", "gg.js", String::class.java)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.also { instance = it }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun subdomainFromURL(url: String, base: String? = null) : String {
|
|
||||||
var retval = "b"
|
|
||||||
|
|
||||||
if (!base.isNullOrBlank())
|
|
||||||
retval = base
|
|
||||||
|
|
||||||
val b = 16
|
|
||||||
|
|
||||||
val r = Regex("""/[0-9a-f]{61}([0-9a-f]{2})([0-9a-f])""")
|
|
||||||
val m = r.find(url) ?: return "a"
|
|
||||||
|
|
||||||
val g = m.groupValues.let { it[2]+it[1] }.toIntOrNull(b)
|
|
||||||
|
|
||||||
if (g != null) {
|
|
||||||
retval = (97+ gg.getInstance().m(g)).toChar().toString() + retval
|
|
||||||
}
|
|
||||||
|
|
||||||
return retval
|
|
||||||
}
|
|
||||||
|
|
||||||
fun urlFromUrl(url: String, base: String? = null) : String {
|
|
||||||
return url.replace(Regex("""//..?\.hitomi\.la/"""), "//${subdomainFromURL(url, base)}.hitomi.la/")
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fun fullPathFromHash(hash: String) : String =
|
|
||||||
"${gg.getInstance().b}${gg.getInstance().s(hash)}/$hash"
|
|
||||||
|
|
||||||
fun realFullPathFromHash(hash: String): String =
|
|
||||||
hash.replace(Regex("""^.*(..)(.)$"""), "$2/$1/$hash")
|
|
||||||
|
|
||||||
fun urlFromHash(galleryID: Int, image: GalleryFiles, dir: String? = null, ext: String? = null) : String {
|
|
||||||
val ext = ext ?: dir ?: image.name.takeLastWhile { it != '.' }
|
|
||||||
val dir = dir ?: "images"
|
|
||||||
return "https://a.hitomi.la/$dir/${fullPathFromHash(image.hash)}.$ext"
|
|
||||||
}
|
|
||||||
|
|
||||||
fun urlFromUrlFromHash(galleryID: Int, image: GalleryFiles, dir: String? = null, ext: String? = null, base: String? = null) =
|
|
||||||
if (base == "tn")
|
|
||||||
urlFromUrl("https://a.hitomi.la/$dir/${realFullPathFromHash(image.hash)}.$ext", base)
|
|
||||||
else
|
|
||||||
urlFromUrl(urlFromHash(galleryID, image, dir, ext), base)
|
|
||||||
|
|
||||||
fun rewriteTnPaths(html: String) =
|
|
||||||
html.replace(Regex("""//tn\.hitomi\.la/[^/]+/[0-9a-f]/[0-9a-f]{2}/[0-9a-f]{64}""")) { url ->
|
|
||||||
urlFromUrl(url.value, "tn")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun imageUrlFromImage(galleryID: Int, image: GalleryFiles, noWebp: Boolean) : String {
|
|
||||||
return when {
|
return when {
|
||||||
noWebp ->
|
noWebp ->
|
||||||
urlFromUrlFromHash(galleryID, image)
|
urlFromUrlFromHash(galleryID, image)
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ data class Gallery(
|
|||||||
val tags: List<String>,
|
val tags: List<String>,
|
||||||
val thumbnails: List<String>
|
val thumbnails: List<String>
|
||||||
)
|
)
|
||||||
fun getGallery(galleryID: Int) : Gallery {
|
suspend fun getGallery(galleryID: Int) : Gallery {
|
||||||
val url = Jsoup.parse(URL("https://hitomi.la/galleries/$galleryID.html").readText())
|
val url = Jsoup.parse(URL("https://hitomi.la/galleries/$galleryID.html").readText())
|
||||||
.select("link").attr("href")
|
.select("link").attr("href")
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ package xyz.quaver.pupil.hitomi
|
|||||||
|
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
import org.jsoup.Jsoup
|
import org.jsoup.Jsoup
|
||||||
|
import xyz.quaver.pupil.webView
|
||||||
import xyz.quaver.readText
|
import xyz.quaver.readText
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
import java.net.URLDecoder
|
import java.net.URLDecoder
|
||||||
@@ -26,42 +27,6 @@ import java.nio.ByteOrder
|
|||||||
import java.util.*
|
import java.util.*
|
||||||
import javax.net.ssl.HttpsURLConnection
|
import javax.net.ssl.HttpsURLConnection
|
||||||
|
|
||||||
//galleryblock.js
|
|
||||||
fun fetchNozomi(area: String? = null, tag: String = "index", language: String = "all", start: Int = -1, count: Int = -1) : Pair<List<Int>, Int> {
|
|
||||||
val url =
|
|
||||||
when(area) {
|
|
||||||
null -> "$protocol//$domain/$tag-$language$nozomiextension"
|
|
||||||
else -> "$protocol//$domain/$area/$tag-$language$nozomiextension"
|
|
||||||
}
|
|
||||||
|
|
||||||
with(URL(url).openConnection() as HttpsURLConnection) {
|
|
||||||
requestMethod = "GET"
|
|
||||||
|
|
||||||
if (start != -1 && count != -1) {
|
|
||||||
val startByte = start*4
|
|
||||||
val endByte = (start+count)*4-1
|
|
||||||
|
|
||||||
setRequestProperty("Range", "bytes=$startByte-$endByte")
|
|
||||||
}
|
|
||||||
|
|
||||||
connect()
|
|
||||||
|
|
||||||
val totalItems = getHeaderField("Content-Range")
|
|
||||||
.replace(Regex("^[Bb]ytes \\d+-\\d+/"), "").toInt() / 4
|
|
||||||
|
|
||||||
val nozomi = ArrayList<Int>()
|
|
||||||
|
|
||||||
val arrayBuffer = ByteBuffer
|
|
||||||
.wrap(inputStream.readBytes())
|
|
||||||
.order(ByteOrder.BIG_ENDIAN)
|
|
||||||
|
|
||||||
while (arrayBuffer.hasRemaining())
|
|
||||||
nozomi.add(arrayBuffer.int)
|
|
||||||
|
|
||||||
return Pair(nozomi, totalItems)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class GalleryBlock(
|
data class GalleryBlock(
|
||||||
val id: Int,
|
val id: Int,
|
||||||
@@ -75,10 +40,21 @@ data class GalleryBlock(
|
|||||||
val relatedTags: List<String>
|
val relatedTags: List<String>
|
||||||
)
|
)
|
||||||
|
|
||||||
fun getGalleryBlock(galleryID: Int) : GalleryBlock {
|
suspend fun getGalleryBlock(galleryID: Int) : GalleryBlock {
|
||||||
val url = "$protocol//$domain/$galleryblockdir/$galleryID$extension"
|
val url = "$protocol//$domain/$galleryblockdir/$galleryID$extension"
|
||||||
|
|
||||||
val doc = Jsoup.parse(rewriteTnPaths(URL(url).readText()))
|
val html: String = webView.evaluatePromise(
|
||||||
|
"""
|
||||||
|
$.get('$url').always(function(data, status) {
|
||||||
|
if (status === 'success') {
|
||||||
|
Callback.onResult(%uid, data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
""".trimIndent(),
|
||||||
|
then = ""
|
||||||
|
)
|
||||||
|
|
||||||
|
val doc = Jsoup.parse(html)
|
||||||
|
|
||||||
val galleryUrl = doc.selectFirst("h1 > a")!!.attr("href")
|
val galleryUrl = doc.selectFirst("h1 > a")!!.attr("href")
|
||||||
|
|
||||||
@@ -101,5 +77,3 @@ fun getGalleryBlock(galleryID: Int) : GalleryBlock {
|
|||||||
|
|
||||||
return GalleryBlock(galleryID, galleryUrl, thumbnails, title, artists, series, type, language, relatedTags)
|
return GalleryBlock(galleryID, galleryUrl, thumbnails, title, artists, series, type, language, relatedTags)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getGalleryBlockOrNull(galleryID: Int) = runCatching { getGalleryBlock(galleryID) }.getOrNull()
|
|
||||||
@@ -44,6 +44,6 @@ data class GalleryFiles(
|
|||||||
|
|
||||||
//Set header `Referer` to reader url to avoid 403 error
|
//Set header `Referer` to reader url to avoid 403 error
|
||||||
@Deprecated("", replaceWith = ReplaceWith("getGalleryInfo"))
|
@Deprecated("", replaceWith = ReplaceWith("getGalleryInfo"))
|
||||||
fun getReader(galleryID: Int) : GalleryInfo {
|
suspend fun getReader(galleryID: Int) : GalleryInfo {
|
||||||
return getGalleryInfo(galleryID)
|
return getGalleryInfo(galleryID)
|
||||||
}
|
}
|
||||||
@@ -16,13 +16,10 @@
|
|||||||
|
|
||||||
package xyz.quaver.pupil.hitomi
|
package xyz.quaver.pupil.hitomi
|
||||||
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.async
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
fun doSearch(query: String, sortByPopularity: Boolean = false) : Set<Int> {
|
suspend fun doSearch(query: String, sortByPopularity: Boolean = false) : Set<Int> = coroutineScope {
|
||||||
val terms = query
|
val terms = query
|
||||||
.trim()
|
.trim()
|
||||||
.replace(Regex("""^\?"""), "")
|
.replace(Regex("""^\?"""), "")
|
||||||
@@ -43,16 +40,16 @@ fun doSearch(query: String, sortByPopularity: Boolean = false) : Set<Int> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val positiveResults = positiveTerms.map {
|
val positiveResults = positiveTerms.map {
|
||||||
CoroutineScope(Dispatchers.IO).async {
|
async {
|
||||||
kotlin.runCatching {
|
runCatching {
|
||||||
getGalleryIDsForQuery(it)
|
getGalleryIDsForQuery(it)
|
||||||
}.getOrElse { emptySet() }
|
}.getOrElse { emptySet() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val negativeResults = negativeTerms.map {
|
val negativeResults = negativeTerms.map {
|
||||||
CoroutineScope(Dispatchers.IO).async {
|
async {
|
||||||
kotlin.runCatching {
|
runCatching {
|
||||||
getGalleryIDsForQuery(it)
|
getGalleryIDsForQuery(it)
|
||||||
}.getOrElse { emptySet() }
|
}.getOrElse { emptySet() }
|
||||||
}
|
}
|
||||||
@@ -64,15 +61,14 @@ fun doSearch(query: String, sortByPopularity: Boolean = false) : Set<Int> {
|
|||||||
else -> emptySet()
|
else -> emptySet()
|
||||||
}
|
}
|
||||||
|
|
||||||
runBlocking {
|
fun filterPositive(newResults: Set<Int>) {
|
||||||
@Synchronized fun filterPositive(newResults: Set<Int>) {
|
|
||||||
results = when {
|
results = when {
|
||||||
results.isEmpty() -> newResults
|
results.isEmpty() -> newResults
|
||||||
else -> results intersect newResults
|
else -> results intersect newResults
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized fun filterNegative(newResults: Set<Int>) {
|
fun filterNegative(newResults: Set<Int>) {
|
||||||
results = results subtract newResults
|
results = results subtract newResults
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +81,6 @@ fun doSearch(query: String, sortByPopularity: Boolean = false) : Set<Int> {
|
|||||||
negativeResults.forEach {
|
negativeResults.forEach {
|
||||||
filterNegative(it.await())
|
filterNegative(it.await())
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return results
|
results
|
||||||
}
|
}
|
||||||
@@ -16,315 +16,37 @@
|
|||||||
|
|
||||||
package xyz.quaver.pupil.hitomi
|
package xyz.quaver.pupil.hitomi
|
||||||
|
|
||||||
import okhttp3.Request
|
import kotlinx.coroutines.Dispatchers
|
||||||
import xyz.quaver.pupil.client
|
import kotlinx.coroutines.withContext
|
||||||
import xyz.quaver.readBytes
|
import kotlinx.serialization.ExperimentalSerializationApi
|
||||||
import xyz.quaver.readText
|
import kotlinx.serialization.Serializable
|
||||||
import java.net.URL
|
import kotlinx.serialization.decodeFromString
|
||||||
import java.nio.ByteBuffer
|
import kotlinx.serialization.json.Json
|
||||||
import java.nio.ByteOrder
|
import xyz.quaver.pupil.webView
|
||||||
import java.security.MessageDigest
|
|
||||||
import kotlin.math.min
|
|
||||||
|
|
||||||
//searchlib.js
|
//searchlib.js
|
||||||
const val separator = "-"
|
|
||||||
const val extension = ".html"
|
const val extension = ".html"
|
||||||
const val index_dir = "tagindex"
|
|
||||||
const val galleries_index_dir = "galleriesindex"
|
|
||||||
const val max_node_size = 464
|
|
||||||
const val B = 16
|
|
||||||
const val compressed_nozomi_prefix = "n"
|
|
||||||
|
|
||||||
val tag_index_version: String by lazy { getIndexVersion("tagindex") }
|
@OptIn(ExperimentalSerializationApi::class)
|
||||||
val galleries_index_version: String by lazy { getIndexVersion("galleriesindex") }
|
suspend fun getGalleryIDsForQuery(query: String) : Set<Int> {
|
||||||
|
val result = webView.evaluatePromise("get_galleryids_for_query('$query')")
|
||||||
|
|
||||||
fun sha256(data: ByteArray) : ByteArray {
|
return Json.decodeFromString(result)
|
||||||
return MessageDigest.getInstance("SHA-256").digest(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
@OptIn(ExperimentalUnsignedTypes::class)
|
|
||||||
fun hashTerm(term: String) : UByteArray {
|
|
||||||
return sha256(term.toByteArray()).toUByteArray().sliceArray(0 until 4)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun sanitize(input: String) : String {
|
|
||||||
return input.replace(Regex("[/#]"), "")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getIndexVersion(name: String) =
|
|
||||||
URL("$protocol//$domain/$name/version?_=${System.currentTimeMillis()}").readText()
|
|
||||||
|
|
||||||
//search.js
|
|
||||||
fun getGalleryIDsForQuery(query: String) : Set<Int> {
|
|
||||||
query.replace("_", " ").let {
|
|
||||||
if (it.indexOf(':') > -1) {
|
|
||||||
val sides = it.split(":")
|
|
||||||
val ns = sides[0]
|
|
||||||
var tag = sides[1]
|
|
||||||
|
|
||||||
var area : String? = ns
|
|
||||||
var language = "all"
|
|
||||||
when (ns) {
|
|
||||||
"female", "male" -> {
|
|
||||||
area = "tag"
|
|
||||||
tag = it
|
|
||||||
}
|
|
||||||
"language" -> {
|
|
||||||
area = null
|
|
||||||
language = tag
|
|
||||||
tag = "index"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return getGalleryIDsFromNozomi(area, tag, language)
|
|
||||||
}
|
|
||||||
|
|
||||||
val key = hashTerm(it)
|
|
||||||
val field = "galleries"
|
|
||||||
|
|
||||||
val node = getNodeAtAddress(field, 0) ?: return emptySet()
|
|
||||||
|
|
||||||
val data = bSearch(field, key, node)
|
|
||||||
|
|
||||||
if (data != null)
|
|
||||||
return getGalleryIDsFromData(data)
|
|
||||||
|
|
||||||
return emptySet()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getSuggestionsForQuery(query: String) : List<Suggestion> {
|
|
||||||
query.replace('_', ' ').let {
|
|
||||||
var field = "global"
|
|
||||||
var term = it
|
|
||||||
|
|
||||||
if (term.indexOf(':') > -1) {
|
|
||||||
val sides = it.split(':')
|
|
||||||
field = sides[0]
|
|
||||||
term = sides[1]
|
|
||||||
}
|
|
||||||
|
|
||||||
val key = hashTerm(term)
|
|
||||||
val node = getNodeAtAddress(field, 0) ?: return emptyList()
|
|
||||||
val data = bSearch(field, key, node)
|
|
||||||
|
|
||||||
if (data != null)
|
|
||||||
return getSuggestionsFromData(field, data)
|
|
||||||
|
|
||||||
return emptyList()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Serializable
|
||||||
data class Suggestion(val s: String, val t: Int, val u: String, val n: String)
|
data class Suggestion(val s: String, val t: Int, val u: String, val n: String)
|
||||||
fun getSuggestionsFromData(field: String, data: Pair<Long, Int>) : List<Suggestion> {
|
|
||||||
val url = "$protocol//$domain/$index_dir/$field.$tag_index_version.data"
|
|
||||||
val (offset, length) = data
|
|
||||||
if (length > 10000 || length <= 0)
|
|
||||||
throw Exception("length $length is too long")
|
|
||||||
|
|
||||||
val inbuf = getURLAtRange(url, offset.until(offset+length))
|
@OptIn(ExperimentalSerializationApi::class)
|
||||||
|
suspend fun getSuggestionsForQuery(query: String) : List<Suggestion> {
|
||||||
|
val result = webView.evaluatePromise("get_suggestions_for_query('$query')")
|
||||||
|
|
||||||
val suggestions = ArrayList<Suggestion>()
|
return Json.decodeFromString<List<List<Suggestion>?>>(result)[0]!!
|
||||||
|
|
||||||
val buffer = ByteBuffer
|
|
||||||
.wrap(inbuf)
|
|
||||||
.order(ByteOrder.BIG_ENDIAN)
|
|
||||||
val numberOfSuggestions = buffer.int
|
|
||||||
|
|
||||||
if (numberOfSuggestions > 100 || numberOfSuggestions <= 0)
|
|
||||||
throw Exception("number of suggestions $numberOfSuggestions is too long")
|
|
||||||
|
|
||||||
for (i in 0.until(numberOfSuggestions)) {
|
|
||||||
var top = buffer.int
|
|
||||||
|
|
||||||
val ns = inbuf.sliceArray(buffer.position().until(buffer.position()+top)).toString(charset("UTF-8"))
|
|
||||||
buffer.position(buffer.position()+top)
|
|
||||||
|
|
||||||
top = buffer.int
|
|
||||||
|
|
||||||
val tag = inbuf.sliceArray(buffer.position().until(buffer.position()+top)).toString(charset("UTF-8"))
|
|
||||||
buffer.position(buffer.position()+top)
|
|
||||||
|
|
||||||
val count = buffer.int
|
|
||||||
|
|
||||||
val tagname = sanitize(tag)
|
|
||||||
val u =
|
|
||||||
when(ns) {
|
|
||||||
"female", "male" -> "/tag/$ns:$tagname${separator}1$extension"
|
|
||||||
"language" -> "/index-$tagname${separator}1$extension"
|
|
||||||
else -> "/$ns/$tagname${separator}all${separator}1$extension"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suggestions.add(Suggestion(tag, count, u, ns))
|
@OptIn(ExperimentalSerializationApi::class)
|
||||||
}
|
suspend fun getGalleryIDsFromNozomi(area: String?, tag: String, language: String) : Set<Int> {
|
||||||
|
val jsArea = if (area == null) "null" else "'$area'"
|
||||||
return suggestions
|
|
||||||
}
|
return Json.decodeFromString(webView.evaluatePromise("""get_galleryids_from_nozomi($jsArea, '$tag', '$language')"""))
|
||||||
|
|
||||||
fun getGalleryIDsFromNozomi(area: String?, tag: String, language: String) : Set<Int> {
|
|
||||||
val nozomiAddress =
|
|
||||||
when(area) {
|
|
||||||
null -> "$protocol//$domain/$compressed_nozomi_prefix/$tag-$language$nozomiextension"
|
|
||||||
else -> "$protocol//$domain/$compressed_nozomi_prefix/$area/$tag-$language$nozomiextension"
|
|
||||||
}
|
|
||||||
|
|
||||||
val bytes = try {
|
|
||||||
URL(nozomiAddress).readBytes()
|
|
||||||
} catch (e: Exception) {
|
|
||||||
return emptySet()
|
|
||||||
}
|
|
||||||
|
|
||||||
val nozomi = mutableSetOf<Int>()
|
|
||||||
|
|
||||||
val arrayBuffer = ByteBuffer
|
|
||||||
.wrap(bytes)
|
|
||||||
.order(ByteOrder.BIG_ENDIAN)
|
|
||||||
|
|
||||||
while (arrayBuffer.hasRemaining())
|
|
||||||
nozomi.add(arrayBuffer.int)
|
|
||||||
|
|
||||||
return nozomi
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getGalleryIDsFromData(data: Pair<Long, Int>) : Set<Int> {
|
|
||||||
val url = "$protocol//$domain/$galleries_index_dir/galleries.$galleries_index_version.data"
|
|
||||||
val (offset, length) = data
|
|
||||||
if (length > 100000000 || length <= 0)
|
|
||||||
throw Exception("length $length is too long")
|
|
||||||
|
|
||||||
val inbuf = getURLAtRange(url, offset.until(offset+length))
|
|
||||||
|
|
||||||
val galleryIDs = mutableSetOf<Int>()
|
|
||||||
|
|
||||||
val buffer = ByteBuffer
|
|
||||||
.wrap(inbuf)
|
|
||||||
.order(ByteOrder.BIG_ENDIAN)
|
|
||||||
|
|
||||||
val numberOfGalleryIDs = buffer.int
|
|
||||||
|
|
||||||
val expectedLength = numberOfGalleryIDs*4+4
|
|
||||||
|
|
||||||
if (numberOfGalleryIDs > 10000000 || numberOfGalleryIDs <= 0)
|
|
||||||
throw Exception("number_of_galleryids $numberOfGalleryIDs is too long")
|
|
||||||
else if (inbuf.size != expectedLength)
|
|
||||||
throw Exception("inbuf.byteLength ${inbuf.size} != expected_length $expectedLength")
|
|
||||||
|
|
||||||
for (i in 0.until(numberOfGalleryIDs))
|
|
||||||
galleryIDs.add(buffer.int)
|
|
||||||
|
|
||||||
return galleryIDs
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getNodeAtAddress(field: String, address: Long) : Node? {
|
|
||||||
val url =
|
|
||||||
when(field) {
|
|
||||||
"galleries" -> "$protocol//$domain/$galleries_index_dir/galleries.$galleries_index_version.index"
|
|
||||||
"languages" -> "$protocol//$domain/$galleries_index_dir/languages.$galleries_index_version.index"
|
|
||||||
"nozomiurl" -> "$protocol//$domain/$galleries_index_dir/nozomiurl.$galleries_index_version.index"
|
|
||||||
else -> "$protocol//$domain/$index_dir/$field.$tag_index_version.index"
|
|
||||||
}
|
|
||||||
|
|
||||||
val nodedata = getURLAtRange(url, address.until(address+max_node_size))
|
|
||||||
|
|
||||||
return decodeNode(nodedata)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getURLAtRange(url: String, range: LongRange) : ByteArray {
|
|
||||||
val request = Request.Builder()
|
|
||||||
.url(url)
|
|
||||||
.header("Range", "bytes=${range.first}-${range.last}")
|
|
||||||
.build()
|
|
||||||
|
|
||||||
return client.newCall(request).execute().body()?.use { it.bytes() } ?: byteArrayOf()
|
|
||||||
}
|
|
||||||
|
|
||||||
@OptIn(ExperimentalUnsignedTypes::class)
|
|
||||||
data class Node(val keys: List<UByteArray>, val datas: List<Pair<Long, Int>>, val subNodeAddresses: List<Long>)
|
|
||||||
@OptIn(ExperimentalUnsignedTypes::class)
|
|
||||||
fun decodeNode(data: ByteArray) : Node {
|
|
||||||
val buffer = ByteBuffer
|
|
||||||
.wrap(data)
|
|
||||||
.order(ByteOrder.BIG_ENDIAN)
|
|
||||||
|
|
||||||
val uData = data.toUByteArray()
|
|
||||||
|
|
||||||
val numberOfKeys = buffer.int
|
|
||||||
val keys = ArrayList<UByteArray>()
|
|
||||||
|
|
||||||
for (i in 0.until(numberOfKeys)) {
|
|
||||||
val keySize = buffer.int
|
|
||||||
|
|
||||||
if (keySize == 0 || keySize > 32)
|
|
||||||
throw Exception("fatal: !keySize || keySize > 32")
|
|
||||||
|
|
||||||
keys.add(uData.sliceArray(buffer.position().until(buffer.position()+keySize)))
|
|
||||||
buffer.position(buffer.position()+keySize)
|
|
||||||
}
|
|
||||||
|
|
||||||
val numberOfDatas = buffer.int
|
|
||||||
val datas = ArrayList<Pair<Long, Int>>()
|
|
||||||
|
|
||||||
for (i in 0.until(numberOfDatas)) {
|
|
||||||
val offset = buffer.long
|
|
||||||
val length = buffer.int
|
|
||||||
|
|
||||||
datas.add(Pair(offset, length))
|
|
||||||
}
|
|
||||||
|
|
||||||
val numberOfSubNodeAddresses = B+1
|
|
||||||
val subNodeAddresses = ArrayList<Long>()
|
|
||||||
|
|
||||||
for (i in 0.until(numberOfSubNodeAddresses)) {
|
|
||||||
val subNodeAddress = buffer.long
|
|
||||||
subNodeAddresses.add(subNodeAddress)
|
|
||||||
}
|
|
||||||
|
|
||||||
return Node(keys, datas, subNodeAddresses)
|
|
||||||
}
|
|
||||||
|
|
||||||
@OptIn(ExperimentalUnsignedTypes::class)
|
|
||||||
fun bSearch(field: String, key: UByteArray, node: Node) : Pair<Long, Int>? {
|
|
||||||
fun compareArrayBuffers(dv1: UByteArray, dv2: UByteArray) : Int {
|
|
||||||
val top = min(dv1.size, dv2.size)
|
|
||||||
|
|
||||||
for (i in 0.until(top)) {
|
|
||||||
if (dv1[i] < dv2[i])
|
|
||||||
return -1
|
|
||||||
else if (dv1[i] > dv2[i])
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
fun locateKey(key: UByteArray, node: Node) : Pair<Boolean, Int> {
|
|
||||||
for (i in node.keys.indices) {
|
|
||||||
val cmpResult = compareArrayBuffers(key, node.keys[i])
|
|
||||||
|
|
||||||
if (cmpResult <= 0)
|
|
||||||
return Pair(cmpResult==0, i)
|
|
||||||
}
|
|
||||||
|
|
||||||
return Pair(false, node.keys.size)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun isLeaf(node: Node) : Boolean {
|
|
||||||
for (subnode in node.subNodeAddresses)
|
|
||||||
if (subnode != 0L)
|
|
||||||
return false
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if (node.keys.isEmpty())
|
|
||||||
return null
|
|
||||||
|
|
||||||
val (there, where) = locateKey(key, node)
|
|
||||||
if (there)
|
|
||||||
return node.datas[where]
|
|
||||||
else if (isLeaf(node))
|
|
||||||
return null
|
|
||||||
|
|
||||||
val nextNode = getNodeAtAddress(field, node.subNodeAddresses[where]) ?: return null
|
|
||||||
return bSearch(field, key, nextNode)
|
|
||||||
}
|
}
|
||||||
@@ -41,12 +41,9 @@ import okhttp3.ResponseBody
|
|||||||
import okio.*
|
import okio.*
|
||||||
import xyz.quaver.pupil.*
|
import xyz.quaver.pupil.*
|
||||||
import xyz.quaver.pupil.ui.ReaderActivity
|
import xyz.quaver.pupil.ui.ReaderActivity
|
||||||
import xyz.quaver.pupil.util.cleanCache
|
import xyz.quaver.pupil.util.*
|
||||||
import xyz.quaver.pupil.util.downloader.Cache
|
import xyz.quaver.pupil.util.downloader.Cache
|
||||||
import xyz.quaver.pupil.util.downloader.DownloadManager
|
import xyz.quaver.pupil.util.downloader.DownloadManager
|
||||||
import xyz.quaver.pupil.util.ellipsize
|
|
||||||
import xyz.quaver.pupil.util.normalizeID
|
|
||||||
import xyz.quaver.pupil.util.requestBuilders
|
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import kotlin.math.ceil
|
import kotlin.math.ceil
|
||||||
@@ -357,7 +354,7 @@ class DownloadService : Service() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
galleryInfo.requestBuilders.forEachIndexed { index, it ->
|
galleryInfo.getRequestBuilders().forEachIndexed { index, it ->
|
||||||
if (progress[galleryID]?.get(index)?.isInfinite() == false) {
|
if (progress[galleryID]?.get(index)?.isInfinite() == false) {
|
||||||
val request = it.tag(Tag(galleryID, index, startId)).build()
|
val request = it.tag(Tag(galleryID, index, startId)).build()
|
||||||
client.newCall(request).enqueue(callback)
|
client.newCall(request).enqueue(callback)
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import android.os.Build
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.text.InputType
|
import android.text.InputType
|
||||||
import android.text.util.Linkify
|
import android.text.util.Linkify
|
||||||
|
import android.util.Log
|
||||||
import android.view.KeyEvent
|
import android.view.KeyEvent
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import android.view.View
|
import android.view.View
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ package xyz.quaver.pupil.util.downloader
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.ContextWrapper
|
import android.content.ContextWrapper
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
import android.util.Log
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|||||||
@@ -97,8 +97,7 @@ fun GalleryBlock.formatDownloadFolderTest(format: String): String =
|
|||||||
}
|
}
|
||||||
}.replace(Regex("""[*\\|"?><:/]"""), "").ellipsize(127)
|
}.replace(Regex("""[*\\|"?><:/]"""), "").ellipsize(127)
|
||||||
|
|
||||||
val GalleryInfo.requestBuilders: List<Request.Builder>
|
suspend fun GalleryInfo.getRequestBuilders(): List<Request.Builder> {
|
||||||
get() {
|
|
||||||
val galleryID = this.id ?: 0
|
val galleryID = this.id ?: 0
|
||||||
val lowQuality = Preferences["low_quality", true]
|
val lowQuality = Preferences["low_quality", true]
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user