forked from xojoc/useragent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawler.go
111 lines (93 loc) · 2.16 KB
/
crawler.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
// Written by https://xojoc.pw. GPLv3 or later.
package useragent
import (
"net/url"
)
// Keep them sorted
var crawlers = map[string]*url.URL{
"Google AdsBot": u("https://support.google.com/webmasters/answer/1061943"),
"Google AdSense": u("https://support.google.com/webmasters/answer/1061943"),
"Googlebot": u("http://www.google.com/bot.html"),
"Googlebot Images": u("https://support.google.com/webmasters/answer/1061943"),
"Googlebot News": u("https://support.google.com/news/publisher/answer/93977"),
"Googlebot Video": u("https://support.google.com/webmasters/answer/1061943"),
}
func parseCrawler(l *lex) *UserAgent {
for _, f := range []parseFn{parseGooglebot, parseGooglebotSmartphone} {
if ua := f(newLex(l.s)); ua != nil {
return ua
}
}
return nil
}
func parseGooglebot(l *lex) *UserAgent {
ua := new()
ua.Type = Crawler
// Alternate Googlebots
if l.match("Googlebot") {
if l.match("-News") {
ua.Name = "Googlebot News"
} else if parseNameVersion(l, ua) {
switch ua.Name {
case "":
ua.Name = "Googlebot"
case "-Image":
ua.Name = "Googlebot Images"
default:
ua.Name = "Googlebot " + ua.Name[1:]
}
} else {
return nil
}
return ua
} else if l.match("Mediapartners-Google") {
ua.Name = "Google AdSense"
return ua
} else if l.match("AdsBot-Google") {
ua.Name = "Google AdsBot"
return ua
}
// Googlebot
if !l.match("Mozilla/5.0 (compatible; Googlebot/") {
return nil
}
ua.Name = "Googlebot"
if !parseVersion(l, ua, ";") {
return nil
}
if !l.match(" +http://www.google.com/bot.html)") {
return nil
}
return ua
}
func parseGooglebotSmartphone(l *lex) *UserAgent {
ua := new()
if _, ok := l.span("Mozilla"); !ok {
return nil
}
if _, ok := l.span("Linux"); !ok {
return nil
}
if _, ok := l.span("Android"); !ok {
return nil
}
if _, ok := l.span("AppleWebKit"); !ok {
return nil
}
if _, ok := l.span("Chrome"); !ok {
return nil
}
if _, ok := l.span("Mobile Safari"); !ok {
return nil
}
if _, ok := l.span("Googlebot/"); !ok {
return nil
}
if !parseVersion(l, ua, ";") {
return nil
}
ua.Type = Crawler
ua.Name = "Googlebot"
ua.Mobile = true
return ua
}