This repository has been archived by the owner on Nov 11, 2017. It is now read-only.
/
googleplay_update_checker.go
executable file
·168 lines (150 loc) · 3.63 KB
/
googleplay_update_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
package main
import (
"flag"
"fmt"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
log "github.com/Sirupsen/logrus"
"github.com/operando/golack"
)
const (
GOOGLE_PLAY = "https://play.google.com/store/apps/details?id="
APP_STORE = "https://itunes.apple.com/{{country}}/app/{{appId}}"
)
var old_update_date string
var new_update_date string
var oldSoftwareVersion string
var newSoftwareVersion string
func checkUpdate(url string) bool {
doc, err := goquery.NewDocument(url)
if err != nil {
log.Fatal(err)
return false
}
isUpdate := false
doc.Find("div[itemprop=\"datePublished\"]").Each(func(_ int, s *goquery.Selection) {
log.Debug(s.Text())
if old_update_date == "" {
old_update_date = s.Text()
log.Info("Old update date : " + old_update_date)
} else {
new_update_date = s.Text()
if old_update_date != new_update_date {
log.Info("New update date : " + new_update_date)
isUpdate = true
}
}
})
log.Debug(isUpdate)
return isUpdate
}
func checkUpdateIos(url string) bool {
isUpdate := false
doc, err := goquery.NewDocument(url)
if err != nil {
log.Fatal(err)
return false
}
doc.Find("ul.list span[itemprop=softwareVersion]").Each(func(_ int, s *goquery.Selection) {
log.Debug(s.Text())
if oldSoftwareVersion == "" {
oldSoftwareVersion = s.Text()
log.Info("Old Software Version : " + oldSoftwareVersion)
} else {
newSoftwareVersion = s.Text()
if oldSoftwareVersion != newSoftwareVersion {
log.Info("New Software Version : " + newSoftwareVersion)
isUpdate = true
}
}
})
log.Debug(isUpdate)
return isUpdate
}
func createAppStoreURL(ios Ios) string {
replaceCountryURL := strings.Replace(APP_STORE, "{{country}}", ios.Country, 1)
appStoreURL := strings.Replace(replaceCountryURL, "{{appId}}", ios.AppId, 1)
log.Debug(appStoreURL)
return appStoreURL
}
func createGooglePlayURL(android Android) string {
googlePlayURL := GOOGLE_PLAY + android.Package
log.Debug(googlePlayURL)
return googlePlayURL
}
func main() {
var configPath = flag.String("c", "", "configuration file path")
flag.Parse()
var config Config
_, err := LoadConfig(*configPath, &config)
if err != nil {
fmt.Println(err)
return
}
setLogLevel(config.Log)
sleep := time.Duration(config.SleepTime*60) * time.Second
var googlePlayURL string
var appStoreURL string
payload := golack.Payload{
config.Slack,
}
checkIos := true
if config.Ios.AppId == "" {
checkIos = false
log.Debug("AppId is empty.")
} else {
appStoreURL = createAppStoreURL(config.Ios)
log.Info("Check App Store URL : " + appStoreURL)
}
checkAndroid := true
if config.Android.Package == "" {
checkAndroid = false
log.Debug("Package is empty.")
} else {
googlePlayURL = createGooglePlayURL(config.Android)
log.Info("Check Google Play URL : " + googlePlayURL)
}
log.Info("Slack Post Message : " + config.Slack.Text)
for {
if checkAndroid {
if checkUpdate(googlePlayURL) {
golack.Post(payload, config.Webhook)
log.Info("Update!!!!!!!!!!!")
break
} else {
log.Info("No Update")
}
}
if checkIos {
if checkUpdateIos(appStoreURL) {
golack.Post(payload, config.Webhook)
log.Info("Update!!!!!!!!!!!")
break
} else {
log.Info("No Update")
}
}
time.Sleep(sleep)
}
log.Info("Update check end.")
}
func init() {
log.SetLevel(log.InfoLevel)
}
func setLogLevel(lv string) {
switch lv {
case "debug", "d":
log.SetLevel(log.DebugLevel)
case "info", "i":
log.SetLevel(log.InfoLevel)
case "warn", "w":
log.SetLevel(log.WarnLevel)
case "error", "e":
log.SetLevel(log.ErrorLevel)
case "fatal", "f":
log.SetLevel(log.FatalLevel)
default:
log.SetLevel(log.InfoLevel)
}
}