Skip to content

Commit d092e06

Browse files
committed
🎨 改进代码结构
1 parent a960b7c commit d092e06

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

setutime/utils/ascii2d.go

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package utils
22

33
import (
4-
"errors"
54
"fmt"
65
"net/http"
76
"net/url"
87
"strings"
98

109
xpath "github.com/antchfx/htmlquery"
10+
"github.com/wdvxdr1123/ZeroBot/message"
1111
)
1212

1313
// Ascii2dSearch Ascii2d 以图搜图
1414
// 第一个参数 返回错误
1515
// 第二个参数 返回的信息
16-
func Ascii2dSearch(pic string) (text string, err error) {
16+
func Ascii2dSearch(pic string) (message.Message, error) {
1717
var (
1818
api = "https://ascii2d.net/search/uri"
1919
)
@@ -35,7 +35,7 @@ func Ascii2dSearch(pic string) (text string, err error) {
3535
req.Header.Set("Accept", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0")
3636
resp, err := client.Do(req)
3737
if err != nil {
38-
return "", err
38+
return nil, err
3939
}
4040
// 色合检索改变到特征检索
4141
var bovwUrl = strings.ReplaceAll(resp.Request.URL.String(), "color", "bovw")
@@ -44,13 +44,13 @@ func Ascii2dSearch(pic string) (text string, err error) {
4444
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")
4545
bovwResp, err := client.Do(bovwReq)
4646
if err != nil {
47-
return "", err
47+
return nil, err
4848
}
4949
defer bovwResp.Body.Close()
5050
// 解析XPATH
5151
doc, err := xpath.Parse(resp.Body)
5252
if err != nil {
53-
return "", err
53+
return nil, err
5454
}
5555
// 取出每个返回的结果
5656
list := xpath.Find(doc, `//div[@class="row item-box"]`)
@@ -69,32 +69,29 @@ func Ascii2dSearch(pic string) (text string, err error) {
6969
// 链接取出PIXIV id
7070
var index = strings.LastIndex(link, "/")
7171
if link == "" || index == -1 {
72-
return "", errors.New("Ascii2d not found")
72+
return nil, fmt.Errorf("Ascii2d not found")
7373
}
7474
var id = Str2Int(link[index+1:])
7575
if id == 0 {
76-
return "", errors.New("convert to pid error")
76+
return nil, fmt.Errorf("convert to pid error")
7777
}
7878
// 根据PID查询插图信息
7979
var illust = &Illust{}
8080
if err := illust.IllustInfo(id); err != nil {
81-
return "", err
81+
return nil, err
8282
}
8383
if illust.AgeLimit != "all-age" {
84-
return "", errors.New("Ascii2d not found")
84+
return nil, fmt.Errorf("Ascii2d not found")
8585
}
8686
// 返回插图信息文本
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
10097
}

setutime/utils/download.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ func (this *Illust) PixivPicDown(path string) (savePath string, err error) {
5050
if err != nil {
5151
return "", err
5252
}
53-
fmt.Println(resp.StatusCode)
54-
if code := resp.StatusCode; code != 200 {
55-
return "", errors.New(fmt.Sprintf("Download failed, code %d", code))
53+
if resp.StatusCode != 200 {
54+
return "", errors.New(fmt.Sprintf("Download failed, code %d", resp.StatusCode))
5655
}
5756
defer resp.Body.Close()
5857
// 写入文件

0 commit comments

Comments
 (0)