1
1
package utils
2
2
3
3
import (
4
- "errors"
5
4
"fmt"
6
5
"net/http"
7
6
"net/url"
8
7
"strings"
9
8
10
9
xpath "github.com/antchfx/htmlquery"
10
+ "github.com/wdvxdr1123/ZeroBot/message"
11
11
)
12
12
13
13
// Ascii2dSearch Ascii2d 以图搜图
14
14
// 第一个参数 返回错误
15
15
// 第二个参数 返回的信息
16
- func Ascii2dSearch (pic string ) (text string , err error ) {
16
+ func Ascii2dSearch (pic string ) (message. Message , error ) {
17
17
var (
18
18
api = "https://ascii2d.net/search/uri"
19
19
)
@@ -35,7 +35,7 @@ func Ascii2dSearch(pic string) (text string, err error) {
35
35
req .Header .Set ("Accept" , "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0" )
36
36
resp , err := client .Do (req )
37
37
if err != nil {
38
- return "" , err
38
+ return nil , err
39
39
}
40
40
// 色合检索改变到特征检索
41
41
var bovwUrl = strings .ReplaceAll (resp .Request .URL .String (), "color" , "bovw" )
@@ -44,13 +44,13 @@ func Ascii2dSearch(pic string) (text string, err error) {
44
44
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" )
45
45
bovwResp , err := client .Do (bovwReq )
46
46
if err != nil {
47
- return "" , err
47
+ return nil , err
48
48
}
49
49
defer bovwResp .Body .Close ()
50
50
// 解析XPATH
51
51
doc , err := xpath .Parse (resp .Body )
52
52
if err != nil {
53
- return "" , err
53
+ return nil , err
54
54
}
55
55
// 取出每个返回的结果
56
56
list := xpath .Find (doc , `//div[@class="row item-box"]` )
@@ -69,32 +69,29 @@ func Ascii2dSearch(pic string) (text string, err error) {
69
69
// 链接取出PIXIV id
70
70
var index = strings .LastIndex (link , "/" )
71
71
if link == "" || index == - 1 {
72
- return "" , errors . New ("Ascii2d not found" )
72
+ return nil , fmt . Errorf ("Ascii2d not found" )
73
73
}
74
74
var id = Str2Int (link [index + 1 :])
75
75
if id == 0 {
76
- return "" , errors . New ("convert to pid error" )
76
+ return nil , fmt . Errorf ("convert to pid error" )
77
77
}
78
78
// 根据PID查询插图信息
79
79
var illust = & Illust {}
80
80
if err := illust .IllustInfo (id ); err != nil {
81
- return "" , err
81
+ return nil , err
82
82
}
83
83
if illust .AgeLimit != "all-age" {
84
- return "" , errors . New ("Ascii2d not found" )
84
+ return nil , fmt . Errorf ("Ascii2d not found" )
85
85
}
86
86
// 返回插图信息文本
87
- return fmt .Sprintf (
88
- `[SetuTime] emmm大概是这个?
89
- 标题:%s
90
- 插画ID:%d
91
- 画师:%s
92
- 画师ID:%d
93
- 直链:https://pixivel.moe/detail?id=%d` ,
94
- illust .Title ,
95
- illust .Pid ,
96
- illust .UserName ,
97
- illust .UserId ,
98
- illust .Pid ,
99
- ), nil
87
+ return message.Message {
88
+ message .Text (
89
+ "[SetuTime] emmm大概是这个?" , "\n " ,
90
+ "标题:" , illust .Title , "\n " ,
91
+ "插画ID:" , illust .Pid , "\n " ,
92
+ "画师:" , illust .UserName , "\n " ,
93
+ "画师ID:" , illust .UserId , "\n " ,
94
+ "直链:" , "https://pixivel.moe/detail?id=" , illust .Pid ,
95
+ ),
96
+ }, nil
100
97
}
0 commit comments