diff options
author | 2022-07-08 20:23:17 +0530 | |
---|---|---|
committer | 2022-07-08 20:23:17 +0530 | |
commit | 471d0b43e3fa2f5f077ce9ad4824992058d26e0e (patch) | |
tree | d262855355d683074c21bb39f196e98e4d26cc94 /src | |
parent | af16350b30a9f02974ddc880ee51bf475cb150f8 (diff) | |
download | shopno-os-log-sync-471d0b43e3fa2f5f077ce9ad4824992058d26e0e.tar.gz shopno-os-log-sync-471d0b43e3fa2f5f077ce9ad4824992058d26e0e.zip |
updating log file algorithm | added AFK feature | changing dependency xdotool to ewmh
Diffstat (limited to 'src')
-rwxr-xr-x | src/Watcher/afk.py | 14 | ||||
-rwxr-xr-x | src/Watcher/get_windows.py | 62 | ||||
-rwxr-xr-x | src/Watcher/watch_log.py | 54 |
3 files changed, 76 insertions, 54 deletions
diff --git a/src/Watcher/afk.py b/src/Watcher/afk.py index e69de29..2c54888 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 >= 3000: + 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/get_windows.py b/src/Watcher/get_windows.py index 6b1960f..393c7ff 100755 --- a/src/Watcher/get_windows.py +++ b/src/Watcher/get_windows.py @@ -1,49 +1,55 @@ import os import time +from ewmh import EWMH class window: def __init__(self, class_name, title_name): self.class_name = class_name self.title_name = title_name -# get classname of app that user working on -def active_window(): - # running bash command and storing result as a string - active_window = os.popen("xdotool getwindowfocus getwindowclassname").read() - active_window = active_window[0:-1] - return active_window - # 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/watch_log.py b/src/Watcher/watch_log.py index 2bf5b9f..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,27 +15,17 @@ def get_date(): d = os.popen('''date +"%Y-%m-%d"''').read() return d[0:-1] -def append_line_in_csv(date, opened_time, 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(opened_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: @@ -42,22 +33,33 @@ def log_creation(): csvwriter.writerow([get_time(), "00:00:00", ""]) # appending line at login - with open(filename, 'r') as file: - logged_out_time = file.readlines()[-1][0:8] - append_line_in_csv(get_date(), logged_out_time, get_time(), "User-logged-in") + 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: - opened_at = get_time() - 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() + opened_at = get_time() + time_spent = "00:00:00" + append_line_in_csv(date, opened_at, time_spent, next_actv_window) + + else: date = get_date() - append_line_in_csv(date, opened_at, closed_at, previous_window) + 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) |