iwla/plugins/display/top_downloads.py
2014-12-30 16:21:49 +01:00

107 lines
3.7 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/>.
#
from iwla import IWLA
from iplugin import IPlugin
from display import *
"""
Display hook
Create TOP downloads page
Plugin requirements :
post_analysis/top_downloads
Conf values needed :
max_downloads_displayed*
create_all_downloads_page*
Output files :
OUTPUT_ROOT/year/month/top_downloads.html
OUTPUT_ROOT/year/month/index.html
Statistics creation :
None
Statistics update :
None
Statistics deletion :
None
"""
class IWLADisplayTopDownloads(IPlugin):
def __init__(self, iwla):
super(IWLADisplayTopDownloads, self).__init__(iwla)
self.API_VERSION = 1
self.requires = ['IWLAPostAnalysisTopDownloads']
self.max_downloads = self.iwla.getConfValue('max_downloads_displayed', 0)
self.create_all_downloads = self.iwla.getConfValue('create_all_downloads_page', True)
def hook(self):
display = self.iwla.getDisplay()
top_downloads = self.iwla.getMonthStats()['top_downloads']
top_downloads = sorted(top_downloads.items(), key=lambda t: t[1], reverse=True)
# All in a file
if self.create_all_downloads:
filename = 'top_downloads.html'
path = self.iwla.getCurDisplayPath(filename)
title = createCurTitle(self.iwla, u'All Downloads')
page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
table = display.createBlock(DisplayHTMLBlockTable, self.iwla._(u'All Downloads'), [self.iwla._(u'URI'), self.iwla._(u'Hit')])
table.setColsCSSClass(['', 'iwla_hit'])
total_entrance = [0]*2
new_list = self.max_downloads and top_downloads[:self.max_downloads] or top_downloads
for (uri, entrance) in new_list:
table.appendRow([generateHTMLLink(uri), entrance])
total_entrance[1] += entrance
if self.max_downloads:
others = 0
for (uri, entrance) in top_downloads[self.max_downloads:]:
others += entrance
table.appendRow([self.iwla._(u'Others'), others])
table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
page.appendBlock(table)
display.addPage(page)
title = self.iwla._(u'Top Downloads')
if self.create_all_downloads:
link = '<a href=\'%s\'>%s</a>' % (filename, self.iwla._(u'All Downloads'))
title = '%s - %s' % (title, link)
# Top in index
index = self.iwla.getDisplayIndex()
table = display.createBlock(DisplayHTMLBlockTable, title, [self.iwla._(u'URI'), self.iwla._(u'Hits')])
table.setColsCSSClass(['', 'iwla_hit'])
for (uri, entrance) in top_downloads[:10]:
table.appendRow([generateHTMLLink(uri), entrance])
total_entrance[1] -= entrance
if total_entrance[1]:
total_entrance[0] = self.iwla._(u'Others')
table.appendRow(total_entrance)
table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
index.appendBlock(table)