-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinterest.go
197 lines (169 loc) · 4.12 KB
/
pinterest.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
package airpin
import (
_ "embed"
"encoding/csv"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"
)
type Period int
const (
Week = iota
Month
)
type Pinterest struct {
token string
client *http.Client
base string
period Period
}
func NewPinterest(token string, period Period) *Pinterest {
root := "https://api.pinterest.com/v5/ad_accounts/"
return &Pinterest{token, &http.Client{}, root, period}
}
func (p Pinterest) get(path string) *http.Request {
req, err := http.NewRequest("GET", p.base+path, nil)
if err != nil {
panic(err)
}
req.Header.Add("Authorization", "Bearer "+p.token)
return req
}
func (p Pinterest) post(path string, body io.Reader) *http.Request {
req, err := http.NewRequest("POST", p.base+path, body)
if err != nil {
panic(err)
}
req.Header.Add("Authorization", "Bearer "+p.token)
req.Header.Add("Content-Type", "application/json")
return req
}
func (p Pinterest) do(req *http.Request) io.ReadCloser {
res, err := p.client.Do(req)
if err != nil {
panic(err)
}
if res.StatusCode != http.StatusOK {
body, err := io.ReadAll(res.Body)
if err != nil {
panic(err) // don't recover; log and notify
}
panic(res.Status + ": " + string(body))
}
return res.Body
}
func (p Pinterest) Reports(account_id string) [][]string {
token := p.requestReport(account_id)
url := p.waitForReport(account_id, token)
return p.getRecords(url)
}
//go:embed "config.json"
var cfg string
func (p Pinterest) requestReport(account_id string) string {
var start_date, end_date string
switch p.period {
case Week:
start_date = formatDaysAgo(7)
end_date = formatDaysAgo(1)
case Month:
y, m, _ := time.Now().Date()
_, m, d := time.Date(y, m, 0, 0, 0, 0, 0, time.UTC).Date()
start_date = fmt.Sprintf("%v-%02d-01", y, int(m))
end_date = fmt.Sprintf("%v-%02d-%v", y, int(m), d)
}
path := fmt.Sprintf("%s/reports", account_id)
cfg = strings.Replace(cfg, "<START_DATE>", start_date, 1)
cfg = strings.Replace(cfg, "<END_DATE>", end_date, 1)
reader := strings.NewReader(cfg)
req := p.post(path, reader)
res := p.do(req)
dec := json.NewDecoder(res)
var objmap map[string]interface{}
if err := dec.Decode(&objmap); err != nil {
panic(err)
}
res.Close()
token := objmap["token"].(string)
return token
}
func (p Pinterest) waitForReport(account_id, token string) string {
reportUrl := ""
backoff := 1 * time.Second
timeout := 60 * time.Second
for backoff <= timeout {
time.Sleep(backoff)
backoff *= 2
s := p.reportStatus(account_id, token)
if s.ReportStatus == "FINISHED" {
reportUrl = s.URL
break
}
}
return reportUrl
}
func (p Pinterest) getRecords(url string) [][]string {
// S3 doesn't require authorization
req, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err)
}
res := p.do(req)
// DEBUG (do not delete; check in to repo)
// data, err := io.ReadAll(res)
// if err != nil {
// panic(err)
// }
csvReader := csv.NewReader(res)
records, err := csvReader.ReadAll()
if err != nil {
panic(err)
}
// DEBUG (do not delete; check in to repo)
// header := records[0]
// DEBUG (do not delete; check in to repo)
// fmt.Printf("\n%#v\n")
// DEBUG (do not delete; check in to repo)
// for i, v := range header {
// fmt.Println(i, v)
// }
// DEBUG (do not delete; check in to repo)
// writeToFile("../test.csv", data)
// DEBUG (do not delete; check in to repo)
// os.Exit(0)
return records
}
type status struct {
ReportStatus string `json:"report_status"`
URL string `json:"url"`
Size int `json:"size"`
}
func (p Pinterest) reportStatus(account_id, token string) status {
path := fmt.Sprintf("%s/reports?token=%s", account_id, url.QueryEscape(token))
req := p.get(path)
res := p.do(req)
dec := json.NewDecoder(res)
var s status
if err := dec.Decode(&s); err != nil {
panic(err)
}
res.Close()
return s
}
// Write the bytes to a file for inspection. On GCP, such files will probably
// need to be created in GCS.
func writeToFile(filename string, bytes []byte) {
file, err := os.Create(filename)
if err != nil {
panic(err)
}
defer file.Close()
_, err = file.Write(bytes)
if err != nil {
panic(err)
}
}