-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetu.go
226 lines (216 loc) · 5.76 KB
/
setu.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"NothinBot/EasyBot"
"encoding/json"
"errors"
"fmt"
"math/rand"
"regexp"
"strconv"
"strings"
"github.com/moxcomic/ihttp"
log "github.com/sirupsen/logrus"
"github.com/ysmood/gson"
)
type setu struct {
R18 int `json:"r18,omitempty"`
Num int `json:"num,omitempty"`
Uid []int `json:"uid,omitempty"`
Keyword string `json:"keyword,omitempty"`
Tag []string `json:"tag,omitempty"`
Size []string `json:"size,omitempty"`
Proxy []string `json:"proxy,omitempty"`
DateAfter int `json:"dateAfter,omitempty"` //ms
DateBefore int `json:"dateBefore,omitempty"` //ms
Desc bool `json:"desc,omitempty"`
ExcludeAI bool `json:"excludeAI,omitempty"`
}
var (
setuEnable = false
useOriginalUrl = false
numberMap = map[string]int{
"两": 2,
"一": 1, "二": 2, "三": 3, "四": 4, "五": 5,
"壹": 1, "贰": 2, "叁": 3, "肆": 4, "伍": 5,
"六": 6, "七": 7, "八": 8, "九": 9, "十": 10,
"陆": 6, "柒": 7, "捌": 8, "玖": 9, "拾": 10,
"十一": 11, "十二": 12, "十三": 13, "十四": 14, "十五": 15,
"拾壹": 11, "拾贰": 12, "拾叁": 13, "拾肆": 14, "拾伍": 15,
"十六": 16, "十七": 17, "十八": 18, "十九": 19, "二十": 20,
"拾陆": 16, "拾柒": 17, "拾捌": 18, "拾玖": 19, "贰拾": 20,
}
)
func initSetu() {
setuEnable = v.GetBool("setu.enable")
}
func checkSetu(ctx *EasyBot.CQMessage) {
//开关控制
matches := ctx.RegFindAllStringSubmatch(regexp.MustCompile(`(开启|启用|关闭|禁用)setu`))
if len(matches) > 0 && ctx.IsPrivateSU() {
switch matches[0][1] {
case "开启", "启用":
setuEnable = true
ctx.SendMsg("setu已启用")
case "关闭", "禁用":
setuEnable = false
ctx.SendMsg("setu已禁用")
}
return
}
if !setuEnable {
return
}
matches = ctx.Unescape().RegFindAllStringSubmatch(
regexp.MustCompile(
`(来(点|一点|几张|几份|.*张|.*份)?([Rr]18)?的?(.*)?的?[色瑟涩铯][图圖])|(([Rr]18)?的?(.*)?的?[色瑟涩铯][图圖]来(点|一点|几张|几份|.*张|.*份)?)`,
),
)
// 一条正则多个同名捕获组只会索引到第一个, 所以下面直接把对应的捕获组全加起来
if len(matches) > 0 && ctx.IsToMe() {
var numOK bool
reqR18 := 0
reqNum := 1
var reqTag []string
r18 := matches[0][3] + matches[0][6]
if r18 != "" {
reqR18 = 1
}
log.Debug("[setu] r18: ", r18)
log.Debug("[setu] reqR18: ", reqR18)
num := matches[0][2] + matches[0][8]
switch num {
case "张", "份":
reqNum = 1
numOK = true
case "点", "一点", "几张", "几份":
reqNum = rand.Intn(4) + 3 // [3,6]
numOK = true
default:
numNoUnit := strings.NewReplacer("张", "", "份", "").Replace(num)
numChar, found := numberMap[numNoUnit]
if !found {
numInt, err := strconv.Atoi(numNoUnit)
if err != nil {
} else {
if numInt >= 1 && numInt <= 20 {
reqNum = numInt
numOK = true
}
}
} else {
if numChar >= 1 && numChar <= 20 {
reqNum = numChar
numOK = true
}
}
}
log.Debug("[setu] num: ", num)
log.Debug("[setu] reqNum: ", reqNum)
tag := matches[0][4] + matches[0][7]
reqTag = regexp.MustCompile(`&`).Split(tag, -1)
log.Debug("[setu] tag: ", tag)
log.Debug("[setu] reqTag: ", reqTag)
if !numOK {
ctx.SendMsgReply("[setu] 请在1-20之间选择数量")
return
} else {
setu := setu{
R18: reqR18,
Num: reqNum,
Tag: reqTag,
Size: []string{"original", "regular"},
}
results, err := setu.get()
if err == nil {
resultsCount := len(results)
content := []string{
fmt.Sprintf(
"r18: %t\nnum: %d\ntag: %v", func() bool {
return reqR18 != 0
}(), reqNum, reqTag,
),
}
content = append(
content, func() (head string) {
head += fmt.Sprint("在 api.lolicon.app/setu/v2 根据以上条件搜索到了", resultsCount, "张setu")
if resultsCount < reqNum {
head += "\nあれれ?似乎没有那么多符合这个条件的setu呢"
}
return
}(),
)
ctx.SendForwardMsg(
EasyBot.FastNewForwardMsg(
"NothinBot", ctx.UserID, 0, 0, append(content, results...)...,
),
)
} else {
ctx.SendMsg(err.Error())
}
}
}
}
func (param setu) get() (results []string, err error) {
postData, _ := json.Marshal(param)
log.Debug("[setu] 请求setu: ", param)
setu, err := ihttp.New().WithUrl("https://api.lolicon.app/setu/v2").
WithHeader("Content-Type", "application/json").
WithBody(postData).Post().ToGson()
if err != nil {
err = errors.New("[setu] getSetu().ihttp请求错误: " + err.Error())
log.Error(err)
} else if setu.Get("error").Str() != "" && setu.Get("error").Str() != "<nil>" {
err = errors.New("[setu] getSetu().ihttp响应错误: " + setu.Get("error").Str())
log.Error(err)
}
data, _ := setu.Gets("data")
if len(data.Arr()) == 0 {
return []string{}, errors.New("[NothingBot] [setu] 没有符合该条件的色图")
}
for _, g := range data.Arr() {
urlOriginal := g.Get("urls.original").Str()
urlRegular := g.Get("urls.regular").Str()
url := func() (url string) {
if useOriginalUrl {
url = urlOriginal
} else {
url = urlRegular
}
return
}()
title := g.Get("title").Str()
author := g.Get("author").Str()
uid := g.Get("uid").Int()
tag := func(tags []gson.JSON) (tag string) {
tagsLen := len(tags)
for i := 0; i < tagsLen; i++ {
tag += tags[i].Str()
if i+1 < tagsLen {
tag += ","
}
}
return
}(g.Get("tags").Arr())
pid := g.Get("pid").Int()
p := g.Get("p").Int()
results = append(
results, fmt.Sprintf(
`[CQ:image,file=%s]
%s (P%d)
%s
作者:%s
pixiv.net/u/%d
pixiv.net/i/%d
原图:%s`,
url,
title, p,
tag,
author,
uid,
pid,
urlOriginal,
),
)
}
return
}