Skip to content

Commit

Permalink
tag suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
tom5079 committed Nov 19, 2024
1 parent 290b7fb commit 1bcbc5f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ android {
compileSdk 34
targetSdkVersion 34
versionCode 69
versionName "5.3.14"
versionName "5.3.15"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
Expand Down
42 changes: 36 additions & 6 deletions app/src/main/java/xyz/quaver/pupil/hitomi/search.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package xyz.quaver.pupil.hitomi

import kotlinx.serialization.json.jsonArray
import okhttp3.Request
import xyz.quaver.pupil.client
import xyz.quaver.pupil.util.content
import java.net.URL
import java.nio.ByteBuffer
import java.nio.ByteOrder
Expand All @@ -35,6 +37,7 @@ const val compressed_nozomi_prefix = "n"

val tag_index_version: String by lazy { getIndexVersion("tagindex") }
val galleries_index_version: String by lazy { getIndexVersion("galleriesindex") }
val tagIndexDomain = "tagindex.hitomi.la"

fun sha256(data: ByteArray) : ByteArray {
return MessageDigest.getInstance("SHA-256").digest(data)
Expand Down Expand Up @@ -91,6 +94,14 @@ fun getGalleryIDsForQuery(query: String) : Set<Int> {
}
}

fun encodeSearchQueryForUrl(s: Char) =
when(s) {
' ' -> "_"
'/' -> "slash"
'.' -> "dot"
else -> s.toString()
}

fun getSuggestionsForQuery(query: String) : List<Suggestion> {
query.replace('_', ' ').let {
var field = "global"
Expand All @@ -102,14 +113,33 @@ fun getSuggestionsForQuery(query: String) : List<Suggestion> {
term = sides[1]
}

val key = hashTerm(term)
val node = getNodeAtAddress(field, 0) ?: return emptyList()
val data = bSearch(field, key, node)
val chars = term.map(::encodeSearchQueryForUrl)
val url = "https://$tagIndexDomain/$field${if (chars.isNotEmpty()) "/${chars.joinToString("/")}" else ""}.json"

if (data != null)
return getSuggestionsFromData(field, data)
val request = Request.Builder()
.url(url)
.build()

val suggestions = json.parseToJsonElement(client.newCall(request).execute().body()?.use { body -> body.string() } ?: return emptyList())

return buildList {
suggestions.jsonArray.forEach { suggestionRaw ->
val suggestion = suggestionRaw.jsonArray
if (suggestion.size < 3) {
return@forEach
}
val ns = suggestion[2].content ?: ""

return emptyList()
val tagname = sanitize(suggestion[0].content ?: return@forEach)
val url = when(ns) {
"female", "male" -> "/tag/$ns:$tagname${separator}1$extension"
"language" -> "/index-$tagname${separator}1$extension"
else -> "/$ns/$tagname${separator}all${separator}1$extension"
}

add(Suggestion(suggestion[0].content ?: "", suggestion[1].content?.toIntOrNull() ?: 0, url, ns))
}
}
}
}

Expand Down

0 comments on commit 1bcbc5f

Please sign in to comment.