mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-03-01 18:40:26 +00:00
分离搜图与随机图(随机图需要cgo而搜图不用)
This commit is contained in:
@@ -1,97 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
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 = Str2Int(link[index+1:])
|
||||
if id == 0 {
|
||||
return nil, fmt.Errorf("convert to pid error")
|
||||
}
|
||||
// 根据PID查询插图信息
|
||||
var illust = &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
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// urlCache 缓存并返回缓存路径
|
||||
func (this *Illust) PixivPicDown(path string) (savePath string, err error) {
|
||||
url := this.ImageUrls
|
||||
pid := this.Pid
|
||||
url = strings.ReplaceAll(url, "img-original", "img-master")
|
||||
url = strings.ReplaceAll(url, "_p0", "_p0_master1200")
|
||||
url = strings.ReplaceAll(url, ".png", ".jpg")
|
||||
// 文件名为url的hash值
|
||||
savePath = path + Int2Str(pid) + ".jpg"
|
||||
// 文件存在或文件大小大于10kb
|
||||
if PathExists(savePath) && FileSize(savePath) > 10240 {
|
||||
return savePath, nil
|
||||
}
|
||||
|
||||
// 模拟QQ客户端请求
|
||||
client := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
DisableKeepAlives: true,
|
||||
// 绕过sni审查
|
||||
TLSClientConfig: &tls.Config{
|
||||
ServerName: "-",
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
// 更改dns
|
||||
Dial: func(network, addr string) (net.Conn, error) {
|
||||
return net.Dial("tcp", "210.140.92.142:443")
|
||||
},
|
||||
},
|
||||
}
|
||||
reqest, _ := http.NewRequest("GET", url, nil)
|
||||
reqest.Header.Set("Referer", "https://www.pixiv.net/")
|
||||
reqest.Header.Set("Host", "i.pximg.net")
|
||||
reqest.Header.Set("User-Agent", "QQ/8.2.0.1296 CFNetwork/1126")
|
||||
|
||||
resp, err := client.Do(reqest)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
return "", errors.New(fmt.Sprintf("Download failed, code %d", resp.StatusCode))
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
// 写入文件
|
||||
data, _ := ioutil.ReadAll(resp.Body)
|
||||
f, _ := os.OpenFile(savePath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
|
||||
defer f.Close()
|
||||
f.Write(data)
|
||||
|
||||
return savePath, err
|
||||
}
|
||||
|
||||
// PicHash 返回图片的 md5 值
|
||||
func PicHash(path string) string {
|
||||
data, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return strings.ToUpper(fmt.Sprintf("%x", md5.Sum(data)))
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package utils
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/Yiwen-Chan/ZeroBot-Plugin/api/pixiv"
|
||||
)
|
||||
|
||||
// PoolsCache 图片缓冲池
|
||||
@@ -11,7 +13,7 @@ type PoolsCache struct {
|
||||
Max int
|
||||
Path string
|
||||
Group int64
|
||||
Pool map[string][]*Illust
|
||||
Pool map[string][]*pixiv.Illust
|
||||
}
|
||||
|
||||
// NewPoolsCache 返回一个缓冲池对象
|
||||
@@ -20,7 +22,7 @@ func NewPoolsCache() *PoolsCache {
|
||||
Max: 10,
|
||||
Path: "./data/SetuTime/cache/",
|
||||
Group: 1048452984,
|
||||
Pool: map[string][]*Illust{},
|
||||
Pool: map[string][]*pixiv.Illust{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +37,7 @@ func (p *PoolsCache) IsFull(type_ string) bool {
|
||||
}
|
||||
|
||||
// Push 向缓冲池插入一张图片,返回错误
|
||||
func (p *PoolsCache) Push(type_ string, illust *Illust) (err error) {
|
||||
func (p *PoolsCache) Push(type_ string, illust *pixiv.Illust) (err error) {
|
||||
p.Lock.Lock()
|
||||
defer p.Lock.Unlock()
|
||||
p.Pool[type_] = append(p.Pool[type_], illust)
|
||||
@@ -43,7 +45,7 @@ func (p *PoolsCache) Push(type_ string, illust *Illust) (err error) {
|
||||
}
|
||||
|
||||
// Push 在缓冲池拿出一张图片,返回错误
|
||||
func (p *PoolsCache) Pop(type_ string) (illust *Illust) {
|
||||
func (p *PoolsCache) Pop(type_ string) (illust *pixiv.Illust) {
|
||||
p.Lock.Lock()
|
||||
defer p.Lock.Unlock()
|
||||
if p.Size(type_) == 0 {
|
||||
@@ -69,46 +71,3 @@ func (p *PoolsCache) GetOnePic(type_ string, form string) string {
|
||||
return illust.NormalPic(file)
|
||||
}
|
||||
}
|
||||
|
||||
// BigPic 返回一张XML大图CQ码
|
||||
func (i *Illust) BigPic(file string) string {
|
||||
var hash = PicHash(file)
|
||||
return fmt.Sprintf(`[CQ:xml,data=<?xml version='1.0'
|
||||
encoding='UTF-8' standalone='yes' ?><msg serviceID="5"
|
||||
templateID="12345" action="" brief="不够涩!"
|
||||
sourceMsgId="0" url="" flag="0" adverSign="0" multiMsgFlag="0">
|
||||
<item layout="0" advertiser_id="0" aid="0"><image uuid="%s.jpg" md5="%s"
|
||||
GroupFiledid="2235033681" filesize="81322" local_path="%s.jpg"
|
||||
minWidth="200" minHeight="200" maxWidth="500" maxHeight="1000" />
|
||||
</item><source name="%s⭐(id:%d author:%s)" icon=""
|
||||
action="" appid="-1" /></msg>]`,
|
||||
hash,
|
||||
hash,
|
||||
hash,
|
||||
i.Title,
|
||||
i.Pid,
|
||||
i.UserName,
|
||||
)
|
||||
}
|
||||
|
||||
// NormalPic 返回一张普通图CQ码
|
||||
func (i *Illust) NormalPic(file string) string {
|
||||
return fmt.Sprintf(`[CQ:image,file=file:///%s]`, file)
|
||||
}
|
||||
|
||||
// DetailPic 返回一张带详细信息的图片CQ码
|
||||
func (i *Illust) DetailPic(file string) string {
|
||||
return fmt.Sprintf(`[SetuTime] %s
|
||||
标题:%s
|
||||
插画ID:%d
|
||||
画师:%s
|
||||
画师ID:%d
|
||||
直链:https://pixivel.moe/detail?id=%d`,
|
||||
i.NormalPic(file),
|
||||
i.Title,
|
||||
i.Pid,
|
||||
i.UserName,
|
||||
i.UserId,
|
||||
i.Pid,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
// Illust 插画信息
|
||||
type Illust struct {
|
||||
Pid int64 `db:"pid"`
|
||||
Title string `db:"title"`
|
||||
Caption string `db:"caption"`
|
||||
Tags string `db:"tags"`
|
||||
ImageUrls string `db:"image_urls"`
|
||||
AgeLimit string `db:"age_limit"`
|
||||
CreatedTime string `db:"created_time"`
|
||||
UserId int64 `db:"user_id"`
|
||||
UserName string `db:"user_name"`
|
||||
}
|
||||
|
||||
// IllustInfo 根据p站插画id返回插画信息Illust
|
||||
func (this *Illust) IllustInfo(id int64) (err error) {
|
||||
api := fmt.Sprintf("https://pixiv.net/ajax/illust/%d", id)
|
||||
transport := http.Transport{
|
||||
DisableKeepAlives: true,
|
||||
// 绕过sni审查
|
||||
TLSClientConfig: &tls.Config{
|
||||
ServerName: "-",
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
// 更改dns
|
||||
Dial: func(network, addr string) (net.Conn, error) {
|
||||
return net.Dial("tcp", "210.140.131.223:443")
|
||||
},
|
||||
}
|
||||
client := &http.Client{
|
||||
Transport: &transport,
|
||||
}
|
||||
|
||||
// 网络请求
|
||||
req, err := http.NewRequest("GET", api, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Host", "pixiv.net")
|
||||
req.Header.Set("Referer", "pixiv.net")
|
||||
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 err
|
||||
}
|
||||
if code := resp.StatusCode; code != 200 {
|
||||
return errors.New(fmt.Sprintf("Search illust's info failed, status %d", code))
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
json := gjson.ParseBytes(body).Get("body")
|
||||
|
||||
// 如果有"R-18"tag则判断为R-18(暂时)
|
||||
var ageLimit = "all-age"
|
||||
for _, tag := range json.Get("tags.tags.#.tag").Array() {
|
||||
if tag.Str == "R-18" {
|
||||
ageLimit = "r18"
|
||||
break
|
||||
}
|
||||
}
|
||||
// 解决json返回带html格式
|
||||
var caption = strings.ReplaceAll(json.Get("illustComment").Str, "<br />", "\n")
|
||||
if index := strings.Index(caption, "<"); index != -1 {
|
||||
caption = caption[:index]
|
||||
}
|
||||
// 解析返回插画信息
|
||||
this.Pid = json.Get("illustId").Int()
|
||||
this.Title = json.Get("illustTitle").Str
|
||||
this.Caption = caption
|
||||
this.Tags = fmt.Sprintln(json.Get("tags.tags.#.tag").Array())
|
||||
this.ImageUrls = json.Get("urls.original").Str
|
||||
this.AgeLimit = ageLimit
|
||||
this.CreatedTime = json.Get("createDate").Str
|
||||
this.UserId = json.Get("userId").Int()
|
||||
this.UserName = json.Get("userName").Str
|
||||
return nil
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
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
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Str2Int string --> int64
|
||||
func Str2Int(str string) int64 {
|
||||
val, _ := strconv.Atoi(str)
|
||||
return int64(val)
|
||||
}
|
||||
|
||||
// Int2Str int64 --> string
|
||||
func Int2Str(val int64) string {
|
||||
str := strconv.FormatInt(val, 10)
|
||||
return str
|
||||
}
|
||||
|
||||
// PathExecute 返回当前运行目录
|
||||
func PathExecute() string {
|
||||
dir, err := os.Getwd()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return dir + "/"
|
||||
}
|
||||
|
||||
// CreatePath 生成路径或文件所对应的目录
|
||||
func CreatePath(path string) {
|
||||
length := len(path)
|
||||
switch {
|
||||
case path[length:] != "/":
|
||||
path = path[:strings.LastIndex(path, "/")]
|
||||
case path[length:] != "\\":
|
||||
path = path[:strings.LastIndex(path, "\\")]
|
||||
default:
|
||||
//
|
||||
}
|
||||
if !PathExists(path) {
|
||||
err := os.MkdirAll(path, 0755)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// PathExists 判断路径或文件是否存在
|
||||
func PathExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return err == nil || os.IsExist(err)
|
||||
}
|
||||
|
||||
// FileSize 获取文件大小
|
||||
func FileSize(file string) int64 {
|
||||
if fi, err := os.Stat(file); err == nil {
|
||||
return fi.Size()
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// Min 返回两数最小值
|
||||
func Min(a, b int) int {
|
||||
switch {
|
||||
default:
|
||||
return a
|
||||
case a > b:
|
||||
return b
|
||||
case a < b:
|
||||
return a
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user