iwla

iwla Git Source Tree

Root/plugins/pre_analysis/robots.py

1import re
2
3from iwla import IWLA
4from iplugin import IPlugin
5
6import awstats_data
7
8#
9# Pre analysis hook
10#
11# Filter robots
12#
13# Plugin requirements :
14# None
15#
16# Conf values needed :
17# page_to_hit_conf*
18# hit_to_page_conf*
19#
20# Output files :
21# None
22#
23# Statistics creation :
24# None
25#
26# Statistics update :
27# visits :
28# remote_addr =>
29# robot
30#
31# Statistics deletion :
32# None
33#
34
35class IWLAPreAnalysisRobots(IPlugin):
36 def __init__(self, iwla):
37 super(IWLAPreAnalysisRobots, self).__init__(iwla)
38 self.API_VERSION = 1
39
40 def load(self):
41 self.awstats_robots = map(lambda (x) : re.compile(('.*%s.*') % (x), re.IGNORECASE), awstats_data.robots)
42
43 return True
44
45# Basic rule to detect robots
46 def hook(self):
47 hits = self.iwla.getCurrentVisists()
48 for (k, super_hit) in hits.items():
49 if super_hit['robot']: continue
50
51 isRobot = False
52 referers = 0
53
54 first_page = super_hit['requests'][0]
55 if not self.iwla.isValidForCurrentAnalysis(first_page): continue
56
57 for r in self.awstats_robots:
58 if r.match(first_page['http_user_agent']):
59 isRobot = True
60 break
61
62 if isRobot:
63 super_hit['robot'] = 1
64 continue
65
66# 1) no pages view --> robot
67 # if not super_hit['viewed_pages']:
68 # super_hit['robot'] = 1
69 # continue
70
71# 2) pages without hit --> robot
72 if not super_hit['viewed_hits']:
73 super_hit['robot'] = 1
74 continue
75
76 for hit in super_hit['requests']:
77# 3) /robots.txt read
78 if hit['extract_request']['http_uri'] == '/robots.txt':
79 isRobot = True
80 break
81
82# 4) Any referer for hits
83 if not hit['is_page'] and hit['http_referer']:
84 referers += 1
85
86 if isRobot:
87 super_hit['robot'] = 1
88 continue
89
90 if not super_hit['viewed_pages'] and \
91 (super_hit['viewed_hits'] and not referers):
92 super_hit['robot'] = 1
93 continue

Archive Download this file

Branches

Tags