@@ -3,6 +3,7 @@ package lolicon
3
3
4
4
import (
5
5
"encoding/base64"
6
+ "errors"
6
7
"net/url"
7
8
"strings"
8
9
"time"
@@ -17,7 +18,7 @@ import (
17
18
ctrl "github.com/FloatTech/zbpctrl"
18
19
"github.com/FloatTech/zbputils/control"
19
20
"github.com/FloatTech/zbputils/ctxext"
20
- "github.com/FloatTech/zbputils/img/pool"
21
+ imagepool "github.com/FloatTech/zbputils/img/pool"
21
22
)
22
23
23
24
const (
@@ -26,8 +27,8 @@ const (
26
27
)
27
28
28
29
var (
29
- queue = make (chan string , capacity )
30
- custapi = ""
30
+ queue = make (chan string , capacity )
31
+ customapi = ""
31
32
)
32
33
33
34
func init () {
@@ -40,45 +41,44 @@ func init() {
40
41
}).ApplySingle (ctxext .DefaultSingle )
41
42
en .OnPrefix ("随机图片" ).Limit (ctxext .LimitByUser ).SetBlock (true ).
42
43
Handle (func (ctx * zero.Ctx ) {
44
+ if imgtype := strings .TrimSpace (ctx .State ["args" ].(string )); imgtype != "" {
45
+ imageurl , err := getimgurl (api + "?tag=" + url .QueryEscape (imgtype ))
46
+ if err != nil {
47
+ ctx .SendChain (message .Text ("ERROR: " , err ))
48
+ return
49
+ }
50
+ if id := ctx .Send (message.Message {ctxext .FakeSenderForwardNode (ctx , message .Image (imageurl ))}).ID (); id == 0 {
51
+ ctx .SendChain (message .Text ("ERROR: 可能被风控了" ))
52
+ }
53
+ return
54
+ }
43
55
go func () {
44
56
for i := 0 ; i < math .Min (cap (queue )- len (queue ), 2 ); i ++ {
45
- if custapi != "" {
46
- data , err := web .GetData (custapi )
57
+ if customapi != "" {
58
+ data , err := web .GetData (customapi )
47
59
if err != nil {
48
60
ctx .SendChain (message .Text ("ERROR: " , err ))
49
61
continue
50
62
}
51
63
queue <- "base64://" + base64 .StdEncoding .EncodeToString (data )
52
64
continue
53
65
}
54
- rapi := api
55
- args := strings .TrimSpace (ctx .State ["args" ].(string ))
56
- if args != "" {
57
- rapi += "?tag=" + url .QueryEscape (args )
58
- }
59
- data , err := web .GetData (rapi )
66
+ imageurl , err := getimgurl (api )
60
67
if err != nil {
61
68
ctx .SendChain (message .Text ("ERROR: " , err ))
62
69
continue
63
70
}
64
- json := gjson .ParseBytes (data )
65
- if e := json .Get ("error" ).Str ; e != "" {
66
- ctx .SendChain (message .Text ("ERROR: " , e ))
67
- continue
68
- }
69
- url := json .Get ("data.0.urls.original" ).Str
70
- url = strings .ReplaceAll (url , "i.pixiv.cat" , "i.pixiv.re" )
71
- name := url [strings .LastIndex (url , "/" )+ 1 : len (url )- 4 ]
72
- m , err := pool .GetImage (name )
71
+ name := imageurl [strings .LastIndex (imageurl , "/" )+ 1 : len (imageurl )- 4 ]
72
+ m , err := imagepool .GetImage (name )
73
73
if err != nil {
74
- m .SetFile (url )
75
- _ , err = m .Push (ctxext .SendToSelf (ctx ), ctxext .GetMessage (ctx ))
74
+ m .SetFile (imageurl )
75
+ _ , _ = m .Push (ctxext .SendToSelf (ctx ), ctxext .GetMessage (ctx ))
76
76
process .SleepAbout1sTo2s ()
77
77
}
78
78
if err == nil {
79
79
queue <- m .String ()
80
80
} else {
81
- queue <- url
81
+ queue <- imageurl
82
82
}
83
83
}
84
84
}()
@@ -98,6 +98,22 @@ func init() {
98
98
Handle (func (ctx * zero.Ctx ) {
99
99
u := strings .TrimSpace (ctx .State ["args" ].(string ))
100
100
ctx .SendChain (message .Text ("成功设置随机图片地址为" , u ))
101
- custapi = u
101
+ customapi = u
102
102
})
103
103
}
104
+
105
+ func getimgurl (url string ) (string , error ) {
106
+ data , err := web .GetData (url )
107
+ if err != nil {
108
+ return "" , err
109
+ }
110
+ json := gjson .ParseBytes (data )
111
+ if e := json .Get ("error" ).Str ; e != "" {
112
+ return "" , errors .New (e )
113
+ }
114
+ var imageurl string
115
+ if imageurl = json .Get ("data.0.urls.original" ).Str ; imageurl == "" {
116
+ return "" , errors .New ("未找到相关内容, 换个tag试试吧" )
117
+ }
118
+ return strings .ReplaceAll (imageurl , "i.pixiv.cat" , "i.pixiv.re" ), nil
119
+ }
0 commit comments