You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
180 lines
5.1 KiB
180 lines
5.1 KiB
#!/usr/bin/python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# made for 42l stats
|
|
|
|
import subprocess
|
|
import datetime as dt
|
|
|
|
from natsort import natsorted, ns
|
|
|
|
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
|
env = Environment(
|
|
loader=FileSystemLoader('.'),
|
|
autoescape=select_autoescape(['html', 'xml'])
|
|
)
|
|
|
|
# without the trailing slash
|
|
# (or '/' alone for root)
|
|
BASE_URL = "/reports"
|
|
|
|
# Folder containing generated stats
|
|
BASE_INDEX = "/base/reports/"
|
|
|
|
# relative to BASE_INDEX
|
|
MONTHLY_REPORTS = "monthly/"
|
|
WEEKLY_REPORTS = "weekly/"
|
|
|
|
month_names = [
|
|
"January",
|
|
"February",
|
|
"March",
|
|
"April",
|
|
"May",
|
|
"June",
|
|
"July",
|
|
"August",
|
|
"September",
|
|
"October",
|
|
"November",
|
|
"December"
|
|
]
|
|
|
|
service_names = {}
|
|
|
|
# fill here your service names
|
|
# servicename.html
|
|
service_names["main"] = "Web"
|
|
service_names["42l"] = "Web"
|
|
service_names["doh"] = "DoH"
|
|
service_names["draw"] = "Draw"
|
|
service_names["element"] = "Element"
|
|
service_names["forms"] = "Forms"
|
|
service_names["git"] = "Git"
|
|
service_names["matrix"] = "Matrix"
|
|
service_names["nextcloud"] = "Nextcloud"
|
|
service_names["nitter"] = "Nitter"
|
|
service_names["riot"] = "Riot"
|
|
service_names["rs-short"] = "Links"
|
|
service_names["rsshort"] = "Links"
|
|
|
|
def month_format(value):
|
|
return month_names[int(value) - 1]
|
|
|
|
def srv_format(value):
|
|
value = value.split(".")[0]
|
|
if value in service_names:
|
|
return service_names[value]
|
|
else:
|
|
print("No service name specified for file " + value)
|
|
return value
|
|
|
|
env.filters['month_format'] = month_format
|
|
env.filters['srv_format'] = srv_format
|
|
|
|
def list_files(folder):
|
|
response = run_command(["ls", folder])
|
|
listed = response.stdout.decode('utf-8').split('\n')
|
|
for i in range(len(listed)):
|
|
listed[i] = listed[i].replace(".br", "")
|
|
|
|
return (listed)
|
|
|
|
def run_command(arr_cmd):
|
|
try:
|
|
response = subprocess.run(arr_cmd, capture_output = True)
|
|
except Exception as e:
|
|
print("Error: " + arr_cmd[0] + " couldn't execute:\n" + e + "\nAborting.")
|
|
exit()
|
|
if (response.returncode != 0):
|
|
print("Error: " + arr_cmd[0] + " didn't return 0:\n" + str(response.stderr) + "\nAborting.")
|
|
exit()
|
|
return response
|
|
|
|
def rm_index(file_list):
|
|
if "index.html" in file_list:
|
|
file_list.pop(file_list.index("index.html"))
|
|
return file_list
|
|
|
|
current_year = dt.datetime.now().year
|
|
years_list = rm_index(list_files(BASE_INDEX+MONTHLY_REPORTS)[:-1])
|
|
|
|
if str(current_year) not in years_list:
|
|
print("Current year not logged yet. Happy New Year!")
|
|
print("Interrupting the script to avoid unpredictable behavior.")
|
|
exit()
|
|
|
|
month_list = []
|
|
week_list = []
|
|
|
|
for i in range(0, len(years_list)):
|
|
month_list.append(rm_index(list_files(BASE_INDEX+MONTHLY_REPORTS+years_list[i])[:-1]))
|
|
week_list.append(rm_index(list_files(BASE_INDEX+WEEKLY_REPORTS+years_list[i])[:-1]))
|
|
|
|
# years [ array of numbers ]
|
|
# months [ array of month ]
|
|
# month ( num: month number ; display: month name )
|
|
# sel_year: selected year
|
|
# weeks [ array of numbers ]
|
|
# base_url
|
|
|
|
mainindex_tpl = env.get_template('index.html')
|
|
|
|
index_tpls = []
|
|
|
|
# All the the index.html for each year
|
|
# duplicated in monthly/<year>/ and weekly/<year>/ due to bad structure
|
|
for i in range(0, len(years_list)):
|
|
index_tpls.append(mainindex_tpl.render(
|
|
base_url=BASE_URL,
|
|
sel_year=years_list[i],
|
|
years=years_list,
|
|
months=natsorted(month_list[i]),
|
|
weeks=natsorted(week_list[i]),
|
|
))
|
|
|
|
with open(BASE_INDEX+MONTHLY_REPORTS+years_list[i]+"/"+"index.html", "w") as fh:
|
|
fh.write(index_tpls[i])
|
|
|
|
with open(BASE_INDEX+WEEKLY_REPORTS+years_list[i]+"/"+"index.html", "w") as fh:
|
|
fh.write(index_tpls[i])
|
|
|
|
cy_index = years_list.index(str(current_year))
|
|
|
|
# Main index.html
|
|
with open(BASE_INDEX+"index.html", "w") as fh:
|
|
fh.write(index_tpls[cy_index])
|
|
|
|
# sel_year: selected year for template
|
|
# sel_period: either "weekly" or "monthly"
|
|
# folder_num: either the selected week or the selected month
|
|
# services [ array of service ]
|
|
|
|
srvindex_tpl = env.get_template('index-service.html')
|
|
|
|
# now writing index per service
|
|
for i in range(0, len(years_list)):
|
|
for j in range(0, len(week_list[i])):
|
|
path = BASE_INDEX+WEEKLY_REPORTS+years_list[i]+"/"+week_list[i][j]+"/"
|
|
list_services = list_files(path+"all/")[:-1]
|
|
with open(path+"index.html", "w") as fh:
|
|
fh.write(srvindex_tpl.render(
|
|
base_url=BASE_URL,
|
|
sel_year=years_list[i],
|
|
sel_period="weekly",
|
|
folder_num=week_list[i][j],
|
|
services=list_services
|
|
))
|
|
|
|
for j in range(0, len(month_list[i])):
|
|
path = BASE_INDEX+MONTHLY_REPORTS+years_list[i]+"/"+month_list[i][j]+"/"
|
|
list_services = list_files(path+"all/")[:-1]
|
|
with open(path+"index.html", "w") as fh:
|
|
fh.write(srvindex_tpl.render(
|
|
base_url=BASE_URL,
|
|
sel_year=years_list[i],
|
|
sel_period="monthly",
|
|
folder_num=month_list[i][j],
|
|
services=list_services
|
|
))
|