stats-index-generator.py (4810B) - raw


      1 #!/usr/bin/env python3
      2 # Copyright (C) 2020 Oscar Benedito <oscar@oscarbenedito.com>
      3 #
      4 # This program is free software: you can redistribute it and/or modify
      5 # it under the terms of the GNU Affero General Public License as
      6 # published by the Free Software Foundation, either version 3 of the
      7 # License, or (at your option) any later version.
      8 #
      9 # This program is distributed in the hope that it will be useful,
     10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 # GNU Affero General Public License for more details.
     13 #
     14 # You should have received a copy of the GNU Affero General Public License
     15 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
     16 
     17 # Generate index for GoAccess stats.
     18 
     19 import sys
     20 import datetime
     21 import os.path
     22 
     23 if len(sys.argv) < 2:
     24     sys.exit('Usage: stats-index-generator statsDir')
     25 
     26 dir = sys.argv[1]
     27 
     28 def has_info(link):
     29     return os.path.isfile(os.path.join(dir, link, 'index.html'))
     30 
     31 output = """<!DOCTYPE html>
     32 <html lang="en">
     33   <head>
     34     <meta charset="utf-8">
     35     <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
     36     <meta name="description" content="Oscar Benedito's website stats">
     37     <meta name="author" content="Oscar Benedito">
     38     <title>Website stats</title>
     39 
     40     <link rel="icon" href="/favicon.min.svg">
     41     <link rel="shortcut icon" href="/favicon.ico">
     42     <style>
     43 body {
     44   font-family: 'sans-serif';
     45   margin: 3em;
     46 }
     47 
     48 a.no-format {
     49   color: inherit;
     50   text-decoration: none;
     51 }
     52 
     53 a.hover:hover {
     54     text-decoration: underline;
     55 }
     56 
     57 h1,
     58 h2 {
     59   text-align: center;
     60 }
     61 
     62 div.year {
     63   margin: 5em auto;
     64 }
     65 
     66 div.year-cal {
     67   display: flex;
     68   flex-flow: row wrap;
     69   justify-content: flex-start;
     70   max-width: 1024px;
     71   margin: 0 auto;
     72 }
     73 
     74 div.month {
     75   width: 17em;
     76   margin: 0;
     77   padding: 1em 0;
     78   flex: 1;
     79 }
     80 
     81 div.month ul {
     82   width: 17em;
     83   margin: 0 auto;
     84   padding: 0em 1em 1em 1em;
     85 }
     86 
     87 div.month li {
     88   float: left;
     89   display: block;
     90   width: 12.5%;
     91   text-align: center;
     92   list-style-type: none;
     93 }
     94 
     95 div.month a li:hover {
     96   border-radius: 5px;
     97   background-color: #1abc9c;
     98   color: #ecf0f1 !important;
     99 }
    100 
    101 div.month li.today {
    102   border-radius: 5px;
    103   background-color: #5759D4;
    104   color: #ecf0f1;
    105 }
    106 
    107 div.month li.week {
    108   font-weight: 900;
    109   color: #e67e22;
    110 }
    111     </style>
    112   </head>
    113 
    114   <body>
    115 """
    116 
    117 output += '<h1><a class="no-format hover" href="/all">See stats of all time</a></h1>'
    118 
    119 for year in [2020]:
    120     link = 'y/' + str(year)
    121     if has_info(link):
    122         output += '<div class="year"><h1><a class="no-format hover" href="/' + link + '">' + str(year) + '</a></h1><div class="year-cal">'
    123     else:
    124         output += '<div class="year"><h1>' + str(year) + '</h1><div class="year-cal">'
    125 
    126     for month in range(1, 13):
    127         date = datetime.datetime(year, month, 1)
    128         link = date.strftime("m/%Y/%m")
    129         if has_info(link):
    130             output += '<div class="month"><h2><a class="no-format hover" href="/' + link + '">' + datetime.datetime(year, month, 1).strftime("%B") + '</a></h2><ul>'
    131         else:
    132             output += '<div class="month"><h2>' + datetime.datetime(year, month, 1).strftime("%B") + '</h2><ul>'
    133 
    134         if date.isocalendar()[2] != 1:
    135             date = datetime.datetime(year, month, 1)
    136             isoweek = date.strftime("%V")
    137             link = date.strftime("w/%G/%V")
    138             if has_info(link):
    139                 output += '<a class="no-format" href="/' + link + '"><li class="week">' + isoweek + '</li></a>'
    140             else:
    141                 output += '<li class="week">' + isoweek + '</li>'
    142 
    143             for i in range(date.isocalendar()[2] - 1):
    144                 output += '<li style="opacity: 0;">-</li>'
    145 
    146         for day in range(1, 32):
    147             try:
    148                 date = datetime.datetime(year, month, day)
    149             except:
    150                 break
    151 
    152             if date.isocalendar()[2] == 1:
    153                 isoweek = date.strftime("%V")
    154                 link = date.strftime("w/%G/%V")
    155                 if has_info(link):
    156                     output += '<a class="no-format" href="/' + link + '"><li class="week">' + isoweek + '</li></a>'
    157                 else:
    158                     output += '<li class="week">' + isoweek + '</li>'
    159 
    160             today = ' class="today"' if datetime.datetime.today().date() == date.date() else ''
    161             link = date.strftime("d/%Y/%m/%d")
    162             if has_info(link):
    163                 output += '<a class="no-format" href="/' + link + '"><li' + today + '>' + str(day) + '</li></a>'
    164             else:
    165                 output += '<li' + today + '>' + str(day) + '</li>'
    166 
    167         output += '</ul></div>'
    168 
    169     output += '</div></div>'
    170 
    171 output += '</body></html>\n'
    172 
    173 with open(os.path.join(dir, 'index.html'), 'wt') as f:
    174     f.write(output)