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

Commit c6402a7

Browse files
committed
add more support for bingbot
1 parent aaa5fd2 commit c6402a7

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

all_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@ var uastrings = []struct {
4444
ua: "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)",
4545
expected: "Mozilla:5.0 Browser:bingbot-2.0 Bot:true Mobile:false",
4646
},
47+
{
48+
title: "BingBotSmartphone(iPhone)",
49+
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)",
50+
expected: "Mozilla:5.0 Browser:bingbot-2.0 Bot:true Mobile:true",
51+
},
52+
{
53+
title: "BingBotSmartphone(Android)",
54+
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)",
55+
expected: "Mozilla:5.0 Browser:bingbot-2.0 Bot:true Mobile:true",
56+
},
57+
{
58+
title: "BingBotEmulateMozilla",
59+
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",
60+
expected: "Mozilla:5.0 Browser:bingbot-2.0 Bot:true Mobile:true",
61+
},
4762
{
4863
title: "BaiduBot",
4964
ua: "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)",

bot.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@ func getFromSite(comment []string) string {
4242
}
4343

4444
// Returns true if the info that we currently have corresponds to the Google
45-
// mobile bot. This function also modifies some attributes in the receiver
45+
// or Bing mobile bot. This function also modifies some attributes in the receiver
4646
// accordingly.
47-
func (p *UserAgent) googleBot() bool {
48-
// This is a hackish way to detect Google's mobile bot (Googlebot, AdsBot-Google-Mobile, etc.).
49-
// See https://support.google.com/webmasters/answer/1061943
50-
if strings.Index(p.ua, "Google") != -1 {
47+
func (p *UserAgent) googleOrBingBot() bool {
48+
// This is a hackish way to detect
49+
// Google's mobile bot (Googlebot, AdsBot-Google-Mobile, etc.)
50+
// (See https://support.google.com/webmasters/answer/1061943)
51+
// and Bing's mobile bot
52+
// (See https://www.bing.com/webmaster/help/which-crawlers-does-bing-use-8c184ec0)
53+
if strings.Index(p.ua, "Google") != -1 || strings.Index(p.ua, "bingbot") != -1{
5154
p.platform = ""
5255
p.undecided = true
5356
}

browser.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ func (p *UserAgent) detectBrowser(sections []section) {
9393
}
9494
// It's possible the google-bot emulates these now
9595
for _, comment := range engine.comment {
96-
if len(comment) > 5 && strings.HasPrefix(comment, "Googlebot") {
96+
if len(comment) > 5 &&
97+
(strings.HasPrefix(comment, "Googlebot") || strings.HasPrefix(comment, "bingbot")) {
9798
p.undecided = true
9899
break
99100
}

operating_systems.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func webkit(p *UserAgent, comment []string) {
8989
if len(comment) > 3 {
9090
p.localization = comment[3]
9191
} else if len(comment) == 3 {
92-
_ = p.googleBot()
92+
_ = p.googleOrBingBot()
9393
}
9494
} else if len(comment) > 0 {
9595
if len(comment) > 3 {
@@ -100,7 +100,7 @@ func webkit(p *UserAgent, comment []string) {
100100
} else if len(comment) < 2 {
101101
p.localization = comment[0]
102102
} else if len(comment) < 3 {
103-
if !p.googleBot() {
103+
if !p.googleOrBingBot() {
104104
p.os = normalizeOS(comment[1])
105105
}
106106
} else {

0 commit comments

Comments
 (0)