Skip to content
This repository has been archived by the owner on Feb 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #65 from snackmgmg/add-support-bingbot
Browse files Browse the repository at this point in the history
add more support for bingbot
  • Loading branch information
mssola authored Apr 15, 2020
2 parents 7269cce + c6402a7 commit 80733e9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
15 changes: 15 additions & 0 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ var uastrings = []struct {
ua: "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)",
expected: "Mozilla:5.0 Browser:bingbot-2.0 Bot:true Mobile:false",
},
{
title: "BingBotSmartphone(iPhone)",
ua: "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)",
expected: "Mozilla:5.0 Browser:bingbot-2.0 Bot:true Mobile:true",
},
{
title: "BingBotSmartphone(Android)",
ua: "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.96 Mobile Safari/537.36 Edg/80.0.345.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)",
expected: "Mozilla:5.0 Browser:bingbot-2.0 Bot:true Mobile:true",
},
{
title: "BingBotEmulateMozilla",
ua: "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm) Chrome/41.0.2272.96 Mobile Safari/537.36 Edg/80.0.345.0",
expected: "Mozilla:5.0 Browser:bingbot-2.0 Bot:true Mobile:true",
},
{
title: "BaiduBot",
ua: "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)",
Expand Down
13 changes: 8 additions & 5 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ func getFromSite(comment []string) string {
}

// Returns true if the info that we currently have corresponds to the Google
// mobile bot. This function also modifies some attributes in the receiver
// or Bing mobile bot. This function also modifies some attributes in the receiver
// accordingly.
func (p *UserAgent) googleBot() bool {
// This is a hackish way to detect Google's mobile bot (Googlebot, AdsBot-Google-Mobile, etc.).
// See https://support.google.com/webmasters/answer/1061943
if strings.Index(p.ua, "Google") != -1 {
func (p *UserAgent) googleOrBingBot() bool {
// This is a hackish way to detect
// Google's mobile bot (Googlebot, AdsBot-Google-Mobile, etc.)
// (See https://support.google.com/webmasters/answer/1061943)
// and Bing's mobile bot
// (See https://www.bing.com/webmaster/help/which-crawlers-does-bing-use-8c184ec0)
if strings.Index(p.ua, "Google") != -1 || strings.Index(p.ua, "bingbot") != -1{
p.platform = ""
p.undecided = true
}
Expand Down
3 changes: 2 additions & 1 deletion browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ func (p *UserAgent) detectBrowser(sections []section) {
}
// It's possible the google-bot emulates these now
for _, comment := range engine.comment {
if len(comment) > 5 && strings.HasPrefix(comment, "Googlebot") {
if len(comment) > 5 &&
(strings.HasPrefix(comment, "Googlebot") || strings.HasPrefix(comment, "bingbot")) {
p.undecided = true
break
}
Expand Down
4 changes: 2 additions & 2 deletions operating_systems.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func webkit(p *UserAgent, comment []string) {
if len(comment) > 3 {
p.localization = comment[3]
} else if len(comment) == 3 {
_ = p.googleBot()
_ = p.googleOrBingBot()
}
} else if len(comment) > 0 {
if len(comment) > 3 {
Expand All @@ -100,7 +100,7 @@ func webkit(p *UserAgent, comment []string) {
} else if len(comment) < 2 {
p.localization = comment[0]
} else if len(comment) < 3 {
if !p.googleBot() {
if !p.googleOrBingBot() {
p.os = normalizeOS(comment[1])
}
} else {
Expand Down

0 comments on commit 80733e9

Please sign in to comment.