aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLibravatar Waishnav <waishnavdeore@gmail.com>2022-03-09 00:01:36 +0530
committerLibravatar Waishnav <waishnavdeore@gmail.com>2022-03-09 00:01:36 +0530
commit5e552e7b007f8b13bd3edb221cb162414bac6f03 (patch)
tree4f23442b6d594602b4bdff610255e75357a7a90d /src
parent994d2a2a25a32a003660c61cb9600f22475c76a8 (diff)
downloadshopno-os-log-sync-5e552e7b007f8b13bd3edb221cb162414bac6f03.tar.gz
shopno-os-log-sync-5e552e7b007f8b13bd3edb221cb162414bac6f03.zip
initializing project with bare minimal features
Diffstat (limited to 'src')
-rwxr-xr-xsrc/Watcher/afk.py0
-rwxr-xr-xsrc/Watcher/get_windows.py50
-rwxr-xr-xsrc/Watcher/log_files.py58
-rwxr-xr-xsrc/Watcher/report_creation.py83
-rwxr-xr-xsrc/Watcher/time_operations.py55
-rwxr-xr-xsrc/bin/watcher27
-rw-r--r--src/service/watcher.service10
7 files changed, 283 insertions, 0 deletions
diff --git a/src/Watcher/afk.py b/src/Watcher/afk.py
new file mode 100755
index 0000000..e69de29
--- /dev/null
+++ b/src/Watcher/afk.py
diff --git a/src/Watcher/get_windows.py b/src/Watcher/get_windows.py
new file mode 100755
index 0000000..6b1960f
--- /dev/null
+++ b/src/Watcher/get_windows.py
@@ -0,0 +1,50 @@
+import os
+import time
+
+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]
+ 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
+
+# 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
+ 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
+
+### and whenever the user puts particular command it will make report till the time for that day and shows that report in terminal
diff --git a/src/Watcher/log_files.py b/src/Watcher/log_files.py
new file mode 100755
index 0000000..1d73b95
--- /dev/null
+++ b/src/Watcher/log_files.py
@@ -0,0 +1,58 @@
+import os
+import csv
+import time
+import get_windows as x
+
+# 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, opened_time, window_name):
+ user = os.getlogin()
+ filename = "/home/"+user+"/.cache/Watcher/raw_data/"+date+".csv"
+ Data = [opened_time, window_name]
+ with open(filename, 'a') as csvfile:
+ csvwriter = csv.writer(csvfile, delimiter='\t')
+ csvwriter.writerow(Data)
+
+def is_date_changed(a):
+ result = False
+ while not(result):
+ time.sleep(1)
+ b = get_date()
+ if a != b :
+ result = True
+ else:
+ result = False
+ return result
+
+# 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
+
+ while True:
+ previous_window = x.active_window()
+ if x.is_window_changed(previous_window) and not(afk):
+ next_window = x.active_window()
+ opened_at = get_time()
+ date = get_date()
+ append_line_in_csv(date, opened_at, next_window)
+
+ if afk:
+ opened_at = get_time()
+ append_line_in_csv(date, opened_at, "AFK")
+
+
+if __name__ == "__main__":
+ log_creation()
diff --git a/src/Watcher/report_creation.py b/src/Watcher/report_creation.py
new file mode 100755
index 0000000..3bae496
--- /dev/null
+++ b/src/Watcher/report_creation.py
@@ -0,0 +1,83 @@
+import csv
+import os
+import time_operations as to
+from log_files import get_date
+
+class color:
+ GREY = '\033[30m'
+ PURPLE = '\033[95m'
+ CYAN = '\033[96m'
+ DARKCYAN = '\033[36m'
+ BLUE = '\033[34m'
+ GREEN = '\033[32m'
+ YELLOW = '\033[33m'
+ RED = '\033[31m'
+ BOLD = '\033[1m'
+ UNDERLINE = '\033[4m'
+ END = '\033[0m'
+
+date = get_date()
+def load_raw_data(date):
+ user = os.getlogin()
+ path = "/home/" + user +"/.cache/Watcher/raw_data/"
+ filename = path + date + ".csv"
+ l = list()
+ d = dict()
+
+ with open(filename, 'r') as file:
+ reader = csv.reader(file)
+ for row in reader:
+ for column in row:
+ l.append([column[0:8],column[9::]])
+ d.update({column[9::]: "opened"})
+ #print(column[0:8])
+ #print(column[9::])
+
+ d = list(d)
+ return l, d
+#print(raw_data_list[1][1]
+#print(window_opened)
+def each_session(raw_data_list):
+ time_spent = []
+ for i in range(len(raw_data_list)-1):
+ a = raw_data_list[i][0]
+ b = raw_data_list[i+1][0]
+ time_spent.append([raw_data_list[i][1], to.time_difference(a, b)])
+
+ return time_spent
+
+# creating dictionary to store time spend on each applicaitons
+def final_report(window_opened, time_spent):
+ report = dict()
+
+ for i in window_opened:
+ time = '00:00:00'
+ for j in time_spent:
+ if i == j[0]:
+ #print(j[to.1],i)
+ time = to.time_addition(j[1], time)
+ report.update({i:time})
+ return report
+
+# ░ ▒ █ ───
+#print("▒▒▒\t▒▒▒\n███")
+
+raw_data_list, window_opened = load_raw_data(date)
+time_spent = each_session(raw_data_list)
+
+
+if __name__ == "__main__":
+
+ 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)
+ print(color.YELLOW + "\n Today's Screen-Time \t\t " + color.END + color.BLUE + to.convert_time(Total_screen_time)+ color.END)
+ print("─────────────────────────────────────────────────")
+ print(color.RED + f'{"App Usages":>28}' + color.END)
+ print("─────────────────────────────────────────────────")
+
+ for x,y in final_report(window_opened, time_spent).items():
+ if x == "":
+ x = "HOME-SCREEN"
+ print(color.GREEN + " "+f'{x:<21}' + color.END ,'\t',to.convert_time(y))
+
diff --git a/src/Watcher/time_operations.py b/src/Watcher/time_operations.py
new file mode 100755
index 0000000..09cf1f0
--- /dev/null
+++ b/src/Watcher/time_operations.py
@@ -0,0 +1,55 @@
+
+def time_difference(a,b): # b - a
+ hr = int(b[0:2])-int(a[0:2])
+ mn = int(b[3:5])-int(a[3:5])
+ sec = int(b[6:8])-int(a[6:8])
+ if int(mn) < 0 and int(sec) < 0:
+ hr = hr - 1
+ mn = 60 + mn - 1
+ sec = 60 + sec
+ if hr < 0:
+ hr = hr + 24
+ elif int(mn) < 0 and int(sec) > 0:
+ hr = hr - 1
+ mn = 60 + mn
+ elif int(sec) < 0 and int(mn) > 0:
+ sec = 60 + sec
+ mn = mn - 1
+
+ hr = str(hr).zfill(2)
+ mn = str(mn).zfill(2)
+ sec = str(sec).zfill(2)
+ result = hr + ":" + mn + ":" + sec
+
+ return result
+
+def time_addition(a,b):
+ hr = int(b[0:2]) + int(a[0:2])
+ mn = int(b[3:5]) + int(a[3:5])
+ sec = int(b[6:8]) + int(a[6:8])
+ if mn > 60 and sec > 60:
+ hr = hr + 1
+ mn = mn - 60 + 1
+ sec = sec - 60
+ elif mn > 60:
+ hr = hr + 1
+ mn = mn - 60
+ elif sec > 60:
+ mn = mn + 1
+ sec = sec - 60
+
+ hr = str(hr).zfill(2)
+ mn = str(mn).zfill(2)
+ sec = str(sec).zfill(2)
+ result = hr + ":" + mn + ":" + sec
+
+ return result
+
+def convert_time(t):
+ if int(t[0:2]) == 0:
+ result = t[3:5] + 'm ' + t[6::] + 's'
+ if int(t[3:5]) == 0:
+ result = t[6::] + 's'
+ else:
+ result = t[0:2] + 'h ' + t[3:5] + 'm ' + t[6::] + 's'
+ return result
diff --git a/src/bin/watcher b/src/bin/watcher
new file mode 100755
index 0000000..9be5779
--- /dev/null
+++ b/src/bin/watcher
@@ -0,0 +1,27 @@
+#!/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
+
+import os
+import sys
+sys.path.insert(0, "/usr/share/Watcher/")
+
+import log_files as x
+#import report_creation
+
+#
+arg = sys.argv[1]
+if arg == "-ds":
+ a = os.popen('''python3 /usr/share/Watcher/report_creation.py''').read()
+ print(a)
+
+#elif arg == "daily":
+# print("daily summary")
+#def main() -> None:
+ ...
+
+#if __name__ == "__main__":
+# main()
+
diff --git a/src/service/watcher.service b/src/service/watcher.service
new file mode 100644
index 0000000..85921f1
--- /dev/null
+++ b/src/service/watcher.service
@@ -0,0 +1,10 @@
+[Unit]
+Description=Get the perspective of Watcher
+
+[Service]
+ExecStart=/bin/python /usr/share/Watcher/log_files.py
+Type=simple
+Restart=on-failure
+
+[Install]
+WantedBy=multi-user.target