Skip to content

Commit

Permalink
🐛 fix: image finder
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Feb 14, 2022
1 parent 37033fe commit 8660d8d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 84 deletions.
6 changes: 3 additions & 3 deletions plugin_fortune/fortune.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ func init() {
}
m.SetFile(file.BOTPATH + "/" + cachefile)
hassent, err := m.Push(ctxext.Send(ctx), ctxext.GetMessage(ctx))
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
if hassent {
return
}
if hassent {
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
}
Expand Down
132 changes: 51 additions & 81 deletions plugin_image_finder/keyword.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,33 @@ package imagefinder

import (
"encoding/json"
"fmt"
"math/rand"
"net/http"
"strings"
"time"

"github.com/FloatTech/AnimeAPI/pixiv"
control "github.com/FloatTech/zbputils/control"
"github.com/FloatTech/zbputils/ctxext"
"github.com/FloatTech/zbputils/file"
"github.com/FloatTech/zbputils/img/pool"
"github.com/FloatTech/zbputils/web"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"

"github.com/FloatTech/zbputils/control/order"
)

type resultjson 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"`
Data struct {
Illusts []struct {
ID int64 `json:"id"`
Title string `json:"title"`
AltTitle string `json:"altTitle"`
Description string `json:"description"`
Sanity int `json:"sanity"`
} `json:"illusts"`
} `json:"data"`
Error bool `json:"error"`
Message string `json:"message"`
}

func init() {
Expand All @@ -72,46 +40,48 @@ func init() {
}).OnRegex(`^来张\s?(.*)$`, zero.AdminPermission).SetBlock(true).
Handle(func(ctx *zero.Ctx) {
keyword := ctx.State["regex_matched"].([]string)[1]
soutujson := soutuapi(keyword)
pom1 := "https://i.pixiv.re"
rannum := randintn(len(soutujson.Illusts))
pom2 := soutujson.Illusts[rannum].ImageUrls.Medium[19:]
u := pom1 + pom2
m, hassent, err := pool.NewImage(ctxext.Send(ctx), ctxext.GetMessage(ctx), u[strings.LastIndex(u, "/")+1:], u)
if err == nil && !hassent {
ctx.SendChain(message.Image(m.String()))
soutujson, err := soutuapi(keyword)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
rannum := rand.Intn(len(soutujson.Data.Illusts))
illust, err := pixiv.Works(soutujson.Data.Illusts[rannum].ID)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
u := illust.ImageUrls[0]
n := u[strings.LastIndex(u, "/")+1 : len(u)-4]
m, err := pool.GetImage(n)
if err != nil {
// 下载图片
f := ""
if f, err = illust.DownloadToCache(0, n); err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
m.SetFile(file.BOTPATH + "/" + f)
hassent, err := m.Push(ctxext.Send(ctx), ctxext.GetMessage(ctx))
if hassent {
return
}
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
return
}
}
ctx.SendChain(message.Image(m.String()))
})
}

// soutuapi 请求api
func soutuapi(keyword string) *resultjson {
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, */*")
req.Header.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36")
res, err := client.Do(req)
func soutuapi(keyword string) (r resultjson, err error) {
url := "https://api.pixivel.moe/v2/pixiv/illust/search/" + keyword + "?page=0"
data, err := web.ReqWith(url, "GET", "https://pixivel.moe/", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36")
if err != nil {
fmt.Println(err)
}
defer res.Body.Close()

result := &resultjson{}
if err := json.NewDecoder(res.Body).Decode(result); err != nil {
panic(err)
return
}
return result
}

// randintn 从json里的30条数据中随机获取一条返回
func randintn(n int) int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(n)
err = json.Unmarshal(data, &r)
return
}

0 comments on commit 8660d8d

Please sign in to comment.