🎨 改进代码结构

This commit is contained in:
Yiwen-Chan
2021-04-12 18:48:12 +08:00
parent 45a2808c13
commit f19721a683
7 changed files with 132 additions and 116 deletions

View File

@@ -1,6 +1,7 @@
package github
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
@@ -9,71 +10,81 @@ import (
"github.com/tidwall/gjson"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
)
func init() { // 插件主体
zero.OnRegex(`>G\s(.*)`).SetBlock(true).FirstPriority().
Handle(func(ctx *zero.Ctx) {
// 发送请求
header := http.Header{
"User-Agent": []string{"User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36"},
}
api, _ := url.Parse("https://api.github.com/search/repositories")
params := url.Values{
api.RawQuery = url.Values{
"q": []string{ctx.State["regex_matched"].([]string)[1]},
}
api.RawQuery = params.Encode()
link := api.String()
client := &http.Client{}
req, err := http.NewRequest("GET", link, nil)
}.Encode()
body, err := netGet(api.String(), header)
if err != nil {
ctx.Send(fmt.Sprintf("ERROR: %v", err))
ctx.SendChain(message.Text("ERROR: ", err))
}
// 解析请求
info := gjson.ParseBytes(body)
if info.Get("total_count").Int() == 0 {
ctx.SendChain(message.Text("ERROR: 没有找到这样的仓库"))
return
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36")
resp, err := client.Do(req)
if err != nil {
ctx.Send(fmt.Sprintf("ERROR: %v", err))
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
ctx.Send(fmt.Sprintf("ERROR: %v", err))
return
}
if code := resp.StatusCode; code != 200 {
// 如果返回不是200则立刻抛出错误
ctx.Send(fmt.Sprintf("ERROR: code %d", code))
return
}
count := gjson.ParseBytes(body).Get("total_count").Int()
if count == 0 {
ctx.Send("没有找到这样的仓库")
return
}
repo := gjson.ParseBytes(body).Get("items.0")
language := repo.Get("language").Str
if language == "" {
language = "None"
}
license := strings.ToUpper(repo.Get("license.key").Str)
if license == "" {
license = "None"
}
id := ctx.Send(fmt.Sprintf(
"%s: \nDescription: %s\nStar/Fork/Issue: %d/%d/%d\nLanguage: %s\nLicense: %s\nLast pushed: %s\nJump: %s",
repo.Get("full_name").Str,
repo.Get("description").Str,
repo.Get("watchers").Int(),
repo.Get("forks").Int(),
repo.Get("open_issues").Int(),
language,
license,
repo.Get("updated_at").Str,
repo.Get("html_url").Str,
))
if id == 0 {
ctx.Send("ERROR: 可能被风控,发送失败")
}
repo := info.Get("items.0")
// 发送结果
ctx.SendChain(
message.Text(
repo.Get("full_name").Str, "\n",
"Description: ",
repo.Get("description").Str, "\n",
"Star/Fork/Issue: ",
repo.Get("watchers").Int(), "/", repo.Get("forks").Int(), "/", repo.Get("open_issues").Int(), "\n",
"Language: ",
notnull(repo.Get("language").Str, "None"), "\n",
"License: ",
notnull(strings.ToUpper(repo.Get("license.key").Str), "None"), "\n",
"Last pushed: ",
repo.Get("pushed_at").Str, "\n",
"Jump: ",
repo.Get("html_url").Str, "\n",
),
)
})
}
// notnull 如果传入文本为空,则返回默认值
func notnull(text, default_ string) string {
if text == "" {
return default_
}
return text
}
// netGet 返回请求结果
func netGet(dest string, header http.Header) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", dest, nil)
if err != nil {
return nil, err
}
req.Header = header
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if code := resp.StatusCode; code != 200 {
// 如果返回不是200则立刻抛出错误
return nil, errors.New(fmt.Sprintf("code %d", code))
}
return body, nil
}