/
checker.go
222 lines (196 loc) · 5.44 KB
/
checker.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
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/xconstruct/go-pushbullet"
)
// Checker holds the settings for the checker of the millon-timer
type Checker struct {
PBClient *pushbullet.Client
Config CheckerConfig
Target *pushbullet.User
Silent bool
Cache Cache
}
// CheckerConfig is setting of Checker
type CheckerConfig struct {
PushBulletToken string
DailyRewardHour int
FesTimeLeftMin int
RedisURL string
}
type pbLink struct {
Email string `json:"email"`
Type string `json:"type"`
Title string `json:"title"`
URL string `json:"url"`
Body string `json:"body,omitempty"`
}
// NewChecker is generator for Checker
func NewChecker(config CheckerConfig, s bool) *Checker {
pb := pushbullet.New(config.PushBulletToken)
user, _ := pb.Me()
var cache Cache
if config.RedisURL != "" {
cache = NewCacheRedis(config.RedisURL)
} else {
cache = NewCacheFile()
}
checker := &Checker{PBClient: pb, Target: user, Silent: s, Config: config, Cache: cache}
return checker
}
// Close closes the Checker
func (c *Checker) Close() {
c.Cache.Close()
}
func (c *Checker) pushNotify(title string, body string) error {
link := pbLink{
Email: c.Target.Email,
Type: "link",
Title: title,
URL: endpoint + "/mypage",
Body: body,
}
return c.PBClient.Push("/pushes", link)
}
// CheckElement is checks whether the specified selector is present in the corresponding page
func (c *Checker) CheckElement(bw *Browser, s, msg, title, body string) error {
html, _ := bw.Find(s).Html()
if html != "" {
flg := c.Cache.GetBool("CheckElement:" + s)
if !flg {
if !c.Silent {
fmt.Println(msg)
}
c.Cache.Set("CheckElement:"+s, true)
return c.pushNotify(title, body)
}
} else {
c.Cache.Del("CheckElement:" + s)
}
return nil
}
// CheckPopup is checks popup list
func (c *Checker) CheckPopup(bw *Browser) error {
n := false
info := ""
m := c.Cache.GetMap("CheckPopup")
bw.Find("div#main-img div#popup ul li a").Each(func(_ int, s *goquery.Selection) {
t := s.Text()
if t == "合同フェスへの参加要請が届いています" {
return
}
n = true
_, ok := m[t]
if ok {
return
}
if !c.Silent {
fmt.Println("new info: " + t)
}
info = info + t + "\n"
m[t] = 1
})
if n {
c.Cache.Set("CheckPopup", m)
if info != "" {
c.pushNotify("未読のお知らせがあります", info)
}
} else {
c.Cache.Del("CheckPopup")
}
return nil
}
// CheckFes is checks fes
func (c *Checker) CheckFes(bw *Browser) error {
flg := false
checker := func() {
bw.Find("ul.list-bg li").Each(func(_ int, s *goquery.Selection) {
html, _ := s.Find("div.fes-li-enemy div.fes-li-label-area").Html()
if strings.Contains(html, "http://m.ip.bn765.com/1100b9af30c4c51d0b") {
t, _ := time.Parse("15:04:05", s.Find("table dd.txt-ngtv").Text())
if t.Minute() <= c.Config.FesTimeLeftMin {
flg = true
}
}
})
}
for _, path := range []string{"/fes/event_multi_list", "/fes/event_list", "/fes"} {
err := bw.Open(path)
if err != nil {
return err
}
checker()
}
if flg {
if !c.Silent {
fmt.Println("exist near the end of fes")
}
c.pushNotify("終了目前のフェスがあります", "勿体ないので処理しましょう")
}
return nil
}
func (c *Checker) checkTextCore(bw *Browser, r *regexp.Regexp, f func(m [][]byte) bool, s, msg, title, body string) error {
matchs := r.FindSubmatch([]byte(bw.Find(s).Text()))
if len(matchs) == 3 {
if f(matchs) {
flg := c.Cache.GetBool("CheckText:" + s + r.String())
if !flg {
if !c.Silent {
fmt.Println(msg)
}
c.Cache.Set("CheckText:"+s+r.String(), true)
return c.pushNotify(title, body)
}
} else {
c.Cache.Del("CheckText:" + s + r.String())
}
}
return nil
}
// CheckText compares retrieves the value by regular expression from the text in the specified selector
func (c *Checker) CheckText(bw *Browser, r *regexp.Regexp, s, msg, title, body string) error {
return c.checkTextCore(bw, r, func(m [][]byte) bool { return string(m[1]) == string(m[2]) },
s, msg, title, body)
}
// CheckTextDailyReward compares retrieves the value by regular expression from the text in the specified selector
func (c *Checker) CheckTextDailyReward(bw *Browser, r *regexp.Regexp, s, msg, title, body string) error {
return c.checkTextCore(bw, r,
func(m [][]byte) bool {
return string(m[1]) != string(m[2]) && time.Now().Hour() == c.Config.DailyRewardHour
},
s, msg, title, body)
}
// CheckBirthday checks birthday's blessing
func (c *Checker) CheckBirthday(bw *Browser) error {
bstr := bw.Find("div.pd-all p:nth-child(2) span.font-ex").Text()
blessing, _ := strconv.Atoi(strings.Replace(bstr, ",", "", -1))
if blessing >= 30000 {
d := c.Cache.GetString("CheckBirthday:present")
if d != time.Now().Format("2006-01-02") {
if !c.Silent {
fmt.Println("can send birthday present")
}
c.Cache.Set("CheckBirthday:present", time.Now().Format("2006-01-02"))
c.pushNotify("アイドルに誕生日プレゼントを贈れます", "お祝いしてあげましょう")
}
}
html, _ := bw.Find("a.birthday-btn.celebrate").Html()
if html != "" {
flg := c.Cache.GetBool("CheckBirthday:bless")
if !flg {
if !c.Silent {
fmt.Println("can bless birthday")
}
c.Cache.Set("CheckBirthday:bless", true)
c.pushNotify("アイドルの誕生日を祝福できます", "お祝いしてあげましょう")
}
} else {
c.Cache.Del("CheckBirthday:bless")
}
return nil
}