-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
347 lines (277 loc) · 7.37 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"bytes"
"database/sql"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"sort"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
_ "github.com/mattn/go-sqlite3"
"gopkg.in/yaml.v2"
)
type Config struct {
AppId string `yaml:"app_id"`
ReviewCount int `yaml:"review_count"`
BotName string `yaml:"bot_name"`
IconEmoji string `yaml:"icon_emoji"`
MessageText string `yaml:"message_text"`
WebHookUri string `yaml:"web_hook_uri"`
DbPath string `yaml:"db_path"`
}
type Review struct {
Id int
Author string
AuthorUri string `meddler:"author_uri"`
Title string
Message string
Rate string
UpdatedAt time.Time `meddler:"updated_at,localtime"`
}
type Reviews []Review
type DBH struct {
*sql.DB
}
type SlackPayload struct {
Text string `json:"text"`
UserName string `json:"username"`
IconEmoji string `json:"icon_emoji"`
Attachments []SlackAttachment `json:"attachments"`
}
type SlackAttachment struct {
Title string `json:"title"`
TitleLink string `json:"title_link"`
Text string `json:"text"`
Fallback string `json:"fallback"`
Fields []SlackAttachmentField `json:"fields"`
}
type SlackAttachmentField struct {
Title string `json:"title"`
Value string `json:"value"`
Short bool `json:"short"`
}
const (
TABLE_NAME = "review"
BASE_URI = "https://play.google.com"
REVIEW_CLASS_NAME = ".single-review"
AUTHOR_NAME_CLASS_NAME = ".review-header .review-info span.author-name"
REVIEW_LINK_CLASS_NAME = ".review-header .review-info .reviews-permalink"
REVIEW_DATE_CLASS_NAME = ".review-header .review-info .review-date"
REVIEW_TITLE_CLASS_NAME = ".review-body .review-title"
REVIEW_MESSAGE_CLASS_NAME = ".review-body"
REVIEW_RATE_CLASS_NAME = ".review-info-star-rating .tiny-star"
RAITING_EMOJI = ":star:"
MAX_REVIEW_NUM = 40
)
var (
dbh *DBH
configFile = flag.String("c", "config.yml", "config file")
)
func GetDBH() *DBH {
return dbh
}
func (dbh *DBH) LastInsertId(tableName string) int {
row := dbh.QueryRow(`SELECT id FROM ` + tableName + ` ORDER BY id DESC LIMIT 1`)
var id int
err := row.Scan(&id)
if err != nil {
if err.Error() == "sql: no rows in result set" {
return 0
}
log.Fatal(err)
}
return id
}
func NewConfig(path string) (config Config, err error) {
config = Config{}
data, err := ioutil.ReadFile(path)
if err != nil {
return config, err
}
if err := yaml.Unmarshal(data, &config); err != nil {
return config, err
}
if config.AppId == "" {
return config, fmt.Errorf("Please Set Your Google Play App Id.")
}
if config.ReviewCount > MAX_REVIEW_NUM || config.ReviewCount < 1 {
return config, fmt.Errorf("Please Set Num Between 1 and 40.")
}
db, err := sql.Open("sqlite3", config.DbPath)
if err != nil {
return config, err
}
err = db.Ping()
if err != nil {
return config, err
}
dbh = &DBH{db}
uri := fmt.Sprintf("%s/store/apps/details?id=%s", BASE_URI, config.AppId)
res, err := http.Get(uri)
if err != nil {
return config, err
}
if res.StatusCode == http.StatusNotFound {
return config, fmt.Errorf("AppID: %s is not exists", config.AppId)
}
return config, err
}
func main() {
flag.Parse()
config, err := NewConfig(*configFile)
if err != nil {
log.Println(err)
return
}
log.Println("start get google play app review")
reviews, err := GetReview(config)
if err != nil {
log.Println(err)
return
}
reviews, err = SaveReviews(reviews)
if err != nil {
log.Println(err)
return
}
err = PostReview(config, reviews)
if err != nil {
log.Println(err)
return
}
log.Println("done")
}
func GetReview(config Config) (Reviews, error) {
uri := fmt.Sprintf("%s/store/apps/details?id=%s", BASE_URI, config.AppId)
doc, err := goquery.NewDocument(uri)
if err != nil {
return nil, err
}
reviews := Reviews{}
doc.Find(REVIEW_CLASS_NAME).Each(func(i int, s *goquery.Selection) {
authorNode := s.Find(AUTHOR_NAME_CLASS_NAME)
authorName := authorNode.Text()
permalink, _ := s.Find(REVIEW_LINK_CLASS_NAME).Attr("href")
dateNode := s.Find(REVIEW_DATE_CLASS_NAME)
date, err := time.Parse("2006年1月2日", dateNode.Text())
if err != nil {
return
}
reviewTitle := s.Find(REVIEW_TITLE_CLASS_NAME).Text()
reviewMessage := s.Find(REVIEW_MESSAGE_CLASS_NAME).Text()
reviewRateNode := s.Find(REVIEW_RATE_CLASS_NAME)
rateMessage, _ := reviewRateNode.Attr("aria-label")
rate := parseRate(rateMessage)
review := Review{
Author: authorName,
AuthorUri: permalink,
Title: reviewTitle,
Message: reviewMessage,
Rate: rate,
UpdatedAt: date,
}
reviews = append(reviews, review)
})
sort.Sort(reviews)
return reviews, nil
}
func parseRate(message string) string {
rate := ""
switch {
case strings.Contains(message, "1つ星"):
rate = strings.Repeat(RAITING_EMOJI, 1)
case strings.Contains(message, "2つ星"):
rate = strings.Repeat(RAITING_EMOJI, 2)
case strings.Contains(message, "3つ星"):
rate = strings.Repeat(RAITING_EMOJI, 3)
case strings.Contains(message, "4つ星"):
rate = strings.Repeat(RAITING_EMOJI, 4)
case strings.Contains(message, "5つ星"):
rate = strings.Repeat(RAITING_EMOJI, 5)
}
return rate
}
func SaveReviews(reviews Reviews) (Reviews, error) {
postReviews := Reviews{}
for _, review := range reviews {
var id int
row := dbh.QueryRow("SELECT id FROM review WHERE author_uri = ?", review.AuthorUri)
err := row.Scan(&id)
if err != nil {
if err.Error() != "sql: no rows in result set" {
return postReviews, err
}
}
if id == 0 {
_, err := dbh.Exec("INSERT INTO review (author, author_uri, updated_at) VALUES (?, ?, ?)",
review.Author, review.AuthorUri, review.UpdatedAt)
if err != nil {
return postReviews, err
}
postReviews = append(postReviews, review)
}
}
return postReviews, nil
}
func PostReview(config Config, reviews Reviews) error {
attachments := []SlackAttachment{}
if 1 > len(reviews) {
return nil
}
for i, review := range reviews {
if i >= config.ReviewCount {
break
}
fields := []SlackAttachmentField{}
fields = append(fields, SlackAttachmentField{
Title: "Raiting",
Value: review.Rate,
Short: true,
})
fields = append(fields, SlackAttachmentField{
Title: "UpdatedAt",
Value: review.UpdatedAt.Format("2006-01-02"),
Short: true,
})
attachments = append(attachments, SlackAttachment{
Title: review.Title,
TitleLink: fmt.Sprintf("%s%s", BASE_URI, review.AuthorUri),
Text: review.Message,
Fallback: review.Title + " " + review.AuthorUri,
Fields: fields,
})
}
slackPayload := SlackPayload{
UserName: config.BotName,
IconEmoji: config.IconEmoji,
Text: config.MessageText,
Attachments: attachments,
}
payload, err := json.Marshal(slackPayload)
if err != nil {
return err
}
req, _ := http.NewRequest("POST", config.WebHookUri, bytes.NewBuffer([]byte(payload)))
req.Header.Set("Content-Type", "application/json")
client := http.DefaultClient
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
return nil
}
func (r Reviews) Len() int {
return len(r)
}
func (r Reviews) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
func (r Reviews) Less(i, j int) bool {
return r[i].UpdatedAt.Unix() > r[j].UpdatedAt.Unix()
}