-
Notifications
You must be signed in to change notification settings - Fork 6
/
RobotParser.py
116 lines (94 loc) · 3.71 KB
/
RobotParser.py
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
#!/usr/bin/env python3
# Author: Emmanuel Odeke <[email protected]>
import time
import utils
firstLetterCompile = utils.regexCompile('([a-z])')
class RobotParser:
def __init__(self):
self.__initTime__ = time.time()
self.__rulesDict__ = dict()
def addRobotRule(self, url):
topDomain = utils.getTopDomain(url)
if topDomain:
robotPath = utils.robotsTxt(topDomain)
def parseRobotFile(self, domain, robotFile):
if not robotFile: # Avoid urls without robots.txt -- debatable issue.
return None
splitL = robotFile.split('\n')
spLen = len(splitL)
tokenCreator = lambda v, s=':': tuple(map(lambda a: a.strip(' '), v.split(s)))
domainAllows = {}
domainDisAllows = {}
for i in range(spLen):
line = splitL[i]
if (not line) or line[0] == '#':
continue
attrs = tokenCreator(line)
if attrs and attrs[0] == 'User-agent':
clausePresent = (attrs[1] == utils.CRAWLER_NAME or attrs[1] == '*')
if not clausePresent:
continue
i += 1
while i < spLen:
l = splitL[i]
cont = tokenCreator(l)
if cont[0] == 'User-agent':
break
i += 1
if (not l) or l[0] == '#':
continue
selector = domainDisAllows
if cont[0] == 'Allow':
selector = domainAllows
elif not (cont[0] == 'Disallow' and cont[1]):
continue
firstCh = firstLetterCompile.search(cont[1])
key = '*'
if firstCh:
key = firstCh.groups(1)[0]
try:
selector.setdefault(key, []).append(utils.regexCompile(cont[1]))
except:
pass
self.__rulesDict__[domain] = {'allow': domainAllows, 'disallow': domainDisAllows}
return True
def canVisit(self, url):
topDomain = utils.getTopDomain(url)
retrRules = self.__rulesDict__.get(topDomain, None)
if retrRules is None: # Cache miss
robotsUrl = utils.robotsTxt(url)
roboFileBuf = utils.dlAndDecode(robotsUrl)
if not self.parseRobotFile(topDomain, roboFileBuf):
return False
retr = self.__rulesDict__[topDomain]
sp = tuple(filter(lambda a: a, url.split(topDomain)))
if sp:
firstCh = firstLetterCompile.search(sp[0])
if firstCh:
# Time to probe
fCh = firstCh.groups(1)[0]
retr = self.__rulesDict__[topDomain]['disallow']
compList = retr.get(fCh, None)
if compList:
for comp in compList:
if comp.search(sp[0]):
return False
return True
return True
def getRules(self):
return self.__rulesDict__
def popRobotRule(self, url):
pass
def editRobotRule(self, url):
pass
def main():
rb = RobotParser()
qList = [
'http://cnn.com/', 'http://time.com/time',
'http://www.cnn.com/2014/07/14/showbiz/music/unlocking-the-truth-sony-record-deal/index.html?hpt=us_t3',
'http://www.google.com/search', 'http://www.google.com/maps/ukraine', 'https://www.youtube.com/watch?v=Ei8nL3SvRSY'
]
for q in qList:
print(q, rb.canVisit(q))
if __name__ == '__main__':
main()