code optimize

and change debug logs on Nbnhhsh command to trace logs
This commit is contained in:
A.C.Sukazyo Eyre 2023-10-20 11:45:58 +08:00
parent c19134811d
commit e6b68ae307
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
2 changed files with 14 additions and 11 deletions

View File

@ -45,26 +45,26 @@ class Nbnhhsh (using coeur: MornyCoeur) extends ITelegramCommand {
val message = StringBuilder(NBNHHSH_RESULT_HEAD_HTML) val message = StringBuilder(NBNHHSH_RESULT_HEAD_HTML)
import cc.sukazyo.cono.morny.Log.logger import cc.sukazyo.cono.morny.Log.logger
logger debug s"**xx len=${queryResp.words.length}" logger trace s"**nbnhhsh got len=${queryResp.words.length}"
for (_word <- queryResp.words) { for (_word <- queryResp.words) {
logger debug "**exec" logger trace s"**start for ${_word.name}"
val _use_trans = (_word.trans ne null) && (_word.trans nonEmpty) val _use_trans = (_word.trans ne null) && (_word.trans nonEmpty)
val _use_inputting = (_word.inputting ne null) && (_word.inputting nonEmpty) val _use_inputting = (_word.inputting ne null) && (_word.inputting nonEmpty)
if (_use_trans || _use_inputting) if (_use_trans || _use_inputting)
message ++= s"\n\n<b>[[ ${h(_word.name)} ]]</b>" message ++= s"\n\n<b>[[ ${h(_word.name)} ]]</b>"
logger debug s"**used [${_word.name}]" logger trace s"**used [${_word.name}]"
if (_use_trans) for (_trans <- _word.trans) if (_use_trans) for (_trans <- _word.trans)
message ++= s"\n* <i>${h(_trans)}</i>" message ++= s"\n* <i>${h(_trans)}</i>"
logger debug s"**used [${_word.name}] used `${_trans}``" logger trace s"**used [${_word.name}] used `${_trans}``"
if (_use_inputting) if (_use_inputting)
logger debug s"**used [${_word.name}] inputting" logger trace s"**used [${_word.name}] inputting"
if (_use_trans) if (_use_trans)
message += '\n' message += '\n'
message ++= " maybe:" message ++= " maybe:"
for (_inputting <- _word.inputting) for (_inputting <- _word.inputting)
logger debug s"**used [${_word.name}] used-i ${_inputting}" logger trace s"**used [${_word.name}] used-i ${_inputting}"
message ++= s"\n` <i>${h(_inputting)}</i>" message ++= s"\n` <i>${h(_inputting)}</i>"
logger debug s"**exec as ${_word.name}" logger trace s"**done"
} }
coeur.account exec SendMessage( coeur.account exec SendMessage(

View File

@ -7,11 +7,13 @@ import sttp.model.MediaType
object NbnhhshQuery { object NbnhhshQuery {
case class Word (name: String, trans: Array[String], inputting: Array[String]) case class Word (
name: String,
trans: Array[String] = Array.empty,
inputting: Array[String] = Array.empty
)
case class GuessResult (words: Array[Word]) case class GuessResult (words: Array[Word])
private case class GuessRequest (text: String)
private val API_URL = uri"https://lab.magiconch.com/api/nbnhhsh/" private val API_URL = uri"https://lab.magiconch.com/api/nbnhhsh/"
private val API_GUESS_METHOD = uri"$API_URL/guess/" private val API_GUESS_METHOD = uri"$API_URL/guess/"
@ -19,12 +21,13 @@ object NbnhhshQuery {
@throws[HttpError[_]|SttpClientException] @throws[HttpError[_]|SttpClientException]
def sendGuess (text: String): GuessResult = { def sendGuess (text: String): GuessResult = {
case class GuessRequest (text: String)
val http = basicRequest val http = basicRequest
.body(Gson().toJson(GuessRequest(text))).contentType(MediaType.ApplicationJson) .body(Gson().toJson(GuessRequest(text))).contentType(MediaType.ApplicationJson)
.post(API_GUESS_METHOD) .post(API_GUESS_METHOD)
.response(asString.getRight) .response(asString.getRight)
.send(httpClient) .send(httpClient)
Gson().fromJson(s"{ 'words': ${http.body} }", classOf[GuessResult]) GuessResult(Gson().fromJson(http.body, classOf[Array[Word]]))
} }
} }