Merge branch 'dev' of soutade.fr:iwla into dev

This commit is contained in:
Gregory Soutade 2015-05-14 09:55:29 +02:00
commit c7daad23c8
2 changed files with 48 additions and 12 deletions

38
iwla.py
View File

@ -720,6 +720,38 @@ class IWLA(object):
else:
self.logger.info('==> Analyse not started : nothing new')
class FileIter(object):
def __init__(self, filenames):
self.filenames = [f for f in filenames.split(',') if f]
for f in self.filenames:
if not os.path.exists(f):
print 'No such file \'%s\'' % (f)
sys.exit(-1)
self.cur_file = None
self._openNextFile()
def __iter__(self):
return self
def __next__(self):
return self.next()
def _openNextFile(self):
if self.cur_file:
self.cur_file.close()
self.cur_file = None
if not self.filenames:
raise StopIteration()
self.cur_file = open(self.filenames.pop(0))
def next(self):
l = self.cur_file.readline()
if not l:
self._openNextFile()
l = self.cur_file.readline()
return l[:-1]
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Intelligent Web Log Analyzer')
@ -775,8 +807,4 @@ if __name__ == '__main__':
iwla.start(sys.stdin)
else:
filename = args.file or conf.analyzed_filename
if not os.path.exists(filename):
print 'No such file \'%s\'' % (filename)
sys.exit(-1)
with open(filename) as f:
iwla.start(f)
iwla.start(FileIter(filename))

View File

@ -19,6 +19,7 @@
#
import re
import logging
from iwla import IWLA
from iplugin import IPlugin
@ -61,13 +62,20 @@ class IWLAPreAnalysisRobots(IPlugin):
self.awstats_robots = map(lambda (x) : re.compile(('.*%s.*') % (x), re.IGNORECASE), awstats_data.robots)
self.robot_re = re.compile(r'.*bot.*', re.IGNORECASE)
self.crawl_re = re.compile(r'.*crawl.*', re.IGNORECASE)
self.logger = logging.getLogger(self.__class__.__name__)
return True
def _setRobot(self, k, super_hit):
self.logger.debug('%s is a robot' % (k))
super_hit['robot'] = 1
# Basic rule to detect robots
def hook(self):
hits = self.iwla.getCurrentVisists()
for (k, super_hit) in hits.items():
if super_hit['robot']: continue
if super_hit['robot']:
self.logger.debug('%s is a robot' % (k))
continue
isRobot = False
referers = 0
@ -76,7 +84,7 @@ class IWLAPreAnalysisRobots(IPlugin):
if self.robot_re.match(first_page['http_user_agent']) or\
self.crawl_re.match(first_page['http_user_agent']):
super_hit['robot'] = 1
self._setRobot(k, super_hit)
continue
for r in self.awstats_robots:
@ -85,7 +93,7 @@ class IWLAPreAnalysisRobots(IPlugin):
break
if isRobot:
super_hit['robot'] = 1
self._setRobot(k, super_hit)
continue
# 1) no pages view --> robot
@ -95,13 +103,13 @@ class IWLAPreAnalysisRobots(IPlugin):
# 2) pages without hit --> robot
if not super_hit['viewed_hits']:
super_hit['robot'] = 1
self._setRobot(k, super_hit)
continue
for hit in super_hit['requests']:
# 3) /robots.txt read
if hit['extract_request']['http_uri'].endswith('/robots.txt'):
isRobot = True
self._setRobot(k, super_hit)
break
# 4) Any referer for hits
@ -109,10 +117,10 @@ class IWLAPreAnalysisRobots(IPlugin):
referers += 1
if isRobot:
super_hit['robot'] = 1
self._setRobot(k, super_hit)
continue
if not super_hit['viewed_pages'] and \
(super_hit['viewed_hits'] and not referers):
super_hit['robot'] = 1
self._setRobot(k, super_hit)
continue