iwla

iwla Git Source Tree

Root/plugins/display/top_downloads.py

1import time
2
3from iwla import IWLA
4from iplugin import IPlugin
5from display import *
6
7#
8# Display hook
9#
10# Create TOP downloads page
11#
12# Plugin requirements :
13# post_analysis/top_downloads
14#
15# Conf values needed :
16# max_downloads_displayed*
17# create_all_downloads_page*
18#
19# Output files :
20# OUTPUT_ROOT/year/month/top_downloads.html
21# OUTPUT_ROOT/year/month/index.html
22#
23# Statistics creation :
24# None
25#
26# Statistics update :
27# None
28#
29# Statistics deletion :
30# None
31#
32
33class IWLADisplayTopDownloads(IPlugin):
34 def __init__(self, iwla):
35 super(IWLADisplayTopDownloads, self).__init__(iwla)
36 self.API_VERSION = 1
37 self.requires = ['IWLAPostAnalysisTopDownloads']
38 self.max_downloads = self.iwla.getConfValue('max_downloads_displayed', 0)
39 self.create_all_downloads = self.iwla.getConfValue('create_all_downloads_page', True)
40
41 def hook(self):
42 display = self.iwla.getDisplay()
43 top_downloads = self.iwla.getMonthStats()['top_downloads']
44 top_downloads = sorted(top_downloads.items(), key=lambda t: t[1], reverse=True)
45
46 # All in a file
47 if self.create_all_downloads:
48 filename = 'top_downloads.html'
49 path = self.iwla.getCurDisplayPath(filename)
50 title = time.strftime('All Downloads - %B %Y', self.iwla.getCurTime())
51
52 page = display.createPage(title, path, self.iwla.getConfValue('css_path', []))
53 table = display.createBlock(DisplayHTMLBlockTable, 'All Downloads', ['URI', 'Hit'])
54 table.setColsCSSClass(['', 'iwla_hit'])
55
56 total_entrance = [0]*2
57 new_list = self.max_downloads and top_downloads[:self.max_downloads] or top_downloads
58 for (uri, entrance) in new_list:
59 table.appendRow([generateHTMLLink(uri), entrance])
60 total_entrance[1] += entrance
61 page.appendBlock(table)
62
63 display.addPage(page)
64
65 title = 'Top Downloads'
66 if self.create_all_downloads:
67 link = '<a href=\'%s\'>All Downloads</a>' % (filename)
68 title = '%s - %s' % (title, link)
69
70 # Top in index
71 index = self.iwla.getDisplayIndex()
72
73 table = display.createBlock(DisplayHTMLBlockTable, title, ['URI', 'Hits'])
74 table.setColsCSSClass(['', 'iwla_hit'])
75 for (uri, entrance) in top_downloads[:10]:
76 table.appendRow([generateHTMLLink(uri), entrance])
77 total_entrance[1] -= entrance
78 if total_entrance[1]:
79 total_entrance[0] = 'Others'
80 table.appendRow(total_entrance)
81 table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
82 index.appendBlock(table)

Archive Download this file

Branches

Tags