diff options
author | 2022-07-09 00:11:48 +0530 | |
---|---|---|
committer | 2022-07-09 00:11:48 +0530 | |
commit | f0e484878af5b86b723de6af5f506f8b18e66ddc (patch) | |
tree | 0e688955119acf9598a85fdcba77ce21c104b327 /src | |
parent | 255487a34700d6790a2771a177c16991caaca72f (diff) | |
parent | ac79bbea7b113613bf160512064b8e2478181db9 (diff) | |
download | shopno-os-log-sync-f0e484878af5b86b723de6af5f506f8b18e66ddc.tar.gz shopno-os-log-sync-f0e484878af5b86b723de6af5f506f8b18e66ddc.zip |
Update 1.2 | AFK feature | New algorithm to update log file at each second
Diffstat (limited to 'src')
-rwxr-xr-x | src/Watcher/afk.py | 14 | ||||
-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/get_windows.py | 66 | ||||
-rwxr-xr-x | src/Watcher/time_operations.py | 6 | ||||
-rwxr-xr-x | src/Watcher/watch_log.py | 64 | ||||
-rwxr-xr-x | src/Watcher/week_analysis.py | 46 | ||||
-rw-r--r--[-rwxr-xr-x] | src/bin/watcher | 12 |
9 files changed, 183 insertions, 176 deletions
diff --git a/src/Watcher/afk.py b/src/Watcher/afk.py index e69de29..e309231 100755 --- a/src/Watcher/afk.py +++ b/src/Watcher/afk.py @@ -0,0 +1,14 @@ +import os +def IsAFK(): + time_since_last_input = int(os.popen("xprintidle").read()) + if time_since_last_input >= 300000: # 3min no input == AFK + video_playback = os.popen("""pacmd list-sink-inputs | grep -w state | grep -i 'CORKED'""").read() + # if playback is not running as well as user is AFK + if "CORKED" in video_playback: + return True + # if playback is running is background as well as user is AFK + else: + return False + else: + return False + 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/get_windows.py b/src/Watcher/get_windows.py index 6e7b3f2..393c7ff 100755 --- a/src/Watcher/get_windows.py +++ b/src/Watcher/get_windows.py @@ -1,45 +1,55 @@ import os import time +from ewmh import EWMH -# get classname of app that user working on -def active_window(): - # above command gives error on ubuntu cause of xdotool version is too old there while on arch it works -# active_window = os.popen("xdotool getwindowfocus getwindowclassname").read() - active_window_id = os.popen("xdotool getactivewindow").read()[:-1] - active_window = os.popen("xprop -id "+ str(active_window_id) +" | grep CLASS").read()[18::].split(",")[1].replace('''"''', "") - return active_window +class window: + def __init__(self, class_name, title_name): + self.class_name = class_name + self.title_name = title_name # get title name of app that user working on def active_window_title(): - active_window_title = os.popen("xdotool getwindowfocus getwindowname").read() - active_window_title = active_window_title[0:-1] + try: + win = EWMH().getActiveWindow() + active_window_title = win.get_wm_name() + except AttributeError: + active_window_title = "unknown" + active_window_title = active_window_title.capitalize() return active_window_title -# get list of opened apps in background as well as in foreground -def opened_windows_list(): - raw_data = os.popen('''wmctrl -lx | awk '{print $3}' ''').read() - raw_data_ls = raw_data.split('\n') - windows_list = [] - for x in raw_data_ls: - last = x.rfind(".") - windows_list.append(x[last+1::]) - windows_list.remove('') - windows_list = list(set(windows_list)) - return windows_list +# get classname of app that user working on +def active_window(): + try: + win = EWMH().getActiveWindow() + active_window = win.get_wm_class()[1] + except AttributeError: + active_window = "unknown" + + if len(active_window) > 20: + active_window = "unknown" + elif "\n" in active_window: + active_window = "unknown" + active_window = active_window.capitalize() + + # check whether user is using nvim or vim + aw_title = active_window_title() + terminals = ["Kitty", "Alacritty", "Terminator", "Tilda", "Guake", "Yakuake", "Roxterm", "Eterm", "Rxvt", "Xterm", "Tilix", "Lxterminal", "Konsole", "St", "Gnome-terminal", "Xfce4-terminal", "Terminology", "Extraterm"] + if active_window in terminals: + if "Nvim" in aw_title: + active_window = "NeoVim" + elif "Vim" in aw_title: + active_window = "Vim" + return active_window # returns true if user has move to next app which is not the same as previous def is_window_changed(a): result = False - while not(result): - time.sleep(1) - b = active_window() - if a != b : - result = True - else: - result = False + time.sleep(0.1) + b = active_window() + if a != b : + result = True return result - ### what to do after window get change I've to append one line in csv data file in following format ### opened-time closed-time time-spent window_class_name window_title_name diff --git a/src/Watcher/time_operations.py b/src/Watcher/time_operations.py index df593b8..b7d3224 100755 --- a/src/Watcher/time_operations.py +++ b/src/Watcher/time_operations.py @@ -14,13 +14,15 @@ def time_difference(a,b): # b - a mn = 60 + mn if hr < 0: hr = hr + 24 +<<<<<<< HEAD +======= +>>>>>>> afk_feature 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 @@ -52,7 +54,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): @@ -69,3 +70,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/watch_log.py b/src/Watcher/watch_log.py index 1359ecc..4956deb 100755 --- a/src/Watcher/watch_log.py +++ b/src/Watcher/watch_log.py @@ -2,7 +2,8 @@ import os import csv import time import get_windows as x -from time_operations import time_difference +import afk +from time_operations import time_difference, time_addition # get current time whenever the function is called def get_time(): @@ -14,58 +15,51 @@ def get_date(): d = os.popen('''date +"%Y-%m-%d"''').read() return d[0:-1] -def append_line_in_csv(date, closed_time, window_name): +def append_line_in_csv(date, opened_time, time_spent, window_name): user = os.getlogin() filename = "/home/"+user+"/.cache/Watcher/raw_data/"+date+".csv" - with open(filename, 'r') as file: - last_app_time = file.readlines()[-1][0:8] - - time_spent = time_difference(last_app_time, closed_time) - - Data = [closed_time, time_spent, window_name] + Data = [opened_time, time_spent, window_name] with open(filename, 'a') as csvfile: csvwriter = csv.writer(csvfile, delimiter='\t') csvwriter.writerow(Data) # Expected Behaviour == if date got changed then append line in new csv file after initializing the csv file -# also if usr is AFK then append line -# TODO: AFK feature devlopement (it will be developed after completing alpha product (after whole project up end running) - -afk = False def log_creation(): - global afk - filename = "/home/"+os.getlogin()+"/.cache/Watcher/raw_data/"+get_date()+".csv" if not(os.path.isfile(filename)): with open(filename, 'a') as csvfile: csvwriter = csv.writer(csvfile, delimiter='\t') csvwriter.writerow([get_time(), "00:00:00", ""]) - append_line_in_csv(get_date(), get_time(), "User-logged-in") + + # appending line at login + logged_out_time = os.popen("tail -n1 " + filename).read().split("\t")[0] + time_away_from_laptop = time_difference(logged_out_time, get_time()) + append_line_in_csv(get_date(), get_time(), time_away_from_laptop, "User-logged-in") while True: - previous_window = x.active_window() - if x.is_window_changed(previous_window) and not(afk): - next_window = x.active_window() - closed_at = get_time() # for next_window its the opening time + actv_window = x.active_window() + if x.is_window_changed(actv_window): + next_actv_window = x.active_window() date = get_date() - filename = "/home/"+os.getlogin()+"/.cache/Watcher/raw_data/"+date+".csv" - if not(os.path.isfile(filename)): - with open(filename, 'a') as csvfile: - csvwriter = csv.writer(csvfile, delimiter='\t') - prev_date = os.popen("""date -d "yesterday" '+%Y-%m-%d'""").read()[0:-1] - prev_file = "/home/"+os.getlogin()+"/.cache/Watcher/raw_data/"+prev_date+".csv" - with open(prev_file, 'r') as file: - last_app_time = file.readlines()[-1][0:8] - csvwriter.writerow([get_time(), time_difference(last_app_time, closed_at), previous_window]) + opened_at = get_time() + time_spent = "00:00:00" + append_line_in_csv(date, opened_at, time_spent, next_actv_window) - else: - # appends line when app gets closed - append_line_in_csv(date, closed_at, previous_window) + else: + date = get_date() + now = get_time() # for next_window its the opening time + last_line = os.popen("tail -n1 " + filename).read().split("\t") + opened_at = last_line[0] + os.popen("""sed -i -e '$ d' """ + filename) + time.sleep(0.2) + time_spnt = time_difference(opened_at, get_time()) - if afk: - afk_closed_time = get_time() - append_line_in_csv(date, afk_closed_time, "AFK") + if afk.IsAFK(): + append_line_in_csv(date, opened_time, time_spent, "AFK") + else: + append_line_in_csv(date, opened_at, time_spnt, actv_window) if __name__ == "__main__": - log_creation() + opened_at = os.popen("""tail -n1 ~/.cache/Watcher/raw_data/2022-07-07.csv""").read().split("\t")[1] + print(opened_at) 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 2835a7f..4bea832 100755..100644 --- a/src/bin/watcher +++ b/src/bin/watcher @@ -1,11 +1,11 @@ -#!/usr/bin/python3 - +#!/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 +from analysis import weeklly_logs def help_option(): print(Color.BLUE("Watcher") + " - Minimal open source screen-time tracker\n") @@ -23,15 +23,15 @@ 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") + 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.0") + print("Version: 1.2") else: print(Color.RED("Wrong")+" [OPTION] choosen. Have a look at the Options!!\n") print(Color.YELLOW("OPTIONS")) |