-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
50 lines (40 loc) · 1.06 KB
/
parse.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
package referrer
import (
"encoding/csv"
"strings"
)
type Parser struct {
// referers is a map of referer groups to a map of referer names to a slice of domains.
referrers map[string]string
}
// NewParser creates a new Parser instance.
func NewParser() (*Parser, error) {
referrers := make(map[string]string)
// Populate referrers map with referer domains --> referrer names.
r := csv.NewReader(strings.NewReader(referrersCSV))
records, err := r.ReadAll()
if err != nil {
return nil, err
}
for _, row := range records {
referrers[row[2]] = row[1]
}
return &Parser{
referrers: referrers,
}, nil
}
// Parse returns the referer name for a given referer domain. If the domain is not found, it returns nil.
func (p *Parser) Parse(domain string) string {
domain = strings.ToLower(strings.TrimSpace(domain))
// Check for exact match
if name, ok := p.referrers[domain]; ok {
return name
}
// Check for a match stripping the leading "www."
if strings.HasPrefix(domain, "www.") {
if name, ok := p.referrers[domain[4:]]; ok {
return name
}
}
return ""
}