16
16
17
17
package xyz.quaver.pupil.hitomi
18
18
19
+ import kotlinx.serialization.json.jsonArray
19
20
import okhttp3.Request
20
21
import xyz.quaver.pupil.client
22
+ import xyz.quaver.pupil.util.content
21
23
import java.net.URL
22
24
import java.nio.ByteBuffer
23
25
import java.nio.ByteOrder
@@ -35,6 +37,7 @@ const val compressed_nozomi_prefix = "n"
35
37
36
38
val tag_index_version: String by lazy { getIndexVersion(" tagindex" ) }
37
39
val galleries_index_version: String by lazy { getIndexVersion(" galleriesindex" ) }
40
+ val tagIndexDomain = " tagindex.hitomi.la"
38
41
39
42
fun sha256 (data : ByteArray ) : ByteArray {
40
43
return MessageDigest .getInstance(" SHA-256" ).digest(data)
@@ -91,6 +94,14 @@ fun getGalleryIDsForQuery(query: String) : Set<Int> {
91
94
}
92
95
}
93
96
97
+ fun encodeSearchQueryForUrl (s : Char ) =
98
+ when (s) {
99
+ ' ' -> " _"
100
+ ' /' -> " slash"
101
+ ' .' -> " dot"
102
+ else -> s.toString()
103
+ }
104
+
94
105
fun getSuggestionsForQuery (query : String ) : List <Suggestion > {
95
106
query.replace(' _' , ' ' ).let {
96
107
var field = " global"
@@ -102,14 +113,33 @@ fun getSuggestionsForQuery(query: String) : List<Suggestion> {
102
113
term = sides[1 ]
103
114
}
104
115
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"
108
118
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 ? : " "
111
132
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
+ }
113
143
}
114
144
}
115
145
0 commit comments