-
Notifications
You must be signed in to change notification settings - Fork 0
/
ua.go
62 lines (56 loc) · 1.44 KB
/
ua.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
package transform
import (
"encoding/json"
"github.com/mssola/user_agent"
"net/url"
)
type UserAgent struct {
Platform string `json:"platform"`
OperatingSystem OperatingSystem `json:"operatingSystem"`
Localization string `json:"localization"`
Browser Browser `json:"browser"`
Bot bool `json:"bot"`
Mobile bool `json:"mobile"`
}
type OperatingSystem struct {
Name string `json:"name"`
FullName string `json:"fullName"`
Version string `json:"version"`
}
type Browser struct {
Name string `json:"name"`
Version string `json:"version"`
Engine string `json:"engine"`
EngineVersion string `json:"engineVersion"`
}
func NewUserAgent(uaString string) (UserAgent, error) {
str, err := url.QueryUnescape(uaString)
if err != nil {
return UserAgent{}, err
}
ua := user_agent.New(str)
os := ua.OSInfo()
bName, bVersion := ua.Browser()
eName, eVersion := ua.Engine()
return UserAgent{
Platform: ua.Platform(),
OperatingSystem: OperatingSystem{
Name: os.Name,
FullName: os.FullName,
Version: os.Version,
},
Localization: ua.Localization(),
Browser: Browser{
Name: bName,
Version: bVersion,
Engine: eName,
EngineVersion: eVersion,
},
Bot: ua.Bot(),
Mobile: ua.Mobile(),
}, nil
}
func (u UserAgent) String() string {
jsonStr, _ := json.Marshal(u)
return string(jsonStr)
}