From fc1c852d9263fc43f27792f38a8e68674e944629 Mon Sep 17 00:00:00 2001 From: Waishnav Date: Fri, 8 Jul 2022 21:37:57 +0530 Subject: updating install | updating bin according to new function names --- src/bin/watcher | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) mode change 100755 => 100644 src/bin/watcher (limited to 'src/bin/watcher') diff --git a/src/bin/watcher b/src/bin/watcher old mode 100755 new mode 100644 index 2d4551e..d2392cd --- a/src/bin/watcher +++ b/src/bin/watcher @@ -1,10 +1,9 @@ #!/usr/bin/python - import os import sys sys.path.insert(0, "/usr/share/Watcher/") import watch_log as x -from colored_text import Color +from commands import Color import commands as cmd def help_option(): @@ -23,7 +22,7 @@ try: if arg == "-ds" or arg == "--day-summary": cmd.daily_summary() elif arg == "-ws" or arg == "--week-summary": - os.popen("python3 /usr/share/Watcher/week_analysis.py") + cmd.week_summary() elif arg == "-h" or arg == "--help": help_option() @@ -31,7 +30,7 @@ try: print("Log creations started... \nif you wanna stop it, use keyboard-shortcut (Ctrl+Shift+C)") x.log_creation() elif arg == "--version": - print("Version: 1.0") + print("Version: 1.2") else: print(Color.RED("Wrong")+" [OPTION] choosen. Have a look at the Options!!\n") print(Color.YELLOW("OPTIONS")) -- cgit v1.2.3 From 7133173a8c33c8120af6006380aecf7846e6e0bd Mon Sep 17 00:00:00 2001 From: Waishnav Date: Fri, 8 Jul 2022 21:45:45 +0530 Subject: refactoring some function --- src/Watcher/analysis.py | 98 ++++++++++++++++++++++++++++++++++++++++++ src/Watcher/colored_text.py | 30 ------------- src/Watcher/commands.py | 57 +++++++++++++++++++++--- src/Watcher/report_creation.py | 82 ----------------------------------- src/Watcher/time_operations.py | 7 ++- src/Watcher/week_analysis.py | 46 -------------------- src/bin/watcher | 5 ++- 7 files changed, 156 insertions(+), 169 deletions(-) create mode 100755 src/Watcher/analysis.py delete mode 100755 src/Watcher/colored_text.py delete mode 100755 src/Watcher/report_creation.py delete mode 100755 src/Watcher/week_analysis.py (limited to 'src/bin/watcher') diff --git a/src/Watcher/analysis.py b/src/Watcher/analysis.py new file mode 100755 index 0000000..7a96b60 --- /dev/null +++ b/src/Watcher/analysis.py @@ -0,0 +1,98 @@ +import os +import csv +from watch_log import get_date +import datetime +import time_operations as to + +def extract_data(date): + user = os.getlogin() + path = "/home/" + user +"/.cache/Watcher/raw_data/" + filename = path + date + ".csv" + + l = list() # l = list of (app_name, time spent on app on that session) + d = dict() + + if os.path.isfile(filename): + with open(filename, 'r') as file: + reader = csv.reader(file) + for row in reader: + for column in row: + l.append([column[18::],column[9:17]]) + d.update({column[18::]: "O"}) + else: + None + + d = list(d) # list of app opened on that day + return d, l + +# creating dictionary to store time spend on each applicaitons on that particular day +def final_report(window_opened, time_spent): + report = dict() + + for i in window_opened: # i is applications name + time = '00:00:00' + for j in time_spent: # j is list of applicaions_name and time_spent in particular session + if i == j[0]: + time = to.time_addition(j[1], time) + report.update({i:time}) + + #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(): + sorted_values.append(to.convert_into_sec(y)) + + sorted_values.sort(reverse=True) + sorted_report = dict() + + for i in sorted_values: + for x, y in report.items(): + if to.convert_into_sec(y) == i: + sorted_report.update({x:y}) + + return sorted_report + +# getting dates of the week for week summary +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] + +def weeklly_logs(): + W_Y = os.popen('''date +"W%V-%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 = 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) + + csvwriter.writerow([weekday_from_date(i), Total_screen_time]) + + for i in dates: + x, y = extract_data(str(i)) + window_opened += x + time_spent += y + for x, y in final_report(window_opened, time_spent).items(): + csvwriter.writerow([y, x]) + diff --git a/src/Watcher/colored_text.py b/src/Watcher/colored_text.py deleted file mode 100755 index bb7ec7c..0000000 --- a/src/Watcher/colored_text.py +++ /dev/null @@ -1,30 +0,0 @@ -class Color: - - def GREY(text): - return '\033[90m' + text + '\033[0m' - - def BLUE(text): - return '\033[34m' + text + '\033[0m' - - def GREEN(text): - return '\033[32m' + text + '\033[0m' - - def YELLOW(text): - return '\033[33m' + text + '\033[0m' - - def RED(text): - return '\033[31m' + text + '\033[0m' - - def PURPLE(text): - return '\033[35m' + text + '\033[0m' - - def DARKCYAN(text): - return '\033[36m' + text + '\033[0m' - - def BOLD(text): - return '\033[1m' + text + '\033[0m' - - def UNDERLINE(text): - return '\033[4m' + text + '\033[0m' - -#print(color.GREEN("hello")) diff --git a/src/Watcher/commands.py b/src/Watcher/commands.py index 6228133..dd1d2c4 100755 --- a/src/Watcher/commands.py +++ b/src/Watcher/commands.py @@ -2,14 +2,61 @@ import os import csv import datetime from watch_log import get_date -import report_creation as rc -from colored_text import Color +import analysis as anls import time_operations as to +class Color: + + def GREY(text): + return '\033[90m' + text + '\033[0m' + + def BLUE(text): + return '\033[34m' + text + '\033[0m' + + def GREEN(text): + return '\033[32m' + text + '\033[0m' + + def YELLOW(text): + return '\033[33m' + text + '\033[0m' + + def RED(text): + return '\033[31m' + text + '\033[0m' + + def PURPLE(text): + return '\033[35m' + text + '\033[0m' + + def DARKCYAN(text): + return '\033[36m' + text + '\033[0m' + + def BOLD(text): + return '\033[1m' + text + '\033[0m' + + def UNDERLINE(text): + return '\033[4m' + text + '\033[0m' + + def daily_summary(): date = get_date() - window_opened, time_spent = rc.extract_data(date) - rc.prints_report(window_opened, time_spent) + window_opened, time_spent = anls.extract_data(date) + Total_screen_time = "00:00:00" + for x,y in anls.final_report(window_opened, time_spent).items(): + Total_screen_time = to.time_addition(y, 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":>29}')) + print(" ────────────────────────────────────────────────") + + for x,y in anls.final_report(window_opened, time_spent).items(): + if x == "": + x = "Home-Screen" + print(" " + Color.GREEN(f'{x:<22}') + '\t ',f'{to.format_time(y):>12}') def week_summary(): W_Y = os.popen('''date +"W%V-%Y"''').read()[0:-1] @@ -35,7 +82,7 @@ def week_summary(): 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) + #anls.prints_report(window_opened, time_spent, is_week) print(" ────────────────────────────────────────────────") print(Color.RED(f'{" App Usages":>29}')) print(" ────────────────────────────────────────────────") diff --git a/src/Watcher/report_creation.py b/src/Watcher/report_creation.py deleted file mode 100755 index 1e080db..0000000 --- a/src/Watcher/report_creation.py +++ /dev/null @@ -1,82 +0,0 @@ -import csv -import os -import time_operations as to -from watch_log import get_date -from colored_text import Color -import datetime - -def extract_data(date): - user = os.getlogin() - path = "/home/" + user +"/.cache/Watcher/raw_data/" - filename = path + date + ".csv" - - l = list() # l = list of (app_name, time spent on app on that session) - d = dict() - - if os.path.isfile(filename): - with open(filename, 'r') as file: - reader = csv.reader(file) - for row in reader: - for column in row: - l.append([column[18::],column[9:17]]) - d.update({column[18::]: "O"}) - else: - None - - d = list(d) # list of app opened on that day - return d, l - -# creating dictionary to store time spend on each applicaitons on that particular day -def final_report(window_opened, time_spent): - report = dict() - - for i in window_opened: # i is applications name - time = '00:00:00' - for j in time_spent: # j is list of applicaions_name and time_spent in particular session - if i == j[0]: - time = to.time_addition(j[1], time) - report.update({i:time}) - - #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(): - sorted_values.append(to.convert_into_sec(y)) - - sorted_values.sort(reverse=True) - sorted_report = dict() - - for i in sorted_values: - for x, y in report.items(): - if to.convert_into_sec(y) == i: - sorted_report.update({x:y}) - - return sorted_report - -# ░ ▒ █ ─── -#print("▒▒▒\t▒▒▒\n███") - -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 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":>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.format_time(y):>12}') - - diff --git a/src/Watcher/time_operations.py b/src/Watcher/time_operations.py index 08a42ef..23326d9 100755 --- a/src/Watcher/time_operations.py +++ b/src/Watcher/time_operations.py @@ -1,4 +1,3 @@ - def time_difference(a,b): # b - a hr = int(b[0:2])-int(a[0:2]) mn = int(b[3:5])-int(a[3:5]) @@ -12,17 +11,17 @@ def time_difference(a,b): # b - a elif mn < 0 and sec >= 0: hr = hr - 1 mn = 60 + mn + if hr < 0: + hr = hr + 24 elif sec < 0 and mn > 0: sec = 60 + sec mn = mn - 1 if hr < 0: hr = hr + 24 - elif sec < 0 and mn == 0: hr = hr - 1 mn = 59 sec = 60 + sec - #elif int(mn) < 0: hr = str(hr).zfill(2) mn = str(mn).zfill(2) @@ -50,7 +49,6 @@ def time_addition(a,b): mn = str(mn).zfill(2) sec = str(sec).zfill(2) result = hr + ":" + mn + ":" + sec - return result def format_time(t): @@ -67,3 +65,4 @@ def convert_into_sec(t): sec = int(t[0:2])*3600 + int(t[3:5])*60 + int(t[6::]) return sec + diff --git a/src/Watcher/week_analysis.py b/src/Watcher/week_analysis.py deleted file mode 100755 index a82b5f3..0000000 --- a/src/Watcher/week_analysis.py +++ /dev/null @@ -1,46 +0,0 @@ -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%V-%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 - 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 d2392cd..4bea832 100644 --- a/src/bin/watcher +++ b/src/bin/watcher @@ -5,6 +5,7 @@ sys.path.insert(0, "/usr/share/Watcher/") import watch_log as x from commands import Color import commands as cmd +from analysis import weeklly_logs def help_option(): print(Color.BLUE("Watcher") + " - Minimal open source screen-time tracker\n") @@ -22,12 +23,12 @@ try: if arg == "-ds" or arg == "--day-summary": cmd.daily_summary() elif arg == "-ws" or arg == "--week-summary": - + weeklly_logs() cmd.week_summary() elif arg == "-h" or arg == "--help": help_option() elif arg == "--start": - print("Log creations started... \nif you wanna stop it, use keyboard-shortcut (Ctrl+Shift+C)") + print("Log creations started... \nif you wanna stop it, use keyboard-shortcut (Ctrl+Shift+C or Ctrl+C)") x.log_creation() elif arg == "--version": print("Version: 1.2") -- cgit v1.2.3