package workers import ( "fmt" "strings" "time" "github.com/goccy/go-json" "github.com/parnurzeal/gorequest" ) func init() { RegisterWorkerFactory("pkg", func(parms []string, uid, gid, role, mid, rawMsg string) Worker { return &Pkg{ StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), } }) } type Pkg struct { *StdAns } func (a *Pkg) GetMsg() string { if len(a.Parms) < 2 { return "请输入包名 如:/pkg linux 查询 linux 相关软件" } // 去除换行符 raw_msg := strings.TrimRight(a.RawMsg, "\n") raw_msg = strings.TrimRight(raw_msg, "\r") fmt.Println("raw_msg:", raw_msg) parms := strings.Split(raw_msg, " ") url := "https://archlinux.org/packages/search/json/?name=" + parms[1] if len(parms) > 2 && parms[1] != "" { url += "&repo=" + strings.ToUpper(parms[2][:1]) + parms[2][1:] } // 输出请求地址 // fmt.Println("pkg url:", url) request := gorequest.New() _, body, errs := request.Get(url).End() if len(errs) > 0 { return "服务器网络错误!" } // fmt.Println("pkg body:", string(body)) // var pkg []Package var pkg map[string]interface{} err := json.Unmarshal([]byte(body), &pkg) if err != nil { return err.Error() } // fmt.Println("pkg:", pkg) resultSlipe := pkg["results"].([]interface{}) if len(resultSlipe) == 0 { url := "https://aur.archlinux.org/rpc/v5/suggest/" + parms[1] _, body, errs := request.Get(url).End() if len(errs) > 0 { fmt.Println(errs) } // fmt.Println("aur suggest url:", url) // fmt.Println(string(re)) var suggestions []string err := json.Unmarshal([]byte(body), &suggestions) if err != nil { fmt.Println(err) } // println(len(suggestions)) if len(suggestions) == 0 { return "没有找到相关软件" } var searchMap map[string]interface{} var searchResult map[string]interface{} if len(suggestions) == 1 || suggestions[0] == parms[1] { searchUrl := "https://aur.archlinux.org/rpc/v5/info/" + suggestions[0] _, body, errs = request.Get(searchUrl).End() if len(errs) > 0 { fmt.Println("searchUrl err:", errs) return "服务器网络错误!" } err = json.Unmarshal([]byte(body), &searchMap) if err != nil { fmt.Println("解析失败:", err) } searchResult = searchMap["results"].([]interface{})[0].(map[string]interface{}) } else { maxVotes := 0.0 var searchResults []map[string]interface{} for _, suggestion := range suggestions { searchUrl := "https://aur.archlinux.org/rpc/v5/info/" + suggestion _, body, errs = request.Get(searchUrl).End() if len(errs) > 0 { fmt.Println("searchUrl err:", errs) } err = json.Unmarshal([]byte(body), &searchMap) if err != nil { fmt.Println(err) } results := searchMap["results"].([]interface{}) searchResults = append(searchResults, results[0].(map[string]interface{})) // searchResults = append(searchResults, searchMap["results"].([]interface{})[0].(map[string]interface{})) // println("searchResults:", len(searchResults)) } // 增加根据投票人数排序 for _, result := range searchResults { // resultMap := result.(map[string]interface{}) numVotes := result["NumVotes"].(float64) if numVotes > maxVotes { maxVotes = numVotes searchResult = result } } } maintainer, ok := searchResult["Maintainer"].(string) if !ok { maintainer = "孤儿包" } OutOfDate := "" _, ok = searchResult["OutOfDate"].(float64) if ok { OutOfDate = fmt.Sprintf("过期时间:%s\n", time.Unix(int64(searchResult["OutOfDate"].(float64)), 0).Format("2006-01-02 15:04:05")) } var msg string msg += "仓库:AUR\n" msg += "包名:" + searchResult["Name"].(string) + "\n" msg += "版本:" + searchResult["Version"].(string) + "\n" msg += "描述:" + searchResult["Description"].(string) + "\n" msg += "维护者:" + maintainer upstream, ok := searchResult["URL"].(string) if !ok || upstream == "" { upstream = "无" } coMaintainers, ok := searchResult["CoMaintainers"].([]interface{}) if ok { msg += " ( " for _, coMaintainer := range coMaintainers { msg += coMaintainer.(string) + " " } msg += ")" } msg += "\n" msg += "上游:" + upstream + "\n" msg += OutOfDate last_update := time.Unix(int64(searchResult["LastModified"].(float64)), 0).Format("2006-01-02 15:04:05") msg += "更新时间:" + last_update + "\n" msg += "投票:" + fmt.Sprintf("%.0f", searchResult["NumVotes"].(float64)) + "\n" msg += "AUR 链接:https://aur.archlinux.org/packages/" + searchResult["Name"].(string) + "\n" // fmt.Println(msg) return msg } result := resultSlipe[0].(map[string]interface{}) var msg string last_update := result["last_update"].(string) last_update = strings.Replace(last_update, "T", " ", 1) last_update = strings.Replace(last_update, "Z", "", 1) msg += "仓库:" + result["repo"].(string) + "\n" msg += "包名:" + result["pkgname"].(string) + "\n" msg += "版本:" + result["pkgver"].(string) + "\n" msg += "描述:" + result["pkgdesc"].(string) + "\n" msg += "打包:" + result["packager"].(string) + "\n" msg += "上游:" + result["url"].(string) + "\n" msg += "更新日期:" + last_update return msg }