-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
129 lines (110 loc) · 2.89 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
package main
import (
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"strconv"
auth "github.com/dombartenope/viewnotifs/Auth"
filters "github.com/dombartenope/viewnotifs/Filters"
"github.com/dombartenope/viewnotifs/Notification"
utils "github.com/dombartenope/viewnotifs/Utils"
)
func main() {
base_url := "https://api.onesignal.com/notifications?app_id="
fmt.Println(`Available flags to use in your request:
-clear | Clear the .env file to pass in all new values
"go run main.go -clear"
-template | Pass in a template ID to search for instead of pulling all notifications
"go run main.go -template=template_id_here"
`)
// Define a boolean flag
clearFlag := flag.Bool("clear", false, "Clear the .env file before proceeding")
// Define the string per user input
var templateId = flag.String("template", "", "Add a template ID to the request")
flag.Parse()
// Check the flag value
if *clearFlag {
utils.ClearEnvFile() // Clear the .env file if flag is set
}
// Check the flag value
if *templateId != "" {
base_url = fmt.Sprintf("https://api.onesignal.com/notifications?template_id=%s&app_id=", *templateId)
fmt.Println("Base URL:", base_url)
} else {
os.Exit(1)
}
out, err := os.Create("out.csv")
if err != nil {
log.Fatalf("error: %s", err)
}
defer out.Close()
writer := csv.NewWriter(out)
defer writer.Flush()
//TODO Put behind a clear flag
defer os.Clearenv()
client := &http.Client{}
app_id, api_key := auth.CheckForAuth()
var os int
headers := []string{
"DelayedOption",
"DeliveryTimeOfDay",
"Errored",
"Failed",
"ID",
"IncludePlayerIds",
"IncludeExternalUserIds",
"IncludeAliases",
"IncludedSegments",
"SendAfter",
"CompletedAt",
"Successful",
"Received",
"TemplateId",
"TimeSentSpending",
}
writer.Write(headers)
param, choiceValue := filters.UserChoice()
for {
//Convert int to string for url param
offset := strconv.Itoa(os)
url := fmt.Sprintf("%s%s&offset=%s", base_url, app_id, offset)
fmt.Printf("url: %s", url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatalf("error: %s", err)
}
apiKey := fmt.Sprintf("Basic %s", api_key)
req.Header.Add("Authorization", apiKey)
resp, err := client.Do(req)
if err != nil {
log.Fatalf("error: %s", err)
}
var apiResp Notification.ApiResponse
decoder := json.NewDecoder(resp.Body)
if err := decoder.Decode(&apiResp); err != nil {
log.Fatalf("error decoding json: %s", err)
}
if apiResp.TotalCount == 0 {
break
}
fmt.Println(apiResp.TotalCount)
fmt.Println(url)
switch param {
case "seg":
filters.FilterBySegment(choiceValue, apiResp, writer)
case "sid":
filters.FilterById(choiceValue, apiResp, writer)
case "eid":
filters.FilterByEid(choiceValue, apiResp, writer)
default:
filters.NoFilter(apiResp, writer)
}
//Take offset and add 50 each loop
os += 50
resp.Body.Close()
}
}