mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-03-01 02:20:25 +00:00
分离搜图与随机图(随机图需要cgo而搜图不用)
This commit is contained in:
98
picsearcher/utils/ascii2d.go
Normal file
98
picsearcher/utils/ascii2d.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/Yiwen-Chan/ZeroBot-Plugin/api/pixiv"
|
||||
xpath "github.com/antchfx/htmlquery"
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
)
|
||||
|
||||
// Ascii2dSearch Ascii2d 以图搜图
|
||||
// 第一个参数 返回错误
|
||||
// 第二个参数 返回的信息
|
||||
func Ascii2dSearch(pic string) (message.Message, error) {
|
||||
var (
|
||||
api = "https://ascii2d.net/search/uri"
|
||||
)
|
||||
transport := http.Transport{
|
||||
DisableKeepAlives: true,
|
||||
}
|
||||
client := &http.Client{
|
||||
Transport: &transport,
|
||||
}
|
||||
|
||||
// 包装请求参数
|
||||
data := url.Values{}
|
||||
data.Set("uri", pic) // 图片链接
|
||||
fromData := strings.NewReader(data.Encode())
|
||||
|
||||
// 网络请求
|
||||
req, _ := http.NewRequest("POST", api, fromData)
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
req.Header.Set("Accept", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0")
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 色合检索改变到特征检索
|
||||
var bovwUrl = strings.ReplaceAll(resp.Request.URL.String(), "color", "bovw")
|
||||
bovwReq, _ := http.NewRequest("POST", bovwUrl, nil)
|
||||
bovwReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
bovwReq.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36")
|
||||
bovwResp, err := client.Do(bovwReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer bovwResp.Body.Close()
|
||||
// 解析XPATH
|
||||
doc, err := xpath.Parse(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 取出每个返回的结果
|
||||
list := xpath.Find(doc, `//div[@class="row item-box"]`)
|
||||
var link string
|
||||
// 遍历取出第一个返回的PIXIV结果
|
||||
for _, n := range list {
|
||||
linkPath := xpath.Find(n, `//div[2]/div[3]/h6/a[1]`)
|
||||
picPath := xpath.Find(n, `//div[1]/img`)
|
||||
if len(linkPath) != 0 && len(picPath) != 0 {
|
||||
link = xpath.SelectAttr(linkPath[0], "href")
|
||||
if strings.Contains(link, "www.pixiv.net") {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
// 链接取出PIXIV id
|
||||
var index = strings.LastIndex(link, "/")
|
||||
if link == "" || index == -1 {
|
||||
return nil, fmt.Errorf("Ascii2d not found")
|
||||
}
|
||||
var id = pixiv.Str2Int(link[index+1:])
|
||||
if id == 0 {
|
||||
return nil, fmt.Errorf("convert to pid error")
|
||||
}
|
||||
// 根据PID查询插图信息
|
||||
var illust = &pixiv.Illust{}
|
||||
if err := illust.IllustInfo(id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if illust.AgeLimit != "all-age" {
|
||||
return nil, fmt.Errorf("Ascii2d not found")
|
||||
}
|
||||
// 返回插图信息文本
|
||||
return message.Message{
|
||||
message.Text(
|
||||
"[SetuTime] emmm大概是这个?", "\n",
|
||||
"标题:", illust.Title, "\n",
|
||||
"插画ID:", illust.Pid, "\n",
|
||||
"画师:", illust.UserName, "\n",
|
||||
"画师ID:", illust.UserId, "\n",
|
||||
"直链:", "https://pixivel.moe/detail?id=", illust.Pid,
|
||||
),
|
||||
}, nil
|
||||
}
|
||||
77
picsearcher/utils/saucenao.go
Normal file
77
picsearcher/utils/saucenao.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
)
|
||||
|
||||
// SauceNaoSearch SauceNao 以图搜图 需要链接 返回错误和信息
|
||||
func SauceNaoSearch(pic string) (message.Message, error) {
|
||||
var (
|
||||
api = "https://saucenao.com/search.php"
|
||||
apiKey = "2cc2772ca550dbacb4c35731a79d341d1a143cb5"
|
||||
|
||||
minSimilarity = 70.0 // 返回图片结果的最小相似度
|
||||
)
|
||||
|
||||
// 包装请求参数
|
||||
link, _ := url.Parse(api)
|
||||
link.RawQuery = url.Values{
|
||||
"url": []string{pic},
|
||||
"api_key": []string{apiKey},
|
||||
"db": []string{"5"},
|
||||
"numres": []string{"1"},
|
||||
"output_type": []string{"2"},
|
||||
}.Encode()
|
||||
|
||||
// 网络请求
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("GET", link.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
req.Header.Set("Accept", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0")
|
||||
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 resp.StatusCode != http.StatusOK {
|
||||
// 如果返回不是200则立刻抛出错误
|
||||
return nil, fmt.Errorf("SauceNAO not found, code %d", resp.StatusCode)
|
||||
}
|
||||
content := gjson.ParseBytes(body)
|
||||
if status := content.Get("header.status").Int(); status != 0 {
|
||||
// 如果json信息返回status不为0则立刻抛出错误
|
||||
return nil, fmt.Errorf("SauceNAO not found, status %d", status)
|
||||
}
|
||||
if content.Get("results.0.header.similarity").Float() < minSimilarity {
|
||||
return nil, fmt.Errorf("SauceNAO not found")
|
||||
}
|
||||
result := content.Get("results.0")
|
||||
// 正常发送
|
||||
return message.Message{
|
||||
message.Text("[SetuTime] 我有把握是这个!"),
|
||||
message.Image(result.Get("header.thumbnail").Str),
|
||||
message.Text(
|
||||
"\n",
|
||||
"相似度:", result.Get("header.similarity").Str, "\n",
|
||||
"标题:", result.Get("data.title").Str, "\n",
|
||||
"插画ID:", result.Get("data.pixiv_id").Int(), "\n",
|
||||
"画师:", result.Get("data.member_name").Str, "\n",
|
||||
"画师ID:", result.Get("data.member_id").Int(), "\n",
|
||||
"直链:", "https://pixivel.moe/detail?id=", result.Get("data.pixiv_id").Int(),
|
||||
),
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user