Skip to content

Commit 1bcbc5f

Browse files
committed
tag suggestion
1 parent 290b7fb commit 1bcbc5f

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ android {
3838
compileSdk 34
3939
targetSdkVersion 34
4040
versionCode 69
41-
versionName "5.3.14"
41+
versionName "5.3.15"
4242
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
4343
vectorDrawables.useSupportLibrary = true
4444
}

app/src/main/java/xyz/quaver/pupil/hitomi/search.kt

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
package xyz.quaver.pupil.hitomi
1818

19+
import kotlinx.serialization.json.jsonArray
1920
import okhttp3.Request
2021
import xyz.quaver.pupil.client
22+
import xyz.quaver.pupil.util.content
2123
import java.net.URL
2224
import java.nio.ByteBuffer
2325
import java.nio.ByteOrder
@@ -35,6 +37,7 @@ const val compressed_nozomi_prefix = "n"
3537

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

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

97+
fun encodeSearchQueryForUrl(s: Char) =
98+
when(s) {
99+
' ' -> "_"
100+
'/' -> "slash"
101+
'.' -> "dot"
102+
else -> s.toString()
103+
}
104+
94105
fun getSuggestionsForQuery(query: String) : List<Suggestion> {
95106
query.replace('_', ' ').let {
96107
var field = "global"
@@ -102,14 +113,33 @@ fun getSuggestionsForQuery(query: String) : List<Suggestion> {
102113
term = sides[1]
103114
}
104115

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

109-
if (data != null)
110-
return getSuggestionsFromData(field, data)
119+
val request = Request.Builder()
120+
.url(url)
121+
.build()
122+
123+
val suggestions = json.parseToJsonElement(client.newCall(request).execute().body()?.use { body -> body.string() } ?: return emptyList())
124+
125+
return buildList {
126+
suggestions.jsonArray.forEach { suggestionRaw ->
127+
val suggestion = suggestionRaw.jsonArray
128+
if (suggestion.size < 3) {
129+
return@forEach
130+
}
131+
val ns = suggestion[2].content ?: ""
111132

112-
return emptyList()
133+
val tagname = sanitize(suggestion[0].content ?: return@forEach)
134+
val url = when(ns) {
135+
"female", "male" -> "/tag/$ns:$tagname${separator}1$extension"
136+
"language" -> "/index-$tagname${separator}1$extension"
137+
else -> "/$ns/$tagname${separator}all${separator}1$extension"
138+
}
139+
140+
add(Suggestion(suggestion[0].content ?: "", suggestion[1].content?.toIntOrNull() ?: 0, url, ns))
141+
}
142+
}
113143
}
114144
}
115145

0 commit comments

Comments
 (0)