diff options
author | 2022-07-08 21:45:45 +0530 | |
---|---|---|
committer | 2022-07-08 21:45:45 +0530 | |
commit | 7133173a8c33c8120af6006380aecf7846e6e0bd (patch) | |
tree | 308aaacaf8809971742ca11014a156f38a914ffa /src | |
parent | fc1c852d9263fc43f27792f38a8e68674e944629 (diff) | |
download | shopno-os-log-sync-7133173a8c33c8120af6006380aecf7846e6e0bd.tar.gz shopno-os-log-sync-7133173a8c33c8120af6006380aecf7846e6e0bd.zip |
refactoring some function
Diffstat (limited to 'src')
-rwxr-xr-x | src/Watcher/analysis.py (renamed from src/Watcher/report_creation.py) | 64 | ||||
-rwxr-xr-x | src/Watcher/colored_text.py | 30 | ||||
-rwxr-xr-x | src/Watcher/commands.py | 57 | ||||
-rwxr-xr-x | src/Watcher/time_operations.py | 7 | ||||
-rwxr-xr-x | src/Watcher/week_analysis.py | 46 | ||||
-rw-r--r-- | src/bin/watcher | 5 |
6 files changed, 98 insertions, 111 deletions
diff --git a/src/Watcher/report_creation.py b/src/Watcher/analysis.py index 1e080db..7a96b60 100755 --- a/src/Watcher/report_creation.py +++ b/src/Watcher/analysis.py @@ -1,9 +1,8 @@ -import csv import os -import time_operations as to +import csv from watch_log import get_date -from colored_text import Color import datetime +import time_operations as to def extract_data(date): user = os.getlogin() @@ -55,28 +54,45 @@ def final_report(window_opened, time_spent): return sorted_report -# ░ ▒ █ ─── -#print("▒▒▒\t▒▒▒\n███") +# 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] -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) + return dates - 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}') +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/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") |