Generate per day statistics for screen time usage.

This commit is contained in:
Ceda EI 2019-05-18 14:49:43 +05:30
parent a1a8d484bc
commit e1ddf2f206
1 changed files with 71 additions and 0 deletions

71
daily.py Executable file
View File

@ -0,0 +1,71 @@
#!/usr/bin/env python3
from datetime import datetime
import matplotlib.pyplot as plt
import sys
def convert_param_to_dates(x):
pair = x.split(',')
first = datetime.fromtimestamp(int(pair[0].replace('(', '')))
try:
second = datetime.fromtimestamp(int(pair[1].replace(')', '')))
except(ValueError):
second = datetime.now()
if first.day == second.day:
return (first, second)
else:
midnight = datetime.fromisoformat(second.strftime('%FT') + "00:00:00")
return [
(first, midnight),
(midnight, second)
]
def flatten(array):
ret_arr = []
for i in array:
if type(i) == list:
ret_arr += flatten(i)
else:
ret_arr.append(i)
return ret_arr
def seconds_to_str(s):
hours = int(s//3600)
minutes = int(s//60 % 60)
seconds = int(s % 60)
return f"{hours:02}:{minutes:02}:{seconds:02}"
filename = sys.argv[1]
with open(filename) as f:
contents = flatten(list(map(convert_param_to_dates, f.readlines())))
dates = []
times = []
current_date = contents[0][0]
time_in_s = 0
for i in contents:
if i[0].strftime("%F") == current_date.strftime("%F"):
diff = i[1] - i[0]
time_in_s += diff.total_seconds()
else:
dates.append(current_date)
times.append(time_in_s)
current_date = i[0]
diff = i[1] - i[0]
time_in_s = diff.total_seconds()
dates.append(current_date)
times.append(time_in_s)
for date, s in zip(dates, times):
print(date.strftime("%F"), "\t", seconds_to_str(s))
fig, ax = plt.subplots()
ax.xaxis.set_tick_params(which='major', rotation=90)
plt.bar(list(map(lambda x: x.strftime('%a %d/%m'), dates)),
list(map(lambda x: x/3600, times)))
plt.show()