From 4f738fda9ada20ed9c0fd558eb10e1d152b263a6 Mon Sep 17 00:00:00 2001 From: Suika <290760339@qq.com> Date: Mon, 28 Jun 2021 18:05:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=B6=A9=E5=9B=BE=E5=85=B3?= =?UTF-8?q?=E9=94=AE=E5=AD=97=E6=90=9C=E7=B4=A2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 搜图api 8太行,以后找到更好的再换 --- setutime/keyword.go | 107 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 setutime/keyword.go diff --git a/setutime/keyword.go b/setutime/keyword.go new file mode 100644 index 0000000000..1744f41d61 --- /dev/null +++ b/setutime/keyword.go @@ -0,0 +1,107 @@ +package setutime + +import ( + "encoding/json" + "fmt" + zero "github.com/wdvxdr1123/ZeroBot" + "github.com/wdvxdr1123/ZeroBot/message" + "math/rand" + "net/http" + "time" +) + + +type AutoGenerated struct { + Illusts []struct { + ID int `json:"id"` + Title string `json:"title"` + Type string `json:"type"` + ImageUrls struct { + SquareMedium string `json:"square_medium"` + Medium string `json:"medium"` + Large string `json:"large"` + } `json:"image_urls"` + Caption string `json:"caption"` + Restrict int `json:"restrict"` + User struct { + ID int `json:"id"` + Name string `json:"name"` + Account string `json:"account"` + ProfileImageUrls struct { + Medium string `json:"medium"` + } `json:"profile_image_urls"` + IsFollowed bool `json:"is_followed"` + } `json:"user"` + Tags []struct { + Name string `json:"name"` + TranslatedName interface{} `json:"translated_name"` + } `json:"tags"` + Tools []interface{} `json:"tools"` + PageCount int `json:"page_count"` + Width int `json:"width"` + Height int `json:"height"` + SanityLevel int `json:"sanity_level"` + XRestrict int `json:"x_restrict"` + Series interface{} `json:"series"` + MetaSinglePage struct { + OriginalImageURL string `json:"original_image_url"` + } `json:"meta_single_page,omitempty"` + MetaPages []interface{} `json:"meta_pages"` + TotalView int `json:"total_view"` + TotalBookmarks int `json:"total_bookmarks"` + IsBookmarked bool `json:"is_bookmarked"` + Visible bool `json:"visible"` + IsMuted bool `json:"is_muted"` + } `json:"illusts"` + NextURL string `json:"next_url"` + SearchSpanLimit int `json:"search_span_limit"` +} + + +func init() { + zero.OnRegex(`^来张 (.*)$`, zero.AdminPermission). + Handle(func(ctx *zero.Ctx) { + keyword := ctx.State["regex_matched"].([]string)[1] + soutujson := soutuapi(keyword) + pom1 := "https://i.pixiv.cat" + rannum := Suiji() + pom2 := soutujson.Illusts[rannum].ImageUrls.Large[19:] + ctx.SendChain(message.Image(pom1 + pom2)) + }) + +} + + +// 请求api +func soutuapi(keyword string) *AutoGenerated { + + url := "https://api.pixivel.moe/pixiv?type=search&page=0&mode=partial_match_for_tags&word=" + keyword + method := "GET" + + client := &http.Client { + } + req, err := http.NewRequest(method, url, nil) + + if err != nil { + fmt.Println(err) + } + req.Header.Add("accept", "application/json, text/plain, */*") + + res, err := client.Do(req) + if err != nil { + fmt.Println(err) + } + defer res.Body.Close() + + result := &AutoGenerated{} + if err := json.NewDecoder(res.Body).Decode(result); err != nil { + panic(err) + } + return result +} + +//从json里的30条数据中随机获取一条返回 +func Suiji() int { + rand.Seed(time.Now().UnixNano()) + return rand.Intn(30) +}