/
setu.go
307 lines (273 loc) · 9.54 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package setu
import (
"fmt"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/ssp97/Ka-ineshizuku-Project/pkg/TypeUtils"
"github.com/ssp97/Ka-ineshizuku-Project/pkg/dbManager"
"github.com/ssp97/Ka-ineshizuku-Project/pkg/fsUtils"
"github.com/ssp97/Ka-ineshizuku-Project/pkg/pixivel"
"github.com/ssp97/Ka-ineshizuku-Project/pkg/zero"
ZeroBot "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/extension/rate"
"github.com/wdvxdr1123/ZeroBot/message"
"io/ioutil"
"math/rand"
"net/http"
"path"
"strings"
"sync"
"time"
)
const PIXIV_IMG_PROXY = "https://pixiv.re"
var db *dbManager.ORM
var pixivapi SetuPixivApi
var limit = rate.NewManager(time.Minute*1, 2)
type Config struct {
Enable bool
Server string
PixivUser string
PixivPassword string
}
func initSetuData(){
data,err := ioutil.ReadFile(path.Join(fsUtils.Getwd(), "static", "sql", "setu.sql"))
if err != nil{
fmt.Println(err)
}
db.DB.Exec(string(data))
data,err = ioutil.ReadFile(path.Join(fsUtils.Getwd(), "static", "sql", "setu_tag.sql"))
if err != nil{
fmt.Println(err)
}
db.DB.Exec(string(data))
}
func Init(c Config) {
var count int64
pixivapi = SetuPixivApi{}
db = dbManager.GetDb(dbManager.DEFAULT_DB_NAME)
db.DB.AutoMigrate(
setu{},
)
db.DB.AutoMigrate(
setuTag{},
)
db.DB.AutoMigrate(
setuTagTranslated{},
)
db.DB.Model(&setu{}).Count(&count)
if count < 10000{
log.Warn("Init setu data")
initSetuData()
log.Warn("Init setu ok")
}
pixivapi.Init()
r := gin.New()
r.Use(gin.Logger(), gin.Recovery())
http.Handle("/app/setu/", r)
r.GET("/app/setu/api/random", func(ctx *gin.Context) {
var data setu
//db.DB.Model(&setu{}).Where("r18 = ?", "0").Order("RANDOM()").First(&data)
//db.DB.Model(&setu{}).Raw("tablesample bernoulli(0.1) ").Where("r18 = ?", "0").Order("RANDOM()").First(&data)
db.DB.Raw("select * from setus tablesample bernoulli(0.1) where r18 = 0 limit 1").First(&data)
data.Url = strings.ReplaceAll(data.Url, `{count}`, fmt.Sprintf("%d",rand.Intn(data.P) ))
fmt.Print(data.Url)
ctx.Redirect(http.StatusFound, fmt.Sprintf("%s%s",PIXIV_IMG_PROXY, data.Url))
})
r.GET("/app/setu/api/randomR18", func(ctx *gin.Context) {
var data setu
//db.DB.Model(&setu{}).Where("r18 = ?", "1").Order(" RANDOM()").First(&data)
//db.DB.Model(&setu{}).Raw("tablesample bernoulli(0.1) ").Where("r18 = ?", "1").Order("RANDOM()").First(&data)
db.DB.Raw("select * from setus tablesample bernoulli(0.1) where r18 = 1 limit 1").First(&data)
data.Url = strings.ReplaceAll(data.Url, `{count}`, fmt.Sprintf("%d",rand.Intn(data.P) ))
fmt.Print(data.Url)
ctx.Redirect(http.StatusFound, fmt.Sprintf("%s%s",PIXIV_IMG_PROXY, data.Url))
})
if c.Enable == false{
return
}
//setuApi.Login()
zero.Default().OnRegex(`^来点(.*)$`).SetBlock(true).SetPriority(20).Handle(func(ctx *ZeroBot.Ctx) {
var tag = ctx.State["regex_matched"].([]string)[1]
getAndSendPic(ctx,tag, 0)
})
zero.Default().OnRegex(`^来张(.*)$`).SetBlock(true).SetPriority(20).Handle(func(ctx *ZeroBot.Ctx) {
var tag = ctx.State["regex_matched"].([]string)[1]
getAndSendPic(ctx,tag, -1)
})
zero.Default().OnRegex(`^来发(.*)$`).SetBlock(true).SetPriority(20).Handle(func(ctx *ZeroBot.Ctx) {
var tag = ctx.State["regex_matched"].([]string)[1]
getAndSendPic(ctx,tag, 1)
})
zero.Default().OnRegex(`^随机色图`).SetBlock(true).SetPriority(20).Handle(func(ctx *ZeroBot.Ctx) {
var data setu
//db.DB.Model(&setu{}).Where("r18 = ?", "0").Order("RANDOM()").First(&data)
db.DB.Raw("select * from setus tablesample bernoulli(0.1) where r18 = 0 limit 1").First(&data)
//j := fmt.Sprintf(`{"app":"com.tencent.miniapp","desc":"","meta":{"Image":"%s"}}`, fmt.Sprintf("%s%s",PIXIV_IMG_PROXY, data.Url))
data.Url = strings.ReplaceAll(data.Url, `{count}`, fmt.Sprintf("%d",rand.Intn(data.P) ))
//ctx.SendChain(zero.Cardimage(fmt.Sprintf("%s%s",PIXIV_IMG_PROXY, data.Url)))
SendPixivBigPic(ctx, data)
})
zero.Default().OnRegex(`^真随机色图`).SetBlock(true).SetPriority(20).Handle(func(ctx *ZeroBot.Ctx) {
url := fmt.Sprintf("https://%s/app/setu/api/random", c.Server)
ctx.SendChain(zero.Share(url,"随机图",url))
})
zero.Default().OnCommand("setu_info").SetBlock(true).SetPriority(20).Handle(func(ctx *ZeroBot.Ctx) {
type SetuCount struct {
SetuR18 int
Count int
}
var (
picCount int64
r18Count int64
pgCount int64
tagCount int64
)
db.DB.Model(&setu{}).Count(&picCount)
db.DB.Model(&setu{}).Where("r18 = ?", 1).Count(&r18Count)
db.DB.Model(&setu{}).Where("r18 = ?", -1).Count(&pgCount)
db.DB.Model(&setuTag{}).Count(&tagCount)
ctx.SendChain(message.Text(fmt.Sprintf("setu:\r\n count:\t%d\r\n isR18:\t%d\r\n isPG: \t%d\r\n tags: \t%d",picCount, r18Count, pgCount, tagCount)))
})
zero.Default().OnRegex("^!setu_pixiv_login:(.*),(.*)$", ZeroBot.SuperUserPermission).FirstPriority().SetBlock(true).Handle(func(ctx *ZeroBot.Ctx) {
token := ctx.State["regex_matched"].([]string)[1]
retoken := ctx.State["regex_matched"].([]string)[2]
err := pixivapi.SetTokenAndLogin(token, retoken)
if err != nil{
ctx.SendChain(message.Text("登录失败:"),message.Text(err))
}else{
ctx.SendChain(message.Text("登录成功!"))
}
})
zero.Default().OnRegex("^!setu_mapping:(.*)->(.*)$", ZeroBot.SuperUserPermission).FirstPriority().SetBlock(true).Handle(func(ctx *ZeroBot.Ctx) {
zh := ctx.State["regex_matched"].([]string)[1]
src := ctx.State["regex_matched"].([]string)[2]
result := db.DB.Model(&setuTagTranslated{}).Create(&setuTagTranslated{
Zh: zh,
Src: src,
})
if result.Error != nil{
ctx.SendChain(message.Text("映射创建失败,因为"),message.Text(result.Error))
return
}
ctx.SendChain(message.Text("创建映射成功"))
return
})
zero.Default().OnRegex("!setu_user:(.*)$", ZeroBot.SuperUserPermission).FirstPriority().SetBlock(true).Handle(func(ctx *ZeroBot.Ctx) {
id := TypeUtils.StrToInt(ctx.State["regex_matched"].([]string)[1])
data, err := pixivel.GetUserInfo(id)
if err!= nil{
ctx.SendChain(message.Text(fmt.Sprintf("发生错误了,因为%s", err)))
return
}
ctx.SendChain(message.Text(*data))
//pixivel.GetUserAllIllust(id)
})
zero.Default().OnRegex("!setu_user_ill:(.*)$", ZeroBot.SuperUserPermission).FirstPriority().SetBlock(true).Handle(func(ctx *ZeroBot.Ctx) {
id := TypeUtils.StrToInt(ctx.State["regex_matched"].([]string)[1])
//_, err := pixivel.GetUserInfo(id)
//if err!= nil{
// ctx.SendChain(message.Text(fmt.Sprintf("发生错误了,因为%s", err)))
// return
//}
////ctx.SendChain(message.Text(*data))
//data := pixivel.GetUserAllIllust(id)
data := pixivapi.GetUserAllPic(uint64(id))
ctx.SendChain(message.Text(data))
})
zero.Default().OnRegex("!setu_add_user_all:(.*)$", ZeroBot.SuperUserPermission).FirstPriority().SetBlock(true).Handle(func(ctx *ZeroBot.Ctx) {
count := 0
id := TypeUtils.StrToInt(ctx.State["regex_matched"].([]string)[1])
_, err := pixivel.GetUserInfo(id)
if err!= nil{
ctx.SendChain(message.Text(fmt.Sprintf("发生错误了,因为%s", err)))
return
}
//ctx.SendChain(message.Text(*data))
data := pixivel.GetUserAllIllust(id)
for i:= range *data{
ill := (*data)[i]
err = addSetu(setu{
Pid: ill.Pid,
P:ill.P,
Title: ill.Title,
UserId: ill.UserId,
UserAccount: ill.UserAccount,
UserName: ill.UserName,
Url: ill.Url,
R18: ill.R18,
Width: ill.Width,
Height: ill.Height,
Tags: strings.Join(ill.Tags, ", "),
}, ill.Tags)
if err == nil{
count++
}
}
ctx.SendChain(message.Text(fmt.Sprintf("增加了%d张图", count)))
})
}
func getAndSendPic(ctx *ZeroBot.Ctx, tag string, isR18 int){
if !limit.Load(ctx.Event.UserID).Acquire() && !ZeroBot.AdminPermission(ctx){
ctx.SendChain(message.Reply(ctx.Event.MessageID),
message.Text("服务受限!接口流量限制!"))
return
}
data, err := searchRandom(tag, isR18)
if err == nil {
SendPixivPic(ctx, data)
return
}
pid, url := loliconSearch(tag, isR18)
if pid > 0{
SendPixivPic(ctx, setu{Pid:pid, Url: url})
}
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(fmt.Sprintf("未找到%s相关的图片", tag)))
return
}
func getSomePic(ctx *ZeroBot.Ctx, tag string, isR18 int, maxCount int){
if !limit.Load(ctx.Event.UserID).Acquire() && !ZeroBot.AdminPermission(ctx){
ctx.SendChain(message.Reply(ctx.Event.MessageID),
message.Text("服务受限!接口流量限制!"))
return
}
//SendSomePic
}
func SendPixivPic(ctx *ZeroBot.Ctx, data setu){
url := fmt.Sprintf("%s/%s",PIXIV_IMG_PROXY, data.Url)
id := ctx.SendChain(message.Reply(ctx.Event.MessageID),zero.ImageUrlMessage(url))
if id == 0 {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("图片发送失败了"))
}else{
// 一分钟后撤回
time.Sleep(60 * time.Second)
ctx.DeleteMessage(id)
}
}
func SendSomePic(ctx *ZeroBot.Ctx, data []setu){
msg := []message.MessageSegment{message.Reply(ctx.Event.MessageID)}
var lock sync.Locker
var wait sync.WaitGroup
for _, s := range data {
wait.Add(1)
go func(s setu) {
url := fmt.Sprintf("%s/%s",PIXIV_IMG_PROXY, s.Url)
m := zero.ImageUrlMessage(url)
lock.Lock()
msg = append(msg, m)
lock.Unlock()
wait.Done()
}(s)
}
wait.Wait()
id := ctx.SendChain(msg...)
if id == 0 {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("图片发送失败了"))
}
}
func SendPixivBigPic(ctx *ZeroBot.Ctx, data setu){
url := fmt.Sprintf("%s/%s",PIXIV_IMG_PROXY, data.Url)
id := ctx.SendChain(zero.Cardimage(zero.ImageUrlMessage(url).Data["file"]))
if id == 0 {
ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text("图片发送失败了"))
}
}