优化pkg功能

This commit is contained in:
liyp 2024-06-27 13:16:14 +08:00
parent 6d8a3476a8
commit 383cdf1e02

View file

@ -59,7 +59,7 @@ func (a *Pkg) GetMsg() string {
} }
defer req.Body.Close() defer req.Body.Close()
fmt.Println("aur url:", url) fmt.Println("aur suggest url:", url)
re, err := io.ReadAll(req.Body) re, err := io.ReadAll(req.Body)
// fmt.Println(string(re)) // fmt.Println(string(re))
if err != nil { if err != nil {
@ -70,7 +70,10 @@ func (a *Pkg) GetMsg() string {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
if len(suggestions) > 0 { // println(len(suggestions))
if len(suggestions) == 0 {
return "没有找到相关软件"
}
searchUrl := "https://aur.archlinux.org/rpc/v5/search/" + suggestions[0] + "?by=name" searchUrl := "https://aur.archlinux.org/rpc/v5/search/" + suggestions[0] + "?by=name"
searchReq, err := http.Get(searchUrl) searchReq, err := http.Get(searchUrl)
if err != nil { if err != nil {
@ -86,10 +89,12 @@ func (a *Pkg) GetMsg() string {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
// 增加根据投票人数排序
searchResults := searchMap["results"].([]interface{}) searchResults := searchMap["results"].([]interface{})
// println("searchResults:", len(searchResults))
maxVotes := 0.0 maxVotes := 0.0
var searchResult map[string]interface{} var searchResult map[string]interface{}
// 增加根据投票人数排序
if len(searchResults) > 1 {
for _, result := range searchResults { for _, result := range searchResults {
resultMap := result.(map[string]interface{}) resultMap := result.(map[string]interface{})
numVotes := resultMap["NumVotes"].(float64) numVotes := resultMap["NumVotes"].(float64)
@ -98,12 +103,18 @@ func (a *Pkg) GetMsg() string {
searchResult = resultMap searchResult = resultMap
} }
} }
fmt.Println(len(searchResult))
} else if len(searchResults) == 1 {
searchResult = searchResults[0].(map[string]interface{})
}
maintainer, ok := searchResult["Maintainer"].(string) maintainer, ok := searchResult["Maintainer"].(string)
if !ok { if !ok {
maintainer = "孤儿包" maintainer = "孤儿包"
} }
println("maintainer:", maintainer)
// last_update := ''
last_update := time.Unix(int64(searchResult["LastModified"].(float64)), 0).Format("2006-01-02 15:04:05") last_update := time.Unix(int64(searchResult["LastModified"].(float64)), 0).Format("2006-01-02 15:04:05")
var msg string var msg string
msg += "仓库AUR\n" msg += "仓库AUR\n"
msg += "包名:" + searchResult["Name"].(string) + "\n" msg += "包名:" + searchResult["Name"].(string) + "\n"
@ -111,13 +122,10 @@ func (a *Pkg) GetMsg() string {
msg += "描述:" + searchResult["Description"].(string) + "\n" msg += "描述:" + searchResult["Description"].(string) + "\n"
msg += "维护者:" + maintainer + "\n" msg += "维护者:" + maintainer + "\n"
msg += "链接:" + searchResult["URL"].(string) + "\n" msg += "链接:" + searchResult["URL"].(string) + "\n"
msg += "更新时间:" + last_update + "\n" msg += "更新时间:" + last_update
fmt.Println(msg) fmt.Println(msg)
return msg return msg
} else {
return "没有找到相关软件"
}
} }
result := resultSlipe[0].(map[string]interface{}) result := resultSlipe[0].(map[string]interface{})