From 6ec1e5597d9fb4d9c81936d440f250586d0b667e Mon Sep 17 00:00:00 2001 From: Ryan Slade Date: Thu, 7 Mar 2019 11:23:18 +0200 Subject: [PATCH] Refactor isBot (#45) --- browser.go | 6 +++--- system.go | 16 +++++----------- uasurfer.go | 1 + 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/browser.go b/browser.go index 55a1733..20987a8 100644 --- a/browser.go +++ b/browser.go @@ -23,7 +23,7 @@ func (u *UserAgent) evalBrowserName(ua string) bool { // Blackberry goes first because it reads as MSIE & Safari if strings.Contains(ua, "blackberry") || strings.Contains(ua, "playbook") || strings.Contains(ua, "bb10") || strings.Contains(ua, "rim ") { u.Browser.Name = BrowserBlackberry - return u.isBot() + return u.maybeBot() } if strings.Contains(ua, "applewebkit") { @@ -92,7 +92,7 @@ func (u *UserAgent) evalBrowserName(ua string) bool { goto notwebkit } - return u.isBot() + return u.maybeBot() } notwebkit: @@ -159,7 +159,7 @@ notwebkit: } - return u.isBot() + return u.maybeBot() } // Retrieve browser version diff --git a/system.go b/system.go index e823c9c..8dc6fe2 100644 --- a/system.go +++ b/system.go @@ -11,7 +11,6 @@ var ( ) func (u *UserAgent) evalOS(ua string) bool { - s := strings.IndexRune(ua, '(') e := strings.IndexRune(ua, ')') if s > e { @@ -102,23 +101,18 @@ func (u *UserAgent) evalOS(ua string) bool { } } - return u.isBot() + return u.maybeBot() } -func (u *UserAgent) isBot() bool { - - if u.OS.Platform == PlatformBot || u.OS.Name == OSBot { - u.DeviceType = DeviceComputer - return true - } - - if u.Browser.Name >= BrowserBot && u.Browser.Name <= BrowserYahooBot { +// maybeBot checks if the UserAgent is a bot and sets +// all bot related fields if it is +func (u *UserAgent) maybeBot() bool { + if u.IsBot() { u.OS.Platform = PlatformBot u.OS.Name = OSBot u.DeviceType = DeviceComputer return true } - return false } diff --git a/uasurfer.go b/uasurfer.go index c1c11c9..4591852 100644 --- a/uasurfer.go +++ b/uasurfer.go @@ -164,6 +164,7 @@ func (ua *UserAgent) Reset() { ua.DeviceType = DeviceUnknown } +// IsBot returns true if the UserAgent represent a bot func (ua *UserAgent) IsBot() bool { if ua.Browser.Name >= BrowserBot && ua.Browser.Name <= BrowserYahooBot { return true