-
Notifications
You must be signed in to change notification settings - Fork 2
/
Activity.py
113 lines (91 loc) · 3.66 KB
/
Activity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import win32gui
import uiautomation as auto
import time
import datetime
import os
import csv
def getCurrentActivity():
win = win32gui.GetForegroundWindow()
fullName = str(win32gui.GetWindowText(win))
if "https://" in fullName or "http://" in fullName:
fullName = "Pop-up Window"
activityName = fullName.split(' - ')[-1]
return win,activityName,fullName
def getWebsite(win):
try:
control = auto.ControlFromHandle(win)
edit = control.EditControl()
# print(edit.GetValuePattern().Value)
website = str(edit.GetValuePattern().Value).split('/')[0]
except:
website = "-"
return website
def ifFileExists():
return os.path.exists('ActivityData.csv')
def setDataFile():
flag = ifFileExists()
if flag:
flag = not os.stat("ActivityData.csv").st_size == 0
with open('ActivityData.csv','a',newline="") as data:
writer = csv.writer(data)
if not flag:
print("Initializing the file to store data....")
writer.writerow(["Date","Activity Name","isWebBrowser","Website","Start Time","End Time","Total Time(h:m:s)"])
time.sleep(2)
print("Initialization done in ActivityData.csv")
def writeData(todayDate,activityName,isWebBrowser,website,startTime,endTime):
setDataFile()
with open('ActivityData.csv','a',newline="") as data:
writer = csv.writer(data)
writer.writerow([todayDate,activityName,isWebBrowser,website,startTime,endTime,(endTime-startTime)])
if __name__ == "__main__":
os.system('cls')
todayDate = datetime.date.today()
startTime = datetime.datetime.now()
last_activity = ""
last_website = ""
webBrowsers = ["Google Chrome","Mozilla Firefox"]
setDataFile()
while True:
try:
win,activityName,fullName = getCurrentActivity()
endTime = datetime.datetime.now()
website = "-"
if activityName in webBrowsers:
website = getWebsite(win)
if fullName.split(" - ")[0]=="New Tab":
website = "google.com"
if last_activity == "":
last_activity = activityName
if activityName in webBrowsers:
last_website = website
elif activityName != last_activity or (activityName in webBrowsers and website!=last_website) or datetime.date.today()!=todayDate:
#print(last_activity + " " + str(endTime - startTime))
if last_activity in webBrowsers:
#print(last_website)
writeData(todayDate,last_activity,1,last_website,startTime,endTime)
else:
writeData(todayDate,last_activity,0,"-",startTime,endTime)
last_website = website
startTime = endTime
last_activity = activityName
todayDate = datetime.date.today()
except KeyboardInterrupt:
print("Do you want to quit? (Y or y to quit)")
choice = input("Choice : ")
if choice.lower() == "y":
if last_activity in webBrowsers:
#print(last_website)
writeData(todayDate,last_activity,1,last_website,startTime,endTime)
last_website = website
else:
writeData(todayDate,last_activity,0,"-",startTime,endTime)
print("Exiting!")
break
else:
print("Continuing!")
continue
except UnicodeEncodeError:
last_activity = ""
startTime = endTime
continue