diff options
m--------- | Watcher | 6 | ||||
-rwxr-xr-x | install | 3 | ||||
-rwxr-xr-x | misc/export_to_2.0 | 83 | ||||
-rwxr-xr-x | src/Watcher/afk.py | 2 | ||||
-rwxr-xr-x | src/Watcher/analysis.py | 70 | ||||
-rwxr-xr-x | src/Watcher/commands.py | 5 | ||||
-rwxr-xr-x | src/Watcher/get_windows.py | 28 | ||||
-rwxr-xr-x | src/Watcher/time_operations.py | 13 | ||||
-rwxr-xr-x | src/Watcher/watch_log.py | 95 | ||||
-rwxr-xr-x | src/bin/watcher | 2 |
10 files changed, 195 insertions, 112 deletions
diff --git a/Watcher b/Watcher new file mode 160000 +Subproject ac72b6cdab6a7dfc6aed570a559688ee33292ac @@ -1,4 +1,5 @@ #!/bin/bash +git clone https://github.com/Waishnav/Watcher -b v2.0 && cd Watcher && ./install echo "[✔] First of all Thanks for dropping by!." sleep 1s @@ -18,7 +19,7 @@ echo "[✔] Making it executable by giving it permission" # making directory for log-files (where all you daily logs are stored) mkdir -p ~/.cache/Watcher/ echo "[✔] To store raw_data making directory as ~/.cache/Watcher" -mkdir -p ~/.cache/Watcher/raw_data/ +mkdir -p ~/.cache/Watcher/daily_data/ mkdir -p ~/.cache/Watcher/Analysis/ # deleting folowing lines "[ -f /etc/xprofile ] && . /etc/xprofile/" and "[ -f ~/.xprofile ] && . ~/.xprofile" diff --git a/misc/export_to_2.0 b/misc/export_to_2.0 new file mode 100755 index 0000000..ef30ffe --- /dev/null +++ b/misc/export_to_2.0 @@ -0,0 +1,83 @@ +#!/usr/bin/python +import os +import sys +sys.path.insert(0, "/usr/share/Watcher/") +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") + if "AFK" in report.keys(): + report.pop("AFK") + if "Unknown" in report.keys(): + report.pop("Unknown") + # 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 + +def export_to_new(date): + window_opened, time_spent = extract_data(date) + sorted_report = final_report(window_opened, time_spent) + filename = "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+date+".csv" + overwrite_Data = [] + with open(filename, 'w') as csvfile: + for x,y in sorted_report.items(): + overwrite_Data.append([y,x]) + + csvwriter = csv.writer(csvfile, delimiter='\t') + csvwriter.writerows(overwrite_Data) + + del sorted_report + del overwrite_Data + +path = "/home/" + os.getlogin() +"/.cache/Watcher/raw_data/" +for file in os.listdir(path): + export_to_new(file[:-4]) + + diff --git a/src/Watcher/afk.py b/src/Watcher/afk.py index 41f8fe9..3d92068 100755 --- a/src/Watcher/afk.py +++ b/src/Watcher/afk.py @@ -15,7 +15,7 @@ def returned_from_afk(afk_active, timeout): return has_returned def is_afk(timeout): - timeout = timeout * 60 * 1000 - 200 # minimizing 200 milisec error + timeout = timeout * 60 * 1000 - 100 # minimizing 100 milisec error #If the AFK feature is installed time_since_last_input = int(os.popen("xprintidle").read()[:-1]) if (time_since_last_input > timeout): diff --git a/src/Watcher/analysis.py b/src/Watcher/analysis.py index 6e26d29..0e917f8 100755 --- a/src/Watcher/analysis.py +++ b/src/Watcher/analysis.py @@ -4,45 +4,26 @@ 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/" +# creating dictionary to store time spend on each applicaitons on that particular day +def final_report(date): + path = "/home/" + os.getlogin() +"/.cache/Watcher/daily_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}) + if os.path.isfile(filename): + with open(filename, 'r') as f: + raw_data = f.readlines() + for x in raw_data: + x = x.split('\t') + a = {x[1][:-1]:x[0]} + report.update(a) #print(report) - if "User-logged-in" in report.keys(): - report.pop("User-logged-in") - if "AFK" in report.keys(): - report.pop("AFK") if "Unknown" in report.keys(): report.pop("Unknown") + return report + +def sort_data(report): # sort report dictonary in decreasing order of Usages sorted_values = [] for x,y in report.items(): @@ -55,9 +36,11 @@ def final_report(window_opened, time_spent): for x, y in report.items(): if to.convert_into_sec(y) == i: sorted_report.update({x:y}) - + del report + del sorted_values return sorted_report + def get_sunday_of_week(week): year = int(week[4:]) week = int(week[1:3]) @@ -84,28 +67,29 @@ def weekly_logs(week = str(os.popen('''date +"W%V-%Y"''').read()[0:-1])): filename = "/home/"+user+"/.cache/Watcher/Analysis/"+week+".csv" with open(filename, "w") as csvfile: csvwriter = csv.writer(csvfile, delimiter='\t') - #csvwriter.writerow(["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]) if os.popen('''date +"W%V-%Y"''').read()[0:-1] == week: dates = week_dates() else: dates = week_dates(get_sunday_of_week(week)) - 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(): + for x, y in final_report(i).items(): Total_screen_time = to.time_addition(y, Total_screen_time) - csvwriter.writerow([weekday_from_date(i), Total_screen_time]) + all_data = dict() 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(): + for x, y in final_report(i).items(): + usage = all_data.get(x) + if usage == None: + usage = "00:00:00" + y = to.time_addition(usage, y) + all_data.update({x:y}) + + all_data = sort_data(all_data) + for x, y in all_data.items(): csvwriter.writerow([y, x]) #testing diff --git a/src/Watcher/commands.py b/src/Watcher/commands.py index 7d1fe97..8805dac 100755 --- a/src/Watcher/commands.py +++ b/src/Watcher/commands.py @@ -35,9 +35,8 @@ class Color: return '\033[4m' + text + '\033[0m' def daily_summary(date = get_date()): - 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(): + for x,y in anls.final_report(date).items(): Total_screen_time = to.time_addition(y, Total_screen_time) if date == get_date(): @@ -66,7 +65,7 @@ def daily_summary(date = get_date()): print(Color.RED(f'{" App Usages":>29}')) print(" ────────────────────────────────────────────────") - for x,y in anls.final_report(window_opened, time_spent).items(): + for x,y in anls.sort_data(anls.final_report(date)).items(): if x == "": x = "Home-Screen" print(" " + Color.GREEN(f'{x:<22}') + '\t ',f'{to.format_time(y):>12}') diff --git a/src/Watcher/get_windows.py b/src/Watcher/get_windows.py index 0769eb9..a0be469 100755 --- a/src/Watcher/get_windows.py +++ b/src/Watcher/get_windows.py @@ -1,6 +1,4 @@ import os -import time -from afk import get_afk_status # get title name of app that user working on def active_window_title(): @@ -42,26 +40,10 @@ def active_window(): return active_window # returns true if user has move to next app which is not the same as previous -def is_window_changed(a, afk, timeout): - result = False - while not(result): - time.sleep(0.5) - b = active_window() - if a != b : - result = True - elif get_afk_status(afk, timeout): - result = True - else: - result = False - return result +def previous_window(array_of_window, active_window): + array_of_window.remove(active_window) + array_of_window.append(active_window) + return array_of_window[-2] - -### 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 - -### and whenever the user puts particular command it will make report till the time for that day and shows that report in terminal if __name__ == "__main__": - while True: - time.sleep(1) - print(active_window_title()) - print(os.popen('''xdotool getwindowfocus getwindowname''').read()) + print(active_window()) diff --git a/src/Watcher/time_operations.py b/src/Watcher/time_operations.py index 1df4922..5042983 100755 --- a/src/Watcher/time_operations.py +++ b/src/Watcher/time_operations.py @@ -66,4 +66,17 @@ def convert_into_sec(t): sec = int(t[0:2])*3600 + int(t[3:5])*60 + int(t[6::]) return sec +def convert(sec): + sec = int(sec) + sec = sec % (24 * 3600) + hr = sec // 3600 + sec %= 3600 + mn = sec // 60 + sec %= 60 + hr = str(hr).zfill(2) + mn = str(mn).zfill(2) + sec = str(sec).zfill(2) + + result = hr + ":" + mn + ":" + sec + return result diff --git a/src/Watcher/watch_log.py b/src/Watcher/watch_log.py index cc85a23..6a44ca5 100755 --- a/src/Watcher/watch_log.py +++ b/src/Watcher/watch_log.py @@ -3,7 +3,7 @@ import csv import time import get_windows as x import afk as y -from time_operations import time_difference +from time_operations import time_difference, time_addition, convert # get current time whenever the function is called def get_time(): @@ -15,59 +15,74 @@ 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): - user = os.getlogin() - 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: +def update_csv(date, Data): + filename = "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+date+".csv" + overwrite_Data = [] + with open(filename, 'w') as csvfile: + for x,y in Data.items(): + overwrite_Data.append([y,x]) + csvwriter = csv.writer(csvfile, delimiter='\t') - csvwriter.writerow(Data) + csvwriter.writerows(overwrite_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 +def import_data(file): + with open(file, 'r') as f: + raw_data = f.readlines() + data = dict() + #l = [] + for x in raw_data: + x = x.split('\t') + a = {x[1][:-1]:x[0]} + #l.append(x[1][:-1]) + data.update(a) + return data + # 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" + filename = "/home/"+os.getlogin()+"/.cache/Watcher/daily_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", ""]) - logout_time = os.popen("tail -n1 " + filename).read().split("\t")[0] - append_line_in_csv(get_date(), logout_time, get_time(), "User-logged-in") + creat_file = "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+get_date()+".csv" + with open(creat_file, 'w') as fp: + pass afk = False - afkTimeout = 3 # timeout in minutes - + afkTimeout = 1 # timeout in minutes + data = import_data(filename) while True: - opened_at = get_time() - previous_window = x.active_window() + date = get_date() + filename = "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+date+".csv" + afk = y.is_afk(afkTimeout) + print(data) - if (y.returned_from_afk(afk, afkTimeout)): - previous_window = "AFK" - if os.path.exists(filename): - opened_at = os.popen(" tail -n1 "+ filename).read().split("\t")[0] - else: - previous_date = os.popen("""date -d "1 day ago" '+%Y-%m-%d'""").read()[:-1] - opened_at = os.popen(" tail -n1 ~/.cache/Watcher/raw_data/"+previous_date+".csv").read().split("\t")[0] - afk = False + if not(afk): + active_window = x.active_window() + usage = data.get(active_window) + if usage == None: + usage = "00:00:00" - if (x.is_window_changed(previous_window, afk, afkTimeout) and not afk): - if(y.is_afk(afkTimeout)): - afk = True - # minimizing error - closed_at = time_difference("00:02:58", get_time()) - else: - closed_at = get_time() # for next_window its the opening time + time.sleep(1) + if y.is_afk(afkTimeout): + afk_time = int(round(int(os.popen("xprintidle").read()[:-1])/1000, 0)) + usage = time_difference(convert(afk_time), usage) - date = get_date() - filename = "/home/"+os.getlogin()+"/.cache/Watcher/raw_data/"+date+".csv" - append_line_in_csv(date, opened_at, closed_at, previous_window) + usage = time_addition("00:00:01", usage) + data.update({active_window : usage}) + if os.path.isfile("/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+get_date()+".csv"): + update_csv(get_date(), data) + elif not(os.path.isfile("/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+get_date()+".csv")): + new_filename = "/home/"+os.getlogin()+"/.cache/Watcher/daily_data/"+get_date()+".csv" + with open(new_filename, 'w') as fp: + pass + + data.clear() + if __name__ == "__main__": - #log_creation() - filename = "/home/"+os.getlogin()+"/.cache/Watcher/raw_data/"+get_date()+".csv" - opened_at = os.popen(" tail -n1 "+ filename).read().split("\t")[0] - print(opened_at) + log_creation() + #afk_time = int(round(int(os.popen("xprintidle").read()[:-1])/1000, 0)) + #print(afk_time) + diff --git a/src/bin/watcher b/src/bin/watcher index 396aef6..0dde744 100755 --- a/src/bin/watcher +++ b/src/bin/watcher @@ -46,7 +46,7 @@ if len(arg) == 2: print("Log creations started... \nif you wanna stop it, use keyboard-shortcut (Ctrl+Shift+C or Ctrl+C)") x.log_creation() elif arg[1] == "--version" or arg[1] == "-v": - print("Version: 1.3.1") + print("Version: 2.0.0") else: wrong_option() |