2014-12-18 19:54:31 +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/>.
|
|
|
|
#
|
|
|
|
|
2014-11-26 16:17:16 +01:00
|
|
|
from iwla import IWLA
|
|
|
|
from iplugin import IPlugin
|
|
|
|
from display import *
|
|
|
|
|
2014-12-10 21:15:56 +01:00
|
|
|
#
|
|
|
|
# Display hook
|
|
|
|
#
|
|
|
|
# Create Referers page
|
|
|
|
#
|
|
|
|
# Plugin requirements :
|
|
|
|
# post_analysis/referers
|
|
|
|
#
|
|
|
|
# Conf values needed :
|
2014-12-11 21:13:24 +01:00
|
|
|
# max_referers_displayed*
|
|
|
|
# create_all_referers_page*
|
|
|
|
# max_key_phrases_displayed*
|
|
|
|
# create_all_key_phrases_page*
|
2014-12-10 21:15:56 +01:00
|
|
|
#
|
|
|
|
# Output files :
|
|
|
|
# OUTPUT_ROOT/year/month/referers.html
|
|
|
|
# OUTPUT_ROOT/year/month/key_phrases.html
|
|
|
|
# OUTPUT_ROOT/year/month/index.html
|
|
|
|
#
|
|
|
|
# Statistics creation :
|
|
|
|
# None
|
|
|
|
#
|
|
|
|
# Statistics update :
|
|
|
|
# None
|
|
|
|
#
|
|
|
|
# Statistics deletion :
|
|
|
|
# None
|
|
|
|
#
|
|
|
|
|
2014-11-26 16:17:16 +01:00
|
|
|
class IWLADisplayReferers(IPlugin):
|
|
|
|
def __init__(self, iwla):
|
|
|
|
super(IWLADisplayReferers, self).__init__(iwla)
|
|
|
|
self.API_VERSION = 1
|
2014-11-26 21:06:36 +01:00
|
|
|
self.requires = ['IWLAPostAnalysisReferers']
|
2014-12-11 21:13:24 +01:00
|
|
|
self.max_referers = self.iwla.getConfValue('max_referers_displayed', 0)
|
|
|
|
self.create_all_referers = self.iwla.getConfValue('create_all_referers_page', True)
|
|
|
|
self.max_key_phrases = self.iwla.getConfValue('max_key_phrases_displayed', 0)
|
|
|
|
self.create_all_key_phrases = self.iwla.getConfValue('create_all_key_phrases_page', True)
|
2014-11-26 16:17:16 +01:00
|
|
|
|
2014-11-26 20:31:13 +01:00
|
|
|
def hook(self):
|
2014-12-08 14:13:26 +01:00
|
|
|
display = self.iwla.getDisplay()
|
2014-11-26 21:06:36 +01:00
|
|
|
month_stats = self.iwla.getMonthStats()
|
|
|
|
referers = month_stats.get('referers', {})
|
|
|
|
robots_referers = month_stats.get('robots_referers', {})
|
|
|
|
search_engine_referers = month_stats.get('search_engine_referers', {})
|
|
|
|
key_phrases = month_stats.get('key_phrases', {})
|
2014-11-26 16:17:16 +01:00
|
|
|
|
|
|
|
top_referers = [(k, referers[k]['pages']) for k in referers.keys()]
|
|
|
|
top_referers = sorted(top_referers, key=lambda t: t[1], reverse=True)
|
|
|
|
|
|
|
|
top_robots_referers = [(k, robots_referers[k]['pages']) for k in robots_referers.keys()]
|
|
|
|
top_robots_referers = sorted(top_robots_referers, key=lambda t: t[1], reverse=True)
|
|
|
|
|
|
|
|
top_search_engine_referers = [(k, search_engine_referers[k]['pages']) for k in search_engine_referers.keys()]
|
|
|
|
top_search_engine_referers = sorted(top_search_engine_referers, key=lambda t: t[1], reverse=True)
|
|
|
|
|
|
|
|
top_key_phrases = key_phrases.items()
|
|
|
|
top_key_phrases = sorted(top_key_phrases, key=lambda t: t[1], reverse=True)
|
|
|
|
|
2014-11-27 19:38:41 +01:00
|
|
|
cur_time = self.iwla.getCurTime()
|
2014-11-26 16:17:16 +01:00
|
|
|
index = self.iwla.getDisplayIndex()
|
|
|
|
|
|
|
|
# All referers in a file
|
2014-12-11 21:13:24 +01:00
|
|
|
if self.create_all_referers:
|
2014-12-17 20:31:59 +01:00
|
|
|
title = createCurTitle(self.iwla, u'Connexion from')
|
2014-12-11 21:13:24 +01:00
|
|
|
|
|
|
|
filename = 'referers.html'
|
|
|
|
path = self.iwla.getCurDisplayPath(filename)
|
|
|
|
|
|
|
|
page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
|
2014-12-17 20:31:59 +01:00
|
|
|
table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Connexion from'), [self.iwla._(u'Origin'), self.iwla._(u'Pages'), self.iwla._(u'Hits')])
|
2014-12-11 21:13:24 +01:00
|
|
|
table.setColsCSSClass(['', 'iwla_page', 'iwla_hit'])
|
|
|
|
|
|
|
|
total_search = [0]*3
|
2014-12-17 20:31:59 +01:00
|
|
|
table.appendRow(['<b>%s</b>' % (self.iwla._(u'Search Engine')), '', ''])
|
2014-12-11 21:13:24 +01:00
|
|
|
new_list = self.max_referers and top_search_engine_referers[:self.max_referers] or top_search_engine_referers
|
|
|
|
for r,_ in new_list:
|
|
|
|
row = [r, search_engine_referers[r]['pages'], search_engine_referers[r]['hits']]
|
|
|
|
total_search[1] += search_engine_referers[r]['pages']
|
|
|
|
total_search[2] += search_engine_referers[r]['hits']
|
|
|
|
table.appendRow(row)
|
2014-12-17 21:27:31 +01:00
|
|
|
if self.max_referers:
|
|
|
|
others = 0
|
|
|
|
for (uri, entrance) in top_referers[self.max_referers:]:
|
|
|
|
others += entrance
|
|
|
|
table.appendRow([self.iwla._(u'Others'), others])
|
|
|
|
table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
|
2014-12-11 21:13:24 +01:00
|
|
|
|
|
|
|
total_external = [0]*3
|
2014-12-17 20:31:59 +01:00
|
|
|
table.appendRow(['<b>%s</b>' % (self.iwla._(u'External URL')), '', ''])
|
2014-12-11 21:13:24 +01:00
|
|
|
new_list = self.max_referers and top_referers[:self.max_referers] or top_referers
|
|
|
|
for r,_ in new_list:
|
|
|
|
row = [generateHTMLLink(r), referers[r]['pages'], referers[r]['hits']]
|
|
|
|
total_external[1] += referers[r]['pages']
|
|
|
|
total_external[2] += referers[r]['hits']
|
|
|
|
table.appendRow(row)
|
2014-12-17 21:27:31 +01:00
|
|
|
if self.max_referers:
|
|
|
|
others = 0
|
|
|
|
for (uri, entrance) in top_referers[self.max_referers:]:
|
|
|
|
others += entrance
|
|
|
|
table.appendRow([self.iwla._(u'Others'), others])
|
|
|
|
table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
|
2014-12-11 21:13:24 +01:00
|
|
|
|
|
|
|
total_robot = [0]*3
|
2014-12-17 20:31:59 +01:00
|
|
|
table.appendRow(['<b>%s</b>' % (self.iwla._(u'External URL (robot)')), '', ''])
|
2014-12-11 21:13:24 +01:00
|
|
|
new_list = self.max_referers and top_robots_referers[:self.max_referers] or top_robots_referers
|
|
|
|
for r,_ in new_list:
|
|
|
|
row = [generateHTMLLink(r), robots_referers[r]['pages'], robots_referers[r]['hits']]
|
|
|
|
total_robot[1] += robots_referers[r]['pages']
|
|
|
|
total_robot[2] += robots_referers[r]['hits']
|
|
|
|
table.appendRow(row)
|
2014-12-17 21:27:31 +01:00
|
|
|
if self.max_referers:
|
|
|
|
others = 0
|
|
|
|
for (uri, entrance) in top_referers[self.max_referers:]:
|
|
|
|
others += entrance
|
|
|
|
table.appendRow([self.iwla._(u'Others'), others])
|
|
|
|
table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
|
2014-12-11 21:13:24 +01:00
|
|
|
|
|
|
|
page.appendBlock(table)
|
|
|
|
|
|
|
|
display.addPage(page)
|
|
|
|
|
2014-12-17 20:31:59 +01:00
|
|
|
title = self.iwla._(u'Top Referers')
|
2014-12-11 21:13:24 +01:00
|
|
|
if self.create_all_referers:
|
2014-12-17 20:31:59 +01:00
|
|
|
link = '<a href=\'%s\'>%s</a>' % (filename, self.iwla._(u'All Referers'))
|
2014-12-11 21:13:24 +01:00
|
|
|
title = '%s - %s' % (title, link)
|
2014-11-27 19:38:41 +01:00
|
|
|
|
|
|
|
# Top referers in index
|
2014-12-17 20:31:59 +01:00
|
|
|
table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'Origin'), self.iwla._(u'Pages'), self.iwla._(u'Hits')])
|
2014-11-27 21:40:23 +01:00
|
|
|
table.setColsCSSClass(['', 'iwla_page', 'iwla_hit'])
|
|
|
|
|
2014-12-17 20:31:59 +01:00
|
|
|
table.appendRow(['<b>%s</b>' % (self.iwla._(u'Search Engine')), '', ''])
|
2014-11-27 19:38:41 +01:00
|
|
|
for r,_ in top_search_engine_referers[:10]:
|
|
|
|
row = [r, search_engine_referers[r]['pages'], search_engine_referers[r]['hits']]
|
2014-12-05 16:03:09 +01:00
|
|
|
total_search[1] -= search_engine_referers[r]['pages']
|
|
|
|
total_search[2] -= search_engine_referers[r]['hits']
|
2014-11-27 19:38:41 +01:00
|
|
|
table.appendRow(row)
|
2014-12-05 16:03:09 +01:00
|
|
|
if total_search[1] or total_search[2]:
|
2014-12-17 20:31:59 +01:00
|
|
|
total_search[0] = self.iwla._(u'Others')
|
2014-12-05 16:03:09 +01:00
|
|
|
table.appendRow(total_search)
|
|
|
|
table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
|
2014-11-27 19:38:41 +01:00
|
|
|
|
2014-12-17 20:31:59 +01:00
|
|
|
table.appendRow(['<b>%s</b>' % (self.iwla._(u'External URL')), '', ''])
|
2014-11-27 19:38:41 +01:00
|
|
|
for r,_ in top_referers[:10]:
|
2014-12-04 21:04:41 +01:00
|
|
|
row = [generateHTMLLink(r), referers[r]['pages'], referers[r]['hits']]
|
2014-12-05 16:03:09 +01:00
|
|
|
total_external[1] -= referers[r]['pages']
|
|
|
|
total_external[2] -= referers[r]['hits']
|
2014-11-27 19:38:41 +01:00
|
|
|
table.appendRow(row)
|
2014-12-05 16:03:09 +01:00
|
|
|
if total_external[1] or total_external[2]:
|
2014-12-17 20:31:59 +01:00
|
|
|
total_external[0] = self.iwla._(u'Others')
|
2014-12-05 16:03:09 +01:00
|
|
|
table.appendRow(total_external)
|
|
|
|
table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
|
2014-11-27 19:38:41 +01:00
|
|
|
|
2014-12-17 20:31:59 +01:00
|
|
|
table.appendRow(['<b>%s</b>' % (self.iwla._(u'External URL (robot)')), '', ''])
|
2014-11-27 19:38:41 +01:00
|
|
|
for r,_ in top_robots_referers[:10]:
|
2014-12-04 21:04:41 +01:00
|
|
|
row = [generateHTMLLink(r), robots_referers[r]['pages'], robots_referers[r]['hits']]
|
2014-12-05 16:03:09 +01:00
|
|
|
total_robot[1] -= robots_referers[r]['pages']
|
|
|
|
total_robot[2] -= robots_referers[r]['hits']
|
2014-11-27 19:38:41 +01:00
|
|
|
table.appendRow(row)
|
2014-12-05 16:03:09 +01:00
|
|
|
if total_robot[1] or total_robot[2]:
|
2014-12-17 20:31:59 +01:00
|
|
|
total_robot[0] = self.iwla._(u'Others')
|
2014-12-05 16:03:09 +01:00
|
|
|
table.appendRow(total_robot)
|
|
|
|
table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
|
2014-11-26 16:17:16 +01:00
|
|
|
|
|
|
|
index.appendBlock(table)
|
|
|
|
|
|
|
|
# All key phrases in a file
|
2014-12-11 21:13:24 +01:00
|
|
|
if self.create_all_key_phrases:
|
2014-12-17 20:31:59 +01:00
|
|
|
title = createCurTitle(self.iwla, u'Key Phrases')
|
2014-11-26 16:17:16 +01:00
|
|
|
|
2014-12-11 21:13:24 +01:00
|
|
|
filename = 'key_phrases.html'
|
|
|
|
path = self.iwla.getCurDisplayPath(filename)
|
2014-11-26 16:17:16 +01:00
|
|
|
|
2014-12-11 21:13:24 +01:00
|
|
|
total_search = [0]*2
|
|
|
|
page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
|
2014-12-17 20:31:59 +01:00
|
|
|
table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'Top key phrases'), [self.iwla._(u'Key phrase'), self.iwla._(u'Search')])
|
2014-12-11 21:13:24 +01:00
|
|
|
table.setColsCSSClass(['', 'iwla_search'])
|
|
|
|
new_list = self.max_key_phrases and top_key_phrases[:self.max_key_phrases] or top_key_phrases
|
|
|
|
for phrase in new_list:
|
|
|
|
table.appendRow([phrase[0], phrase[1]])
|
|
|
|
total_search[1] += phrase[1]
|
|
|
|
page.appendBlock(table)
|
2014-12-04 21:04:41 +01:00
|
|
|
|
2014-12-11 21:13:24 +01:00
|
|
|
display.addPage(page)
|
2014-11-26 19:33:08 +01:00
|
|
|
|
2014-12-17 20:31:59 +01:00
|
|
|
title = self.iwla._(u'Top key phrases')
|
2014-12-11 21:13:24 +01:00
|
|
|
if self.create_all_key_phrases:
|
2014-12-17 20:31:59 +01:00
|
|
|
link = '<a href=\'%s\'>%s</a>' % (filename, self.iwla._(u'All key phrases'))
|
2014-12-11 21:13:24 +01:00
|
|
|
title = '%s - %s' % (title, link)
|
2014-11-27 19:38:41 +01:00
|
|
|
|
|
|
|
# Top key phrases in index
|
2014-12-17 20:31:59 +01:00
|
|
|
table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'Key phrase'), self.iwla._(u'Search')])
|
2014-11-27 21:40:23 +01:00
|
|
|
table.setColsCSSClass(['', 'iwla_search'])
|
2014-11-27 19:38:41 +01:00
|
|
|
for phrase in top_key_phrases[:10]:
|
|
|
|
table.appendRow([phrase[0], phrase[1]])
|
2014-12-05 16:03:09 +01:00
|
|
|
total_search[1] -= phrase[1]
|
|
|
|
if total_search[1]:
|
2014-12-17 20:31:59 +01:00
|
|
|
total_search[0] = self.iwla._(u'Others')
|
2014-12-05 16:03:09 +01:00
|
|
|
table.appendRow(total_search)
|
|
|
|
table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
|
2014-11-27 19:38:41 +01:00
|
|
|
index.appendBlock(table)
|