-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.cpp
116 lines (94 loc) · 2.7 KB
/
bot.cpp
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
//
// Created by tyy on 2017/1/5.
//
#include <iostream>
#include <cstdlib>
#include <vector>
#include <unordered_map>
#include <boost/regex.hpp>
#include <string>
#include <cstring>
#include "userAgent.h"
boost::regex botFromSiteRegexp{"http://.+\\.\\w+"};
boost::regex botRegexp{"(?i)(bot|crawler|(s|S)p(i|y)der|search|worm|fetch|nutch)"};
string getFromSite(vector<string>& comment) {
size_t slen = comment.size();
if (slen == 0) {
return "";
}
int idx = (slen < 3) ? 0 : 2;
boost::smatch result;
string ret = "";
int count = 0;
std::string::const_iterator start = comment[idx].begin();
std::string::const_iterator end = comment[idx].end();
while ( boost::regex_search(start, end, result, botFromSiteRegexp) ) {
if (count++ == 0) {
ret = result[0];
}
start = result[0].second;
}
if (count == 1) {
if (idx == 0) {
return ret;
}
return trim_copy(comment[1]);
}
return "";
}
bool googleBot(UserAgent& p) {
if (contains(p.ua, "Googlebot")) {
p.platform = "";
p.undecided = true;
}
return p.undecided;
}
void setSimple(UserAgent& p, string name, string version, bool bot) {
p.bot = bot;
if(!bot) {
p.mozilla = "";
}
p.browser.Name = replace_all_copy(name, "\\", "");
p.browser.Version = version;
p.browser.Engine = "";
p.browser.EngineVersion = "";
p.os = "";
p.localization = "";
}
void fixOther(UserAgent& p, vector<Section>& sections, int slen) {
if (slen > 0) {
p.browser.Name = sections[0].name;
p.browser.Version = sections[0].version;
p.mozilla = "";
}
}
void checkBot(UserAgent& p, vector<Section>& sections, int slen) {
if (slen == 1 && sections[0].name != "Mozilla") {
p.mozilla = "";
if (boost::regex_search(sections[0].name, botRegexp)) {
setSimple(p, sections[0].name, "", true);
return;
}
string name = getFromSite(sections[0].comment);
if (name != "") {
setSimple(p, sections[0].name, sections[0].version, true);
return;
}
setSimple(p, sections[0].name, sections[0].version, false);
} else {
for (int i = 0; i < slen; i++) {
string name = getFromSite(sections[i].comment);
if (name != "") {
vector<string> s;
split(s, name, is_any_of("/"));
string version = "";
if (s.size() == 2) {
version = s[1];
}
setSimple(p, s[0], version, true);
return;
}
}
fixOther(p, sections, slen);
}
}