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