import time from iwla import IWLA from iplugin import IPlugin from display import * class IWLADisplayReferers(IPlugin): def __init__(self, iwla): super(IWLADisplayReferers, self).__init__(iwla) self.API_VERSION = 1 self.requires = ['IWLAPostAnalysisReferers'] def hook(self): 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', {}) 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) # Top referers in index index = self.iwla.getDisplayIndex() table = DisplayHTMLBlockTable('Connexion from', ['Origin', 'Pages', 'Hits']) table.appendRow(['Search Engine', '', '']) for r,_ in top_search_engine_referers[:10]: row = [r, search_engine_referers[r]['pages'], search_engine_referers[r]['hits']] table.appendRow(row) table.appendRow(['External URL', '', '']) for r,_ in top_referers[:10]: row = [r, referers[r]['pages'], referers[r]['hits']] table.appendRow(row) table.appendRow(['External URL (robot)', '', '']) for r,_ in top_robots_referers[:10]: row = [r, robots_referers[r]['pages'], robots_referers[r]['hits']] table.appendRow(row) index.appendBlock(table) # All referers in a file cur_time = self.iwla.getCurTime() title = time.strftime('Connexion from - %B %Y', cur_time) filename = 'referers.html' path = self.iwla.getCurDisplayPath(filename) page = DisplayHTMLPage(title, path) table = DisplayHTMLBlockTable('Connexion from', ['Origin', 'Pages', 'Hits']) table.appendRow(['Search Engine', '', '']) for r,_ in top_search_engine_referers: row = [r, search_engine_referers[r]['pages'], search_engine_referers[r]['hits']] table.appendRow(row) table.appendRow(['External URL', '', '']) for r,_ in top_referers: row = [r, referers[r]['pages'], referers[r]['hits']] table.appendRow(row) table.appendRow(['External URL (robot)', '', '']) for r,_ in top_robots_referers: row = [r, robots_referers[r]['pages'], robots_referers[r]['hits']] table.appendRow(row) page.appendBlock(table) display = self.iwla.getDisplay() display.addPage(page) block = DisplayHTMLRawBlock() block.setRawHTML('All referers' % (filename)) index.appendBlock(block) # Top key phrases in index table = DisplayHTMLBlockTable('Top key phrases', ['Key phrase', 'Search']) for phrase in top_key_phrases[:10]: table.appendRow([phrase[0], phrase[1]]) index.appendBlock(table) # All key phrases in a file title = time.strftime('Key Phrases - %B %Y', cur_time) filename = 'key_phrases.html' path = self.iwla.getCurDisplayPath(filename) page = DisplayHTMLPage(title, path) table = DisplayHTMLBlockTable('Top key phrases', ['Key phrase', 'Search']) for phrase in top_key_phrases: table.appendRow([phrase[0], phrase[1]]) page.appendBlock(table) display.addPage(page) block = DisplayHTMLRawBlock() block.setRawHTML('All key phrases' % (filename)) index.appendBlock(block)