-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.go
268 lines (246 loc) · 6.84 KB
/
export.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
package main
import (
"encoding/json"
"errors"
"flag"
"fmt"
"net/http"
"os"
"path"
"time"
"github.com/go-kit/kit/log"
"github.com/gocarina/gocsv"
"github.com/peterbourgon/ff/v3"
)
func main() {
fs := flag.NewFlagSet("ge", flag.ExitOnError)
var (
cookie = fs.String("cookie", "develop", "cookie value from browser")
csrfToken = fs.String("csrf_token", "", "the CSRF token from the browser")
)
ff.Parse(fs, os.Args[1:],
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(ff.PlainParser),
ff.WithEnvVarNoPrefix(),
)
w := log.NewSyncWriter(os.Stderr)
l := log.NewLogfmtLogger(w)
l = log.With(l, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
if *cookie == "" && *csrfToken == "" {
l.Log("msg", "missing parameters", "err", errors.New("cookies or csrfToken env variables can't be empty"))
return
}
client := &http.Client{
Transport: &AuthenticatedTransport{
cookie: *cookie,
csrfToken: *csrfToken,
},
}
if err := collect(client, l); err != nil {
l.Log("msg", "there was an error collecting analytics data", "err", err)
return
}
}
func collect(c *http.Client, l log.Logger) error {
req, err := http.NewRequest("GET", "https://secure.gaug.es/gauges/embedded", nil)
if err != nil {
return err
}
resp, err := c.Do(req)
if err != nil {
return err
}
var p profile
if err := json.NewDecoder(resp.Body).Decode(&p); err != nil {
return err
}
for _, g := range p.Gauges {
l.Log("msg", "exporting traffic for site", "site", g.Title)
if err := exportMonth(c, g.Urls.Traffic, g.Title); err != nil {
l.Log("msg", "error while exporting monthly data", "err", err)
continue
}
}
return nil
}
func exportMonth(c *http.Client, url string, gaugeName string) error {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
resp, err := c.Do(req)
if err != nil {
return err
}
var pd profileData
if err := json.NewDecoder(resp.Body).Decode(&pd); err != nil {
return err
}
var events []dayTraffic
for _, d := range pd.Data.Month.Traffic {
events = append(events, dayTraffic{
Date: d.Date,
Views: d.Views,
People: d.People,
})
}
// Get month for file name
var dateFilename string
if len(events) > 0 {
t, err := time.Parse("2006-01-02", events[0].Date)
if err != nil {
return err
}
dateFilename = t.Format("2006-01")
}
outputDirectory := fmt.Sprintf("output/%s", gaugeName)
if _, err := os.Stat(outputDirectory); os.IsNotExist(err) {
if err := os.Mkdir(outputDirectory, 0700); err != nil {
return err
}
}
file, err := os.Create(path.Join(outputDirectory, fmt.Sprintf("%s-%s.csv", gaugeName, dateFilename)))
if err != nil {
return err
}
defer file.Close()
err = gocsv.MarshalFile(events, file)
if err != nil {
return err
}
if pd.Urls.Month.Older != "" {
if err := exportMonth(c, pd.Urls.Older, gaugeName); err != nil {
return err
}
}
return nil
}
type AuthenticatedTransport struct {
cookie string
csrfToken string
T http.RoundTripper
}
func (t *AuthenticatedTransport) RoundTrip(req *http.Request) (*http.Response, error) {
originalRoundtripper := t.T
if originalRoundtripper == nil {
originalRoundtripper = http.DefaultTransport
}
req.Header.Add("Accept", "application/json, text/javascript, */*; q=0.01")
req.Header.Add("Cookie", t.cookie)
req.Header.Add("Accept-Language", "en-us")
req.Header.Add("Host", "secure.gaug.es")
req.Header.Add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15")
req.Header.Add("Referer", "https://secure.gaug.es/dashboard")
req.Header.Add("Connection", "keep-alive")
req.Header.Add("X_csrf_token", t.csrfToken)
req.Header.Add("X-Requested-With", "XMLHttpRequest")
return originalRoundtripper.RoundTrip(req)
}
// dayTraffic contains the aggregated traffic information for one day
type dayTraffic struct {
Date string `csv:"date"`
Views int `csv:"views"`
People int `csv:"people"`
}
// profile is a user profile on gaug.es
type profile struct {
Gauges []struct {
ID string `json:"id"`
CreatedAt time.Time `json:"created_at"`
CreatorID string `json:"creator_id"`
AllowedHosts string `json:"allowed_hosts"`
Title string `json:"title"`
Tz string `json:"tz"`
Enabled bool `json:"enabled"`
NowInZone time.Time `json:"now_in_zone"`
Urls struct {
Self string `json:"self"`
Referrers string `json:"referrers"`
Content string `json:"content"`
Traffic string `json:"traffic"`
Resolutions string `json:"resolutions"`
Technology string `json:"technology"`
Terms string `json:"terms"`
Engines string `json:"engines"`
Locations string `json:"locations"`
Shares string `json:"shares"`
} `json:"urls"`
AllTime struct {
Views int `json:"views"`
People int `json:"people"`
} `json:"all_time"`
Today struct {
Date string `json:"date"`
Views int `json:"views"`
People int `json:"people"`
} `json:"today"`
Yesterday struct {
Date string `json:"date"`
Views int `json:"views"`
People int `json:"people"`
} `json:"yesterday"`
RecentHours []struct {
Hour string `json:"hour"`
Views int `json:"views"`
People int `json:"people"`
} `json:"recent_hours"`
RecentDays []struct {
Date string `json:"date"`
Views int `json:"views"`
People int `json:"people"`
} `json:"recent_days"`
RecentMonths []struct {
Date string `json:"date"`
Views int `json:"views"`
People int `json:"people"`
} `json:"recent_months"`
RecentYears []struct {
Date string `json:"date"`
Views int `json:"views"`
People int `json:"people"`
} `json:"recent_years"`
} `json:"gauges"`
}
// profileData is a page of metrics for a gauge in a time range
type profileData struct {
Date string `json:"date"`
Traffic []struct {
Date string `json:"date"`
Views int `json:"views"`
People int `json:"people"`
} `json:"traffic"`
Views int `json:"views"`
People int `json:"people"`
Data struct {
Month struct {
Traffic []struct {
Date string `json:"date"`
Views int `json:"views"`
People int `json:"people"`
} `json:"traffic"`
Views int `json:"views"`
People int `json:"people"`
} `json:"month"`
Year struct {
Traffic []struct {
Date string `json:"date"`
Views int `json:"views"`
People int `json:"people"`
} `json:"traffic"`
Views int `json:"views"`
People int `json:"people"`
} `json:"year"`
} `json:"data"`
Urls struct {
Older string `json:"older"`
Newer interface{} `json:"newer"`
Month struct {
Older string `json:"older"`
Newer interface{} `json:"newer"`
} `json:"month"`
Year struct {
Older string `json:"older"`
Newer interface{} `json:"newer"`
} `json:"year"`
} `json:"urls"`
}