diff options
Diffstat (limited to 'src')
-rwxr-xr-x | src/Watcher/colored_text.py | 2 | ||||
-rwxr-xr-x | src/Watcher/commands.py | 45 | ||||
-rwxr-xr-x | src/Watcher/report_creation.py | 59 | ||||
-rwxr-xr-x | src/Watcher/time_operations.py | 22 | ||||
-rwxr-xr-x | src/Watcher/watch_log.py | 2 | ||||
-rwxr-xr-x | src/Watcher/week_analysis.py | 46 | ||||
-rwxr-xr-x | src/bin/watcher | 23 | ||||
-rw-r--r-- | src/service/watcher.service | 4 |
8 files changed, 133 insertions, 70 deletions
diff --git a/src/Watcher/colored_text.py b/src/Watcher/colored_text.py index 162510d..bb7ec7c 100755 --- a/src/Watcher/colored_text.py +++ b/src/Watcher/colored_text.py @@ -16,7 +16,7 @@ class Color: return '\033[31m' + text + '\033[0m' def PURPLE(text): - return '\033[95m' + text + '\033[0m' + return '\033[35m' + text + '\033[0m' def DARKCYAN(text): return '\033[36m' + text + '\033[0m' diff --git a/src/Watcher/commands.py b/src/Watcher/commands.py new file mode 100755 index 0000000..4bbd8c2 --- /dev/null +++ b/src/Watcher/commands.py @@ -0,0 +1,45 @@ +import os +import csv +import datetime +from watch_log import get_date +import report_creation as rc +from colored_text import Color +import time_operations as to + +def daily_summary(): + date = get_date() + window_opened, time_spent = rc.extract_data(date) + rc.prints_report(window_opened, time_spent) + +def week_summary(): + W_Y = os.popen('''date +"W%U-%Y"''').read()[0:-1] + user = os.getlogin() + filename = "/home/"+user+"/.cache/Watcher/Analysis/"+W_Y+".csv" + with open(filename, 'r') as file: + csvreader = csv.reader(file, delimiter='\t') + week_overview = dict() + app_usages = dict() + for row in csvreader: + if len(row[0]) == 3: + week_overview.update({row[0]:row[1]}) + else: + app_usages.update({row[1]:row[0]}) + + week_screen_time = "00:00:00" + for x, y in week_overview.items(): + week_screen_time = to.time_addition(y, week_screen_time) + + print(Color.PURPLE("\n Week's screen-time\t\t ") + Color.BLUE(to.format_time(week_screen_time))) + print(" ────────────────────────────────────────────────") + + for x, y in week_overview.items(): + print(" " + f'{Color.YELLOW(x):>21}' + "\t\t " + Color.BLUE(to.format_time(y))) + + #rc.prints_report(window_opened, time_spent, is_week) + print(" ────────────────────────────────────────────────") + print(Color.RED(f'{" App Usages":>29}')) + print(" ────────────────────────────────────────────────") + for x,y in app_usages.items(): + if x == "": + x = "Home-Screen" + print(" " + Color.GREEN(f'{x:<22}') + '\t ',f'{to.format_time(y):>12}') diff --git a/src/Watcher/report_creation.py b/src/Watcher/report_creation.py index bde5d2d..1e080db 100755 --- a/src/Watcher/report_creation.py +++ b/src/Watcher/report_creation.py @@ -37,7 +37,9 @@ def final_report(window_opened, time_spent): time = to.time_addition(j[1], time) report.update({i:time}) - #report.pop("User-logged-in") + #print(report) + if "User-logged-in" in report.keys(): + report.pop("User-logged-in") # sort report dictonary in decreasing order of Usages sorted_values = [] for x,y in report.items(): @@ -56,56 +58,25 @@ def final_report(window_opened, time_spent): # ░ ▒ █ ─── #print("▒▒▒\t▒▒▒\n███") -def prints_report(window_opened, time_spent, is_week): +def prints_report(window_opened, time_spent): Total_screen_time = "00:00:00" for x,y in final_report(window_opened, time_spent).items(): Total_screen_time = to.time_addition(y, Total_screen_time) - if is_week: - print(Color.YELLOW("\n Week's Screen-Time\t\t ") + Color.BLUE(to.convert_time(Total_screen_time))) - else: - print(Color.YELLOW("\n Today's Screen-Time\t\t ") + Color.BLUE(to.convert_time(Total_screen_time))) + + if len(to.format_time(Total_screen_time)) == 3: + print(Color.YELLOW("\n Today's Screen-Time\t\t ") + Color.BLUE(f'{to.format_time(Total_screen_time):>16}')) + elif len(to.format_time(Total_screen_time)) == 7: + print(Color.YELLOW("\n Today's Screen-Time\t\t ") + Color.BLUE(f'{to.format_time(Total_screen_time):>11}')) + elif len(to.format_time(Total_screen_time)) == 11: + print(Color.YELLOW("\n Today's Screen-Time\t\t ") + Color.BLUE(to.format_time(Total_screen_time))) + print(" ────────────────────────────────────────────────") - print(Color.RED(f'{" App Usages":>28}')) + print(Color.RED(f'{" App Usages":>29}')) print(" ────────────────────────────────────────────────") for x,y in final_report(window_opened, time_spent).items(): if x == "": x = "Home-Screen" - print(" " + Color.GREEN(f'{x:<22}') + '\t ',f'{to.convert_time(y):>12}') - -def daily_summary(): - date = get_date() - window_opened, time_spent = extract_data(date) - prints_report(window_opened, time_spent, False) - -def week_summary(): - dates = [] - theday = datetime.date.today() - weekday = theday.isoweekday() - 1 # week starts on Monday and ends on Sunday - start = theday - datetime.timedelta(days=weekday) - dates = [start + datetime.timedelta(days=d) for d in range(weekday+1)] - dates = [str(d) for d in dates] - - window_opened = list() - time_spent = list() - for i in dates: - window_opened, time_spent = extract_data(i) - Total_screen_time = "00:00:00" - for x, y in final_report(window_opened, time_spent).items(): - Total_screen_time = to.time_addition(y, Total_screen_time) - print(" "+Total_screen_time, end=' ') - - print("\n Mon Tue Wed Thu Fri Sat Sun" ) - - for i in dates: - x, y = extract_data(str(i)) - window_opened += x # smth is wrong here - time_spent += y - prints_report(window_opened, time_spent, True) - - -#d = os.popen('''date -d "2022-03-10" +%a''').read() -if __name__ == "__main__": - daily_summary() - #week_summary() + print(" " + Color.GREEN(f'{x:<22}') + '\t ',f'{to.format_time(y):>12}') + diff --git a/src/Watcher/time_operations.py b/src/Watcher/time_operations.py index 4145e8d..066a15e 100755 --- a/src/Watcher/time_operations.py +++ b/src/Watcher/time_operations.py @@ -9,12 +9,17 @@ def time_difference(a,b): # b - a sec = 60 + sec if hr < 0: hr = hr + 24 - elif int(mn) < 0 and int(sec) > 0: + elif int(mn) < 0 and int(sec) >= 0: hr = hr - 1 mn = 60 + mn elif int(sec) < 0 and int(mn) > 0: sec = 60 + sec mn = mn - 1 + elif int(sec) < 0 and int(mn) == 0: + hr = hr - 1 + mn = 59 + sec = 60 + sec + #elif int(mn) < 0: hr = str(hr).zfill(2) mn = str(mn).zfill(2) @@ -45,13 +50,14 @@ def time_addition(a,b): return result -def convert_time(t): - if int(t[0:2]) == 0: - result = t[3:5] + 'm ' + t[6::] + 's' - if int(t[3:5]) == 0: - result = t[6::] + 's' - else: - result = t[0:2] + 'h ' + t[3:5] + 'm ' + t[6::] + 's' +def format_time(t): + result = t[0:2] + 'h ' + t[3:5] + 'm ' + t[6::] + 's' + #if int(t[0:2]) == 0: + # result = t[3:5] + 'm ' + t[6::] + 's' + # if int(t[3:5]) == 0: + # result = t[6::] + 's' + #else: + # result = t[0:2] + 'h ' + t[3:5] + 'm ' + t[6::] + 's' return result def convert_into_sec(t): diff --git a/src/Watcher/watch_log.py b/src/Watcher/watch_log.py index 3ac7aa4..98895bf 100755 --- a/src/Watcher/watch_log.py +++ b/src/Watcher/watch_log.py @@ -35,7 +35,7 @@ def append_line_in_csv(date, closed_time, window_name): afk = False def log_creation(): global afk - append_line_in_csv(get_date(), get_time(), "User-loged-in") + append_line_in_csv(get_date(), get_time(), "User-logged-in") while True: previous_window = x.active_window() if x.is_window_changed(previous_window) and not(afk): diff --git a/src/Watcher/week_analysis.py b/src/Watcher/week_analysis.py new file mode 100755 index 0000000..1f064b2 --- /dev/null +++ b/src/Watcher/week_analysis.py @@ -0,0 +1,46 @@ +import os +import csv +import datetime +import report_creation as rc +import time_operations as to + +def get_dates(): + theday = datetime.date.today() + weekday = theday.isoweekday() - 1 + # The start of the week + start = theday - datetime.timedelta(days=weekday) + # build a simple range + dates = [start + datetime.timedelta(days=d) for d in range(weekday+1)] + dates = [str(d) for d in dates] + + return dates + +def weekday_from_date(date): + day = os.popen('''date -d "'''+ date + '''" +%a''').read() + return day[0:-1] + +W_Y = os.popen('''date +"W%U-%Y"''').read()[0:-1] +user = os.getlogin() +filename = "/home/"+user+"/.cache/Watcher/Analysis/"+W_Y+".csv" +with open(filename, "w") as csvfile: + csvwriter = csv.writer(csvfile, delimiter='\t') + #csvwriter.writerow(["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]) + dates = get_dates() + + window_opened = list() + time_spent = list() + for i in dates: + window_opened, time_spent = rc.extract_data(i) + Total_screen_time = "00:00:00" + for x, y in rc.final_report(window_opened, time_spent).items(): + Total_screen_time = to.time_addition(y, Total_screen_time) + + csvwriter.writerow([weekday_from_date(i), Total_screen_time]) + + for i in dates: + x, y = rc.extract_data(str(i)) + window_opened += x # smth is wrong here + time_spent += y + for x, y in rc.final_report(window_opened, time_spent).items(): + csvwriter.writerow([y, x]) + diff --git a/src/bin/watcher b/src/bin/watcher index 126341a..0943df2 100755 --- a/src/bin/watcher +++ b/src/bin/watcher @@ -10,23 +10,24 @@ sys.path.insert(0, "/usr/share/Watcher/") import argparse import watch_log as x from colored_text import Color -import report_creation as rc +import commands as cmd def help_option(): print(Color.BLUE("Watcher") + " - Minimal open source screen-time tracker\n") print(Color.YELLOW("USAGE:")+"\n\t watcher [OPTION]\n") print(Color.YELLOW("OPTIONS")) - print("\t-ds, --daily-summary Displays where you have spend your time of that day") + print("\t-ds, --day-summary Displays where you have spend your time of that day") print("\t-ws, --week-summary Displays screen-time of each day of week") - print("\nFor more information see github repo: https://github.com/Waishnav/Watcher") + print("\nFor more information see github repo: https://github.com/Waishnav/Watcher and Don't forget to star the repo") #print("▒▒▒\t▒▒▒\n███") arg = sys.argv[1] -if arg == "-ds" or arg == "--daily-summary": - rc.daily_summary() +if arg == "-ds" or arg == "--day-summary": + cmd.daily_summary() elif arg == "-ws" or arg == "--week-summary": - rc.week_summary() + os.popen("python3 /usr/share/Watcher/week_analysis.py") + cmd.week_summary() elif arg == "-h" or arg == "--help": help_option() elif arg == "--start": @@ -36,17 +37,11 @@ elif arg == "--version": else: print(Color.RED("Wrong")+" [OPTION] choosen. Have a look at the Options!!\n") print(Color.YELLOW("OPTIONS")) - print("\t-ds, --daily-summary Displays where you have spend your time of that day") + print("\t-ds, --day-summary Displays where you have spend your time of that day") print("\t-ws, --week-summary Displays screen-time of each day of week") + print("\nFor more information see github repo: https://github.com/Waishnav/Watcher and Don't forget to star the repo") #parser = argparse.ArgumentParser(description="Minimal open source screen-time calulator for digitally wellbeing") #parser.add_argument() #args = parser.parse_args() #print(args) - -#def main(args=None): -# if arg is None: -# print("") -# else: -# print('''▒▒▒ ███ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒\n▒▒▒ ███ ▒▒▒ ███ ▒▒▒ ███ ███\n███ ███ ███ ███ ▒▒▒ ███ ███\n███ ███ ███ ███ ███ ███ ███\n███ ███ ███ ███ ███ ███ ███\n███ ███ ███ ███ ███ ███ ███\n███ ███ ███ ███ ███ ███ ███''') - diff --git a/src/service/watcher.service b/src/service/watcher.service index 803f2ee..90bd2a8 100644 --- a/src/service/watcher.service +++ b/src/service/watcher.service @@ -1,8 +1,8 @@ [Unit] -Description=Get the perspective of Watcher of your Screen-Time +Description=Get the perspective from Watcher about your Screen-Time [Service] -ExecStart=/bin/python /usr/share/Watcher/log_files.py +ExecStart=/bin/python /usr/share/Watcher/watch_log.py Type=simple Restart=on-failure |