-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
307 lines (271 loc) · 7.54 KB
/
main.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 main
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"time"
"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus"
)
var (
HTTPProxy *url.URL
Payload []Danbooru
DiscordWebHookURL string
FistRunning *bool
DisableMale bool
RemoveDuplicate bool
LewdsPic []string
Tags []string
DisableOthers []string
)
const (
EndPoint = "https://147.135.4.93/posts.json?tags=order:change%20"
)
func init() {
tmp := true
FistRunning = &tmp
Prxy := os.Getenv("PROXY")
if Prxy != "" {
if strings.HasPrefix(Prxy, "http") {
urlproxy, err := url.Parse(Prxy)
if err != nil {
log.Error(err)
}
HTTPProxy = urlproxy
} else {
log.Warning("Invalid http proxy,format http://<addr>:<port>")
}
}
Web := os.Getenv("DISCORD")
if Web != "" {
DiscordWebHookURL = Web
} else {
log.Fatal("DISCORD WebHookURL not found")
}
tgs := os.Getenv("TAGS")
if tgs != "" {
Tags = strings.Split(tgs, ",")
} else {
log.Fatal("Tags not found")
}
Male := os.Getenv("MALE")
if Male != "" {
if strings.ToLower(Male) == "enable" {
DisableMale = false
} else {
DisableMale = true
}
} else {
DisableMale = true
}
dpl := os.Getenv("DUPLICATE")
if dpl != "" {
if strings.ToLower(dpl) == "enable" || dpl == "1" {
RemoveDuplicate = false
} else {
RemoveDuplicate = true
}
} else {
RemoveDuplicate = true
}
opt := os.Getenv("DISABLETAGS")
if opt != "" {
DisableOthers = strings.Split(opt, ",")
}
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
}
func main() {
log.Info("Starting RSS")
StartCheck()
c := cron.New()
c.Start()
c.AddFunc("@every 0h2m0s", StartCheck)
shutdown := make(chan int)
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigChan
log.Info("Shutting down...")
shutdown <- 1
}()
<-shutdown
}
func StartCheck() {
Counter := 0
for _, v := range Tags {
log.Info("Start checking lewd ", v)
Data, err := Curl(EndPoint + v + "&limit=20")
if err != nil {
log.Error(err)
break
}
err = json.Unmarshal(Data, &Payload)
if err != nil {
log.Error(err)
}
if *FistRunning {
Counter++
log.Info("First Running")
for _, v := range Payload {
if v.CheckRSS() {
v.AddNewLewd()
}
}
if Counter == len(Tags) {
tmp := false
FistRunning = &tmp
}
} else {
for _, v := range Payload {
if v.CheckNew() && v.CheckRSS() {
log.Info("New Pic ", v.ID)
v.AddNewLewd()
fixURL := strings.Replace(v.FileURL, "147.135.4.93", "danbooru.donmai.us", -1)
Pic, err := json.Marshal(map[string]interface{}{
"content": fixURL,
})
if err != nil {
log.Error(err)
}
req, err := http.NewRequest("POST", DiscordWebHookURL, bytes.NewReader(Pic))
if err != nil {
log.Error(err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Error(err)
}
defer resp.Body.Close()
}
}
}
}
}
func (Data Danbooru) CheckRSS() bool {
safebutcrott, _ := regexp.MatchString("(swimsuits|lingerie|pantyshot)", Data.TagString)
male, _ := regexp.MatchString("(male_focus|yaoi|mature_male|muscular_male)", Data.TagString)
if DisableMale && male {
return false
}
if RemoveDuplicate && Data.HasChildren {
return false
}
if DisableOthers != nil {
others, _ := regexp.MatchString(strings.Join(DisableOthers, "|"), Data.TagString)
if others {
return false
}
}
if Data.Rating == "e" || Data.Rating == "q" || safebutcrott {
return true
}
return false
}
func (Data Danbooru) CheckNew() bool {
for _, v := range LewdsPic {
if Data.FileURL == v {
return false
}
}
return true
}
func (Data Danbooru) AddNewLewd() {
LewdsPic = append(LewdsPic, Data.FileURL)
}
func Curl(URL string) ([]byte, error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := http.Client{Transport: tr}
if HTTPProxy != nil {
client = http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(HTTPProxy),
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
}).DialContext,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
}
req, err := http.NewRequest(http.MethodGet, URL, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0")
req.Header.Set("Host", "danbooru.donmai.us")
result, err := client.Do(req)
if err != nil {
return nil, err
}
if result.StatusCode != http.StatusOK {
return nil, errors.New(result.Status)
}
data, err := ioutil.ReadAll(result.Body)
if err != nil {
return nil, err
}
return data, nil
}
type Danbooru struct {
ID int `json:"id,omitempty"`
CreatedAt string `json:"created_at"`
UploaderID int `json:"uploader_id"`
Score int `json:"score"`
Source string `json:"source"`
Md5 string `json:"md5,omitempty"`
LastCommentBumpedAt interface{} `json:"last_comment_bumped_at"`
Rating string `json:"rating"`
ImageWidth int `json:"image_width"`
ImageHeight int `json:"image_height"`
TagString string `json:"tag_string"`
IsNoteLocked bool `json:"is_note_locked"`
FavCount int `json:"fav_count"`
FileExt string `json:"file_ext,omitempty"`
LastNotedAt interface{} `json:"last_noted_at"`
IsRatingLocked bool `json:"is_rating_locked"`
ParentID interface{} `json:"parent_id"`
HasChildren bool `json:"has_children"`
ApproverID interface{} `json:"approver_id"`
TagCountGeneral int `json:"tag_count_general"`
TagCountArtist int `json:"tag_count_artist"`
TagCountCharacter int `json:"tag_count_character"`
TagCountCopyright int `json:"tag_count_copyright"`
FileSize int `json:"file_size"`
IsStatusLocked bool `json:"is_status_locked"`
PoolString string `json:"pool_string"`
UpScore int `json:"up_score"`
DownScore int `json:"down_score"`
IsPending bool `json:"is_pending"`
IsFlagged bool `json:"is_flagged"`
IsDeleted bool `json:"is_deleted"`
TagCount int `json:"tag_count"`
UpdatedAt string `json:"updated_at"`
IsBanned bool `json:"is_banned"`
PixivID int `json:"pixiv_id"`
LastCommentedAt interface{} `json:"last_commented_at"`
HasActiveChildren bool `json:"has_active_children"`
BitFlags int `json:"bit_flags"`
TagCountMeta int `json:"tag_count_meta"`
HasLarge bool `json:"has_large"`
HasVisibleChildren bool `json:"has_visible_children"`
TagStringGeneral string `json:"tag_string_general"`
TagStringCharacter string `json:"tag_string_character"`
TagStringCopyright string `json:"tag_string_copyright"`
TagStringArtist string `json:"tag_string_artist"`
TagStringMeta string `json:"tag_string_meta"`
FileURL string `json:"file_url,omitempty"`
LargeFileURL string `json:"large_file_url,omitempty"`
PreviewFileURL string `json:"preview_file_url,omitempty"`
}