forked from digitalcrab/browscap_go
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbrowser.go
226 lines (189 loc) · 5.31 KB
/
browser.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
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package browscap_go
import (
"fmt"
"reflect"
"strings"
"sync"
)
type Browser struct {
parent string //name of the parent
built bool // has complete data from parents
buildMu sync.Mutex
Browser string
BrowserVersion string
BrowserMajorVer string
BrowserMinorVer string
// Browser, Application, Bot/Crawler, Useragent Anonymizer, Offline Browser,
// Multimedia Player, Library, Feed Reader, Email Client or unknown
BrowserType string
Platform string
PlatformShort string
PlatformVersion string
// Mobile Phone, Mobile Device, Tablet, Desktop, TV Device, Console,
// FonePad, Ebook Reader, Car Entertainment System or unknown
DeviceType string
DeviceName string
DeviceBrand string
Crawler string
Cookies string
JavaScript string
RenderingEngineName string
RenderingEngineVersion string
}
func (browser *Browser) build(browsers map[string]*Browser) {
if browser.built {
return
}
browser.buildMu.Lock()
defer browser.buildMu.Unlock()
// Check again after lock if another gorutine built the object while we were waiting
if browser.built {
return
}
n := reflect.ValueOf(*browser).NumField()
current := reflect.ValueOf(browser)
parent := browser.parent
for parent != "" {
b, ok := browsers[parent]
if !ok {
break
}
parentObj := reflect.ValueOf(b)
for i := 0; i < n; i++ {
cField := current.Elem().Field(i)
if cField.String() != "" {
continue
}
pField := parentObj.Elem().Field(i)
if pField.String() == "" {
continue
}
cField.SetString(pField.String())
}
parent = b.parent
}
browser.built = true
}
func (browser *Browser) setValue(key, item string) {
if key == "Parent" {
browser.parent = item
} else if key == "Browser" {
browser.Browser = item
} else if key == "Version" {
browser.BrowserVersion = item
} else if key == "MajorVer" {
browser.BrowserMajorVer = item
} else if key == "MinorVer" {
browser.BrowserMinorVer = item
} else if key == "Browser_Type" {
browser.BrowserType = item
} else if key == "JavaScript" {
browser.JavaScript = item
} else if key == "Cookies" {
browser.Cookies = item
} else if key == "Crawler" {
browser.Crawler = item
} else if key == "Platform" {
browser.Platform = item
browser.PlatformShort = strings.ToLower(item)
if strings.HasPrefix(browser.PlatformShort, "win") {
browser.PlatformShort = "win"
} else if strings.HasPrefix(browser.PlatformShort, "mac") {
browser.PlatformShort = "mac"
}
} else if key == "Platform_Version" {
browser.PlatformVersion = item
} else if key == "RenderingEngine_Name" {
browser.RenderingEngineName = item
} else if key == "RenderingEngine_Version" {
browser.RenderingEngineVersion = item
} else if key == "Device_Type" {
browser.DeviceType = item
} else if key == "Device_Code_Name" {
browser.DeviceName = item
} else if key == "Device_Brand_Name" {
browser.DeviceBrand = item
}
}
func extractBrowser(data map[string]string) *Browser {
browser := &Browser{}
if debug {
fmt.Println("= Browser ==================")
for k, v := range data {
fmt.Printf("%s = %s\n", k, v)
}
fmt.Println("============================")
}
// Browser
if item, ok := data["Browser"]; ok {
browser.Browser = item
}
if item, ok := data["Version"]; ok {
browser.BrowserVersion = item
}
if item, ok := data["MajorVer"]; ok {
browser.BrowserMajorVer = item
}
if item, ok := data["MinorVer"]; ok {
browser.BrowserMinorVer = item
}
if item, ok := data["Browser_Type"]; ok {
browser.BrowserType = item
}
// Platform
if item, ok := data["Platform"]; ok {
browser.Platform = item
browser.PlatformShort = strings.ToLower(item)
if strings.HasPrefix(browser.PlatformShort, "win") {
browser.PlatformShort = "win"
} else if strings.HasPrefix(browser.PlatformShort, "mac") {
browser.PlatformShort = "mac"
}
}
if item, ok := data["Platform_Version"]; ok {
browser.PlatformVersion = item
}
// Device
if item, ok := data["Device_Type"]; ok {
browser.DeviceType = item
}
if item, ok := data["Device_Code_Name"]; ok {
browser.DeviceName = item
}
if item, ok := data["Device_Brand_Name"]; ok {
browser.DeviceBrand = item
}
return browser
}
func (browser *Browser) IsCrawler() bool {
return browser.BrowserType == "Bot/Crawler" || browser.Crawler == "true"
}
func (browser *Browser) IsMobile() bool {
return browser.DeviceType == "Mobile Phone" || browser.DeviceType == "Mobile Device"
}
func (browser *Browser) IsTablet() bool {
return browser.DeviceType == "Tablet" || browser.DeviceType == "FonePad" || browser.DeviceType == "Ebook Reader"
}
func (browser *Browser) IsDesktop() bool {
return browser.DeviceType == "Desktop"
}
func (browser *Browser) IsConsole() bool {
return browser.DeviceType == "Console"
}
func (browser *Browser) IsTv() bool {
return browser.DeviceType == "TV Device"
}
func (browser *Browser) IsAndroid() bool {
return browser.Platform == "Android"
}
func (browser *Browser) IsIPhone() bool {
return browser.Platform == "iOS" && browser.DeviceName == "iPhone"
}
func (browser *Browser) IsIPad() bool {
return browser.Platform == "iOS" && browser.DeviceName == "iPad"
}
func (browser *Browser) IsWinPhone() bool {
return strings.Index(browser.Platform, "WinPhone") != -1 || browser.Platform == "WinMobile"
}