-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (143 loc) · 3.46 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
package main
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/schollz/progressbar/v2"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"strings"
)
const playerIDCharLen = 36
type Tags map[string]interface{}
type Body map[string]interface{}
type ErrorInfo struct {
PlayerID string
Error string
}
func (tags *Tags) Parse(jsonStr string) error {
err := json.Unmarshal([]byte(jsonStr), tags)
if err != nil {
return err
}
return nil
}
func main() {
checkArgs()
errorInfo := make([]ErrorInfo, 0)
var sCount int
registerExitHook(&errorInfo, &sCount)
tags := Tags{}
err := tags.Parse(os.Args[1])
if err != nil {
log.Fatal(err)
}
file, err := os.Open("playerids.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
log.Fatal(err)
}
bar := progressbar.New(int(fileInfo.Size() / playerIDCharLen))
_ = bar.RenderBlank()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if playerID := strings.TrimSpace(scanner.Text()); playerID != "" {
_, err := SendTags(playerID, tags)
if err != nil {
errorInfo = append(errorInfo, ErrorInfo{PlayerID: playerID, Error: err.Error()})
} else {
sCount++
}
_ = bar.Add(1)
}
}
_ = bar.Finish()
err = printSummary(errorInfo, sCount)
if err != nil {
log.Fatal(err)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
func SendTags(playerID string, tags Tags) (string, error) {
body := Body{}
body["tags"] = tags
return NewRequest("PUT", "/players/"+playerID, body)
}
func NewRequest(method, subUrl string, body Body) (string, error) {
reqUrl := "https://onesignal.com/api/v1" + subUrl
body["app_id"] = os.Getenv("APP_ID")
jsonObj, err := json.Marshal(body)
if err != nil {
return "", err
}
req, err := http.NewRequest(method, reqUrl, bytes.NewBuffer(jsonObj))
if err != nil {
return "", err
}
defer req.Body.Close()
req.Header.Set("Content-Type", "application/json")
c := &http.Client{}
resp, err := c.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return "", errors.New(
fmt.Sprintf("status code: %d, response: %s", resp.StatusCode, string(b)))
}
return string(b), nil
}
func printSummary(errorInfo []ErrorInfo, sCount int) error {
fmt.Println("\n\nSummary report has been written to \"summary.log\"")
file, err := os.OpenFile("summary.log", os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
_ = file.Truncate(0)
_, _ = file.Seek(0, 0)
_, _ = fmt.Fprintln(file, "************ Summary Report ************")
if len(errorInfo) == 0 {
_, _ = fmt.Fprintln(file, "All (", sCount, ") PlayerID succeed with no errors.")
} else {
_, _ = fmt.Fprintln(file, "We have (", sCount, ") PlayerIDs succeed and",
"(", len(errorInfo), ") PlayerIDs Failed as follows:")
for key := range errorInfo {
_, _ = fmt.Fprintf(file, "PlayerID: %s failed with error: %s\n",
errorInfo[key].PlayerID, errorInfo[key].Error)
}
}
return nil
}
func registerExitHook(errorInfo *[]ErrorInfo, sCount *int) {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
go func() {
<-c
_ = printSummary(*errorInfo, *sCount)
os.Exit(-1)
}()
}
func checkArgs() {
if len(os.Args) < 2 {
fmt.Println("Usage:", os.Args[0], "<json tag>")
fmt.Println("example:\nAPP_ID=myOneSignalAppId", os.Args[0], `'{"approved": true}'`)
os.Exit(-1)
}
}