From 83199cf3aa11b759fbdae379f84cd214bb05886c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Sun, 26 Sep 2021 10:15:22 +0800 Subject: [PATCH] Update nbnhhsh.go --- plugin_nbnhhsh/nbnhhsh.go | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/plugin_nbnhhsh/nbnhhsh.go b/plugin_nbnhhsh/nbnhhsh.go index a2aee0b6..018c7964 100644 --- a/plugin_nbnhhsh/nbnhhsh.go +++ b/plugin_nbnhhsh/nbnhhsh.go @@ -23,19 +23,25 @@ func init() { func getValue(text string) []string { urlValues := url.Values{} urlValues.Add("text", text) - resp, _ := http.PostForm("https://lab.magiconch.com/api/nbnhhsh/guess", urlValues) - body, _ := ioutil.ReadAll(resp.Body) - resp.Close() - json := gjson.ParseBytes(body) - res := make([]string, 0) - var jsonPath string - if json.Get("0.trans").Exists() { - jsonPath = "0.trans" - } else { - jsonPath = "0.inputting" + resp, err := http.PostForm("https://lab.magiconch.com/api/nbnhhsh/guess", urlValues) + if err == nil { + body, err := ioutil.ReadAll(resp.Body) + if err == nil { + resp.Body.Close() + json := gjson.ParseBytes(body) + res := make([]string, 0) + var jsonPath string + if json.Get("0.trans").Exists() { + jsonPath = "0.trans" + } else { + jsonPath = "0.inputting" + } + for _, value := range json.Get(jsonPath).Array() { + res = append(res, value.String()) + } + return res + } + return []string{err.Error()} } - for _, value := range json.Get(jsonPath).Array() { - res = append(res, value.String()) - } - return res + return []string{err.Error()} }