#!/usr/bin/env python3 # Copyright (C) 2020 Oscar Benedito # License: Affero General Public License version 3 or later # Generate index for GoAccess stats. import sys import datetime import os.path if len(sys.argv) < 2: sys.exit('Usage: stats-index-generator statsDir') dir = sys.argv[1] def has_info(link): return os.path.isfile(os.path.join(dir, link, 'index.html')) output = """ Website stats """ output += '

See stats of all time

' for year in [2020]: link = 'y/' + str(year) if has_info(link): output += '

' + str(year) + '

' else: output += '

' + str(year) + '

' for month in range(1, 13): date = datetime.datetime(year, month, 1) link = date.strftime("m/%Y/%m") if has_info(link): output += '

' + datetime.datetime(year, month, 1).strftime("%B") + '

    ' else: output += '

    ' + datetime.datetime(year, month, 1).strftime("%B") + '

      ' if date.isocalendar()[2] != 1: date = datetime.datetime(year, month, 1) isoweek = date.strftime("%V") link = date.strftime("w/%G/%V") if has_info(link): output += '
    • ' + isoweek + '
    • ' else: output += '
    • ' + isoweek + '
    • ' for i in range(date.isocalendar()[2] - 1): output += '
    • -
    • ' for day in range(1, 32): try: date = datetime.datetime(year, month, day) except: break if date.isocalendar()[2] == 1: isoweek = date.strftime("%V") link = date.strftime("w/%G/%V") if has_info(link): output += '
    • ' + isoweek + '
    • ' else: output += '
    • ' + isoweek + '
    • ' today = ' class="today"' if datetime.datetime.today().date() == date.date() else '' link = date.strftime("d/%Y/%m/%d") if has_info(link): output += '' + str(day) + '' else: output += '' + str(day) + '' output += '
    ' output += '
' output += '\n' with open(os.path.join(dir, 'index.html'), 'wt') as f: f.write(output)