iwla/plugins/post_analysis/browsers.py

133 lines
3.4 KiB
Python

# -*- coding: utf-8 -*-
#
# Copyright Grégory Soutadé 2015
# This file is part of iwla
# iwla is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# iwla is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with iwla. If not, see <http://www.gnu.org/licenses/>.
#
import re
from iwla import IWLA
from iplugin import IPlugin
"""
Post analysis hook
Detect browser information from requests
Plugin requirements :
None
Conf values needed :
None
Output files :
None
Statistics creation :
visits :
remote_addr =>
browser
month_stats :
browsers =>
browser => count
Statistics update :
None
Statistics deletion :
None
"""
browser_order = ['android', 'iphone', 'xbox', 'edge', 'opera', 'chrome', 'safari', 'firefox', 'ie', 'mozilla', 'curl', 'wget', 'w3m']
browser_hashid = {
'android':'Android',
'iphone':'iPhone',
'edge':'Edg',
'chrome':['Chrom', 'Chrome'],
'safari':'Safari',
'firefox':'Firefox',
'ie':'MSIE',
'mozilla':'Mozilla',
'opera':'OPR',
'xbox':'Xbox',
'curl':'curl',
'wget':'Wget',
'w3m':'w3m'
}
browser_name = {
'android':'Android',
'iphone':'iPhone',
'edge':'Edge',
'chrome':'Chrome',
'safari':'Safari',
'firefox':'Firefox',
'ie':'Internet Explorer',
'mozilla':'Mozilla',
'opera':'Opera',
'xbox':'Xbox',
'curl':'Curl',
'wget':'Wget',
'w3m':'w3m'
}
class IWLAPostAnalysisBrowsers(IPlugin):
def hook(self):
stats = self.iwla.getValidVisitors()
month_stats = self.iwla.getMonthStats()
browsers = month_stats.get('browsers', {})
browsers_stats = {}
for (k, super_hit) in stats.items():
if not 'browser' in super_hit:
for r in super_hit['requests'][::-1]:
user_agent = r['http_user_agent']
if not user_agent: continue
name = 'Unknown'
for browser in browser_order:
reference = browser_hashid[browser]
if type(reference) == list:
for ref in reference:
if ref in user_agent:
name = browser_name[browser]
break
if name != 'Unknown':
break
else:
if browser_hashid[browser] in user_agent:
name = browser_name[browser]
break
if name == 'Unknown' and 'Macintosh' in user_agent:
name = 'Safari'
super_hit['browser'] = name
break
else:
name = super_hit['browser']
if not name in browsers_stats.keys():
browsers_stats[name] = 1
else:
browsers_stats[name] += 1
month_stats['browsers'] = browsers_stats