diff options
-rwxr-xr-x | src/Watcher/afk.py | 25 | ||||
-rwxr-xr-x | src/Watcher/get_windows.py | 33 | ||||
-rwxr-xr-x | src/Watcher/watch_log.py | 58 |
3 files changed, 60 insertions, 56 deletions
diff --git a/src/Watcher/afk.py b/src/Watcher/afk.py index e309231..4f2b1f7 100755 --- a/src/Watcher/afk.py +++ b/src/Watcher/afk.py @@ -1,14 +1,19 @@ 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 + +# checks if currently in afk mode, only returns true once +def get_afk_status(afk_active, timeout): + if (is_afk(timeout)): + return True + elif returned_from_afk(afk_active, timeout): + return True else: return False +def returned_from_afk(afk_active, timeout): + has_returned = (afk_active and not (is_afk(timeout))) + return has_returned + +def is_afk(timeout): + timeout = timeout * 60 * 1000 + #If the AFK feature is installed + return (int((os.popen("xprintidle").read())) > timeout) diff --git a/src/Watcher/get_windows.py b/src/Watcher/get_windows.py index f895662..8cfbc70 100755 --- a/src/Watcher/get_windows.py +++ b/src/Watcher/get_windows.py @@ -1,12 +1,8 @@ import os import time +from afk import get_afk_status from ewmh import EWMH -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(): try: @@ -35,21 +31,30 @@ def active_window(): 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" + try: + if "Nvim" in aw_title: + active_window = "NeoVim" + elif "Vim" in aw_title: + active_window = "Vim" + except TypeError: + None return active_window # returns true if user has move to next app which is not the same as previous -def is_window_changed(a): +def is_window_changed(a, afk, timeout): result = False - time.sleep(0.5) - b = active_window() - if a != b : - result = True + while not(result): + time.sleep(1) + b = active_window() + if a != b : + result = True + elif get_afk_status(afk, timeout): + result = True + else: + result = False 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/watch_log.py b/src/Watcher/watch_log.py index c8fbc95..250670d 100755 --- a/src/Watcher/watch_log.py +++ b/src/Watcher/watch_log.py @@ -2,8 +2,8 @@ import os import csv import time import get_windows as x -import afk -from time_operations import time_difference, time_addition +import afk as y +from time_operations import time_difference # get current time whenever the function is called def get_time(): @@ -15,15 +15,19 @@ def get_date(): d = os.popen('''date +"%Y-%m-%d"''').read() return d[0:-1] -def append_line_in_csv(date, opened_time, time_spent, window_name): +def append_line_in_csv(date, opened_time, closed_time, window_name): user = os.getlogin() - filename = "/home/"+user+"/.cache/Watcher/raw_data/"+date+".csv" - Data = [opened_time, time_spent, window_name] + time_spent = time_difference(opened_time, closed_time) + filename = "/home/"+os.getlogin()+"/.cache/Watcher/raw_data/"+date+".csv" + Data = [closed_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) def log_creation(): filename = "/home/"+os.getlogin()+"/.cache/Watcher/raw_data/"+get_date()+".csv" @@ -31,37 +35,27 @@ def log_creation(): with open(filename, 'a') as csvfile: csvwriter = csv.writer(csvfile, delimiter='\t') csvwriter.writerow([get_time(), "00:00:00", ""]) + logout_time = os.popen("tail -n1 " + filename).read().split("\t")[0] + append_line_in_csv(get_date(), logout_time, 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") + afk = False + afkTimeout = 3 # timeout in minutes while True: - actv_window = x.active_window() - if x.is_window_changed(actv_window): - next_actv_window = x.active_window() - date = get_date() - opened_at = get_time() - time.sleep(0.5) - time_spent = "00:00:01" - append_line_in_csv(date, opened_at, time_spent, next_actv_window) + opened_at = get_time() + previous_window = x.active_window() - else: - actv_window = x.active_window() - 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(1) - time_spnt = time_difference(opened_at, get_time()) + if (y.returned_from_afk(afk, afkTimeout)): + previous_window = "AFK" + afk = False - 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 (x.is_window_changed(previous_window, afk, afkTimeout) and not afk): + if(y.is_afk(afkTimeout)): + afk = True + closed_at = get_time() # for next_window its the opening time + date = get_date() + filename = "/home/"+os.getlogin()+"/.cache/Watcher/raw_data/"+date+".csv" + append_line_in_csv(date, opened_at, closed_at, previous_window) if __name__ == "__main__": - opened_at = os.popen("""tail -n1 ~/.cache/Watcher/raw_data/2022-07-07.csv""").read().split("\t")[1] - print(opened_at) + log_creation() |