/
lolicon.go
66 lines (61 loc) · 1.42 KB
/
lolicon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
基于 https://api.lolicon.app 随机图片
*/
package lolicon
import (
"github.com/ssp97/Ka-ineshizuku-Project/pkg/zero"
"io/ioutil"
"net/http"
"time"
"github.com/tidwall/gjson"
ZeroBot "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
)
const (
API = "https://api.lolicon.app/setu/v2"
CAP = 10
)
var (
QUEUE = make(chan string, CAP)
)
func init() {
zero.Default().OnFullMatch("来点萝莉").
Handle(func(ctx *ZeroBot.Ctx) {
go func() {
min := func(a, b int) int {
if a < b {
return a
} else {
return b
}
}
for i := 0; i < min(cap(QUEUE)-len(QUEUE), 2); i++ {
resp, err := http.Get(API)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
continue
}
if resp.StatusCode != http.StatusOK {
ctx.SendChain(message.Text("ERROR: code ", resp.StatusCode))
continue
}
data, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
json := gjson.ParseBytes(data)
if e := json.Get("error").Str; e != "" {
ctx.SendChain(message.Text("ERROR: ", e))
continue
}
url := json.Get("data.0.urls.original").Str
//ctx.SendGroupMessage(0, message.Image(url))
QUEUE <- url
}
}()
select {
case <-time.After(time.Second * 10):
ctx.SendChain(message.Text("ERROR: 等待填充,请稍后再试......"))
case url := <-QUEUE:
ctx.SendChain(message.Image(url))
}
})
}