-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.go
46 lines (36 loc) · 820 Bytes
/
url.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
package linkquisition
import (
"net/url"
"regexp"
"golang.org/x/net/publicsuffix"
)
type URL struct {
url string
}
func NewURL(u string) *URL {
return &URL{url: u}
}
func (u URL) GetDomain() (string, error) {
parsedUrl, err := url.Parse(u.url)
if err != nil {
return "", err
}
// If the hostname is an IP address, we return it as is
re := regexp.MustCompile(`^\d+\.\d+\.\d+\.\d+$`)
if re.MatchString(parsedUrl.Hostname()) {
return parsedUrl.Hostname(), nil
}
tldPlusOne, err := publicsuffix.EffectiveTLDPlusOne(parsedUrl.Hostname())
if err != nil {
return "", err
}
return tldPlusOne, nil
}
func (u URL) GetSite() (string, error) {
re := regexp.MustCompile(`^https?://([^/]+)(/|$)`)
match := re.FindStringSubmatch(u.url)
if len(match) > 1 {
return match[1], nil
}
return "", nil
}