From f534b52074bf68737106c43614d4dbcfe6447537 Mon Sep 17 00:00:00 2001 From: Yiwen-Chan Date: Mon, 29 Mar 2021 00:19:58 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=B7=BB=E5=8A=A0=20GitHub=20?= =?UTF-8?q?=E4=BB=93=E5=BA=93=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- github/repo_searcher.go | 72 +++++++++++++++++++++++++++++++++++++++++ main.go | 2 ++ 2 files changed, 74 insertions(+) create mode 100644 github/repo_searcher.go diff --git a/github/repo_searcher.go b/github/repo_searcher.go new file mode 100644 index 00000000..aa61bb59 --- /dev/null +++ b/github/repo_searcher.go @@ -0,0 +1,72 @@ +package github + +import ( + "fmt" + "io/ioutil" + "net/http" + "strings" + + "github.com/tidwall/gjson" + zero "github.com/wdvxdr1123/ZeroBot" +) + +func init() { // 插件主体 + zero.OnRegex(`>G\s(.*)`).SetBlock(true).SetPriority(0). + Handle(func(ctx *zero.Ctx) { + link := "https://api.github.com/search/repositories?q=" + ctx.State["regex_matched"].([]string)[1] + client := &http.Client{} + + req, err := http.NewRequest("GET", link, nil) + if err != nil { + ctx.Send(fmt.Sprintf("ERROR: %v", err)) + 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: 可能被风控,发送失败") + } + }) +} diff --git a/main.go b/main.go index dc6431bb..8b495090 100644 --- a/main.go +++ b/main.go @@ -8,8 +8,10 @@ import ( zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/driver" + _ "bot/github" _ "bot/manager" _ "bot/music" + _ "bot/run" setutime "bot/setutime" )