iwla/plugins/post_analysis/browsers.py

133 lines
3.4 KiB
Python
Raw Normal View History

2015-01-08 21:01:33 +01:00
# -*- 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
"""
2023-04-18 20:30:40 +02:00
browser_order = ['android', 'iphone', 'xbox', 'edge', 'opera', 'chrome', 'safari', 'firefox', 'ie', 'mozilla', 'curl', 'wget', 'w3m']
browser_hashid = {
'android':'Android',
'iphone':'iPhone',
'edge':'Edg',
2023-04-18 20:30:40 +02:00
'chrome':['Chrom', 'Chrome'],
'safari':'Safari',
'firefox':'Firefox',
2023-04-18 20:30:40 +02:00
'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',
2023-04-18 20:30:40 +02:00
'ie':'Internet Explorer',
'mozilla':'Mozilla',
'opera':'Opera',
'xbox':'Xbox',
'curl':'Curl',
'wget':'Wget',
'w3m':'w3m'
}
2015-01-08 21:01:33 +01:00
class IWLAPostAnalysisBrowsers(IPlugin):
2015-01-08 21:01:33 +01:00
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:
2023-04-18 20:30:40 +02:00
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
2015-01-08 21:01:33 +01:00
break
else:
name = super_hit['browser']
2015-01-08 21:01:33 +01:00
if not name in browsers_stats.keys():
browsers_stats[name] = 1
2015-01-08 21:01:33 +01:00
else:
browsers_stats[name] += 1
2015-01-08 21:01:33 +01:00
month_stats['browsers'] = browsers_stats