From 0a411d21a4fe70a6db8496114a6dd49dd4cf345a Mon Sep 17 00:00:00 2001 From: Waishnav Date: Fri, 11 Mar 2022 23:40:58 +0530 Subject: updating executable file and added function in time_operation module --- src/Watcher/colored_text.py | 2 +- src/Watcher/log_files.py | 54 -------------------------------------- src/Watcher/time_operations.py | 5 ++++ src/Watcher/watch_log.py | 59 ++++++++++++++++++++++++++++++++++++++++++ src/bin/watcher | 53 +++++++++++++++++++++++++++---------- src/service/watcher.service | 2 +- 6 files changed, 105 insertions(+), 70 deletions(-) delete mode 100755 src/Watcher/log_files.py create mode 100755 src/Watcher/watch_log.py (limited to 'src') diff --git a/src/Watcher/colored_text.py b/src/Watcher/colored_text.py index b143829..162510d 100755 --- a/src/Watcher/colored_text.py +++ b/src/Watcher/colored_text.py @@ -1,7 +1,7 @@ class Color: def GREY(text): - return '\033[30m' + text + '\033[0m' + return '\033[90m' + text + '\033[0m' def BLUE(text): return '\033[34m' + text + '\033[0m' diff --git a/src/Watcher/log_files.py b/src/Watcher/log_files.py deleted file mode 100755 index 9dfc713..0000000 --- a/src/Watcher/log_files.py +++ /dev/null @@ -1,54 +0,0 @@ -import os -import csv -import time -import get_windows as x -from time_operations import time_difference - -# get current time whenever the function is called -def get_time(): - t = os.popen('''date +"%T"''').read() - return t[0:-1] - -# get date of today -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): - 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] - 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 - append_line_in_csv(get_date(), get_time(), "User-loged-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 - date = get_date() - append_line_in_csv(date, closed_at, previous_window) - - if afk: - afk_closed_time = get_time() - append_line_in_csv(date, afk_closed_time, "AFK") - - -if __name__ == "__main__": - log_creation() diff --git a/src/Watcher/time_operations.py b/src/Watcher/time_operations.py index 985a5e8..4145e8d 100755 --- a/src/Watcher/time_operations.py +++ b/src/Watcher/time_operations.py @@ -53,3 +53,8 @@ def convert_time(t): else: result = t[0:2] + 'h ' + t[3:5] + 'm ' + t[6::] + 's' return result + +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 new file mode 100755 index 0000000..3ac7aa4 --- /dev/null +++ b/src/Watcher/watch_log.py @@ -0,0 +1,59 @@ +import os +import csv +import time +import get_windows as x +from time_operations import time_difference + +# get current time whenever the function is called +def get_time(): + t = os.popen('''date +"%T"''').read() + return t[0:-1] + +# get date of today +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): + 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] + 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 + append_line_in_csv(get_date(), get_time(), "User-loged-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 + date = get_date() + append_line_in_csv(date, closed_at, previous_window) + + if afk: + afk_closed_time = get_time() + append_line_in_csv(date, afk_closed_time, "AFK") + + +if __name__ == "__main__": + 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", ""]) + + log_creation() diff --git a/src/bin/watcher b/src/bin/watcher index 9be5779..126341a 100755 --- a/src/bin/watcher +++ b/src/bin/watcher @@ -1,27 +1,52 @@ #!/usr/bin/python ## watcher --start == will starts the process of making csv -# watcher --daily == will give you your day overview till that point -# watcher --week == will gives user their week overview of screen time +# watcher --daily-summary == will give you your day overview till that point +# watcher --week-summary == will gives user their week overview of screen time import os import sys sys.path.insert(0, "/usr/share/Watcher/") +import argparse +import watch_log as x +from colored_text import Color +import report_creation as rc -import log_files as x -#import report_creation +def help_option(): + print(Color.BLUE("Watcher") + " - Minimal open source screen-time tracker\n") + print(Color.YELLOW("USAGE:")+"\n\t watcher [OPTION]\n") + print(Color.YELLOW("OPTIONS")) + print("\t-ds, --daily-summary Displays where you have spend your time of that day") + print("\t-ws, --week-summary Displays screen-time of each day of week") + print("\nFor more information see github repo: https://github.com/Waishnav/Watcher") -# + +#print("▒▒▒\t▒▒▒\n███") arg = sys.argv[1] -if arg == "-ds": - a = os.popen('''python3 /usr/share/Watcher/report_creation.py''').read() - print(a) +if arg == "-ds" or arg == "--daily-summary": + rc.daily_summary() +elif arg == "-ws" or arg == "--week-summary": + rc.week_summary() +elif arg == "-h" or arg == "--help": + help_option() +elif arg == "--start": + x.log_creation() +elif arg == "--version": + print("Version: 1.0") +else: + print(Color.RED("Wrong")+" [OPTION] choosen. Have a look at the Options!!\n") + print(Color.YELLOW("OPTIONS")) + print("\t-ds, --daily-summary Displays where you have spend your time of that day") + print("\t-ws, --week-summary Displays screen-time of each day of week") -#elif arg == "daily": -# print("daily summary") -#def main() -> None: - ... +#parser = argparse.ArgumentParser(description="Minimal open source screen-time calulator for digitally wellbeing") +#parser.add_argument() +#args = parser.parse_args() +#print(args) -#if __name__ == "__main__": -# main() +#def main(args=None): +# if arg is None: +# print("") +# else: +# print('''▒▒▒ ███ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒ ▒▒▒\n▒▒▒ ███ ▒▒▒ ███ ▒▒▒ ███ ███\n███ ███ ███ ███ ▒▒▒ ███ ███\n███ ███ ███ ███ ███ ███ ███\n███ ███ ███ ███ ███ ███ ███\n███ ███ ███ ███ ███ ███ ███\n███ ███ ███ ███ ███ ███ ███''') diff --git a/src/service/watcher.service b/src/service/watcher.service index 85921f1..803f2ee 100644 --- a/src/service/watcher.service +++ b/src/service/watcher.service @@ -1,5 +1,5 @@ [Unit] -Description=Get the perspective of Watcher +Description=Get the perspective of Watcher of your Screen-Time [Service] ExecStart=/bin/python /usr/share/Watcher/log_files.py -- cgit v1.2.3