-
-
Notifications
You must be signed in to change notification settings - Fork 200
/
ref.go
275 lines (233 loc) · 7.46 KB
/
ref.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
269
270
271
272
273
274
275
// Copyright © Martin Tournoij – This file is part of GoatCounter and published
// under the terms of a slightly modified EUPL v1.2 license, which can be found
// in the LICENSE file or at https://license.goatcounter.com
package goatcounter
import (
"context"
"net/url"
"strings"
"zgo.at/errors"
"zgo.at/zcache"
"zgo.at/zdb"
"zgo.at/zstd/ztime"
"zgo.at/zstd/ztype"
)
// ref_scheme column
var (
RefSchemeHTTP = ztype.Ptr("h")
RefSchemeOther = ztype.Ptr("o")
RefSchemeGenerated = ztype.Ptr("g")
RefSchemeCampaign = ztype.Ptr("c")
)
var groups = map[string]string{
// HN has <meta name="referrer" content="origin"> so we only get the domain.
"news.ycombinator.com": "Hacker News",
"hn.algolia.com": "Hacker News",
"hckrnews.com": "Hacker News",
"hn.premii.com": "Hacker News",
"com.stefandekanski.hackernews.free": "Hacker News",
"io.github.hidroh.materialistic": "Hacker News",
"hackerweb.app": "Hacker News",
"www.daemonology.net/hn-daily": "Hacker News",
"quiethn.com": "Hacker News",
"hnews.xyz": "Hacker News",
"hackernewsmobile.com": "Hacker News",
// http://www.elegantreader.com/item/17358103
// https://www.daemonology.net/hn-daily/2019-05.html
"mail.google.com": "Email",
"com.google.android.gm": "Email",
"mail.yahoo.com": "Email",
// https://mailchi.mp
"org.fox.ttrss": "RSS",
"www.inoreader.com": "RSS",
"com.innologica.inoreader": "RSS",
"usepanda.com": "RSS",
"feedly.com": "RSS",
"com.google.android.googlequicksearchbox": "Google",
"com.google.android.googlequicksearchbox/https/www.google.com": "Google",
"com.andrewshu.android.reddit": "www.reddit.com",
"com.laurencedawson.reddit_sync": "www.reddit.com",
"com.laurencedawson.reddit_sync.dev": "www.reddit.com",
"com.laurencedawson.reddit_sync.pro": "www.reddit.com",
"m.facebook.com": "www.facebook.com",
"l.facebook.com": "www.facebook.com",
"lm.facebook.com": "www.facebook.com",
"org.telegram.messenger": "Telegram Messenger",
// Slack
"com.slack": "Slack Chat",
// Baidu
"baidu.com": "Baidu",
"c.tieba.baidu.com": "Baidu",
"m.baidu.com": "Baidu",
"tieba.baidu.com": "Baidu",
"www.baidu.com": "Baidu",
}
var hostAlias = map[string]string{
"en.m.wikipedia.org": "en.wikipedia.org",
"m.facebook.com": "www.facebook.com",
"m.habr.com": "habr.com",
"old.reddit.com": "www.reddit.com",
"i.reddit.com": "www.reddit.com",
"np.reddit.com": "www.reddit.com",
"fr.reddit.com": "www.reddit.com",
}
type Ref struct {
ID int64 `db:"ref_id"`
Ref string `db:"ref"`
RefScheme *string `db:"ref_scheme"`
}
func (r *Ref) Defaults(ctx context.Context) {}
func (r *Ref) Validate(ctx context.Context) error {
v := NewValidate(ctx)
//v.Required("ref", r.Ref)
//v.Required("ref_scheme", r.RefScheme)
v.UTF8("ref", r.Ref)
v.Len("ref", r.Ref, 0, 2048)
return v.ErrorOrNil()
}
func (r *Ref) GetOrInsert(ctx context.Context) error {
k := r.Ref
if r.RefScheme != nil {
k += string(*r.RefScheme)
}
if r.Ref == "" && r.RefScheme == nil {
r.ID = 1
return nil
}
c, ok := cacheRefs(ctx).Get(k)
if ok {
*r = c.(Ref)
cacheRefs(ctx).Touch(k, zcache.DefaultExpiration)
return nil
}
r.Defaults(ctx)
err := r.Validate(ctx)
if err != nil {
return err
}
err = zdb.Get(ctx, r, `/* Ref.GetOrInsert */
select * from refs
where lower(ref) = lower(?) and ref_scheme = ?
limit 1`, r.Ref, r.RefScheme)
if err == nil {
cacheRefs(ctx).SetDefault(k, *r)
return nil
}
if !zdb.ErrNoRows(err) {
return errors.Wrap(err, "Ref.GetOrInsert get")
}
r.ID, err = zdb.InsertID(ctx, "ref_id",
`insert into refs (ref, ref_scheme) values (?, ?)`,
r.Ref, r.RefScheme)
if err != nil {
return errors.Wrap(err, "Ref.GetOrInsert insert")
}
cacheRefs(ctx).SetDefault(k, *r)
return nil
}
func cleanRefURL(ref string, refURL *url.URL) (string, bool) {
// I'm not sure where these links are generated, but there are *a lot* of
// them.
if refURL.Host == "link.oreilly.com" {
return "link.oreilly.com", false
}
// Always remove protocol.
ref = strings.TrimPrefix(ref, refURL.Scheme)
refURL.Scheme = ""
// Normalize some hosts.
if a, ok := hostAlias[refURL.Host]; ok {
refURL.Host = a
}
// Group based on URL.
if strings.HasPrefix(refURL.Host, "www.google.") || strings.HasPrefix(refURL.Host, "google.") {
// Group all "google.co.nz", "google.nl", etc. as "Google".
return "Google", true
}
if strings.Contains(refURL.Host, "search.yahoo.com") {
return "Yahoo", true
}
if g, ok := groups[strings.ToLower(refURL.Host)]; ok {
return g, true
}
if g, ok := groups[strings.ToLower(ref)]; ok {
return g, true
}
// Useful: https://lobste.rs/s/tslw6k/why_i_m_still_using_jquery_2019
// Not really: https://lobste.rs/newest/page/8, https://lobste.rs/page/7
// https://lobste.rs/search, https://lobste.rs/t/javascript
if refURL.Host == "lobste.rs" && !strings.HasPrefix(refURL.Path, "/s/") {
return "lobste.rs", false
}
if refURL.Host == "gambe.ro" && !strings.HasPrefix(refURL.Path, "/s/") {
return "lobste.rs", false
}
// No sense in retaining path for Pocket:
// app.getpocket.com
// app.getpocket.com/read/2369667792
// getpocket.com
// getpocket.com/a/read/2580004052
// getpocket.com/recommendations
// getpocket.com/redirect
// getpocket.com/users/AAA/feed/read
if refURL.Host == "getpocket.com" || refURL.Host == "app.getpocket.com" {
return "getpocket.com", false
}
// Reddit
// www.reddit.com/r/programming/top
// www.reddit.com/r/programming/.compact
// www.reddit.com/r/programming.compact
// www.reddit.com/r/webdev/new
// www.reddit.com/r/vim/search
if refURL.Host == "www.reddit.com" {
switch {
case strings.HasSuffix(refURL.Path, "/top") || strings.HasSuffix(refURL.Path, "/new"):
refURL.Path = refURL.Path[:len(refURL.Path)-4]
case strings.HasSuffix(refURL.Path, "/search"):
refURL.Path = refURL.Path[:len(refURL.Path)-7]
case strings.HasSuffix(refURL.Path, ".compact"):
refURL.Path = refURL.Path[:len(refURL.Path)-8]
}
}
// Linking https://t.co/c3MITw38Yq isn't too useful as that will link back
// to the page, so link to the Tweet instead.
if refURL.Host == "t.co" && len(refURL.Path) > 1 {
return "twitter.com/search?q=https%3A%2F%2Ft.co" +
url.QueryEscape(refURL.Path), false
}
// Clean query parameters.
i := strings.Index(ref, "?")
if i == -1 {
// No parameters so no work.
return strings.TrimLeft(refURL.String(), "/"), false
}
q := refURL.Query()
refURL.RawQuery = ""
q.Del("utm_source") // Google analytics tracking parameters.
q.Del("utm_medium")
q.Del("utm_campaign")
q.Del("utm_term")
q.Del("utm_content")
q.Del("__cf_chl_captcha_tk__") // Cloudflare
q.Del("__cf_chl_jschl_tk__")
s := refURL.String()
if len(s) > 1 {
return s[2:], false
}
return "/", false
}
// ListRefsByPath lists all references for a pathID.
func (h *HitStats) ListRefsByPathID(ctx context.Context, pathID int64, rng ztime.Range, limit, offset int) error {
err := zdb.Select(ctx, &h.Stats, "load:ref.ListRefsByPathID.sql", map[string]any{
"site": MustGetSite(ctx).ID,
"start": rng.Start,
"end": rng.End,
"path": pathID,
"limit": limit + 1,
"offset": offset,
})
if len(h.Stats) > limit {
h.More = true
h.Stats = h.Stats[:len(h.Stats)-1]
}
return errors.Wrap(err, "HitStats.ListRefsByPathID")
}