-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.py
88 lines (76 loc) · 3.97 KB
/
monitor.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
import datetime
import subprocess
import time
import json
import re
drive = None
def upload():
tracking = drive.CreateFile({"title": "tracking.json", 'mimeType':'application/json', "id": "1PHdJYxWWb7SrudjOeIDiDvK-gGE1JPvY"})
tracking_content = tracking.GetContentString()
lines = str()
for line in open("tracking.json", "r").readlines():
lines += line
tracking_content = lines
tracking.SetContentString(tracking_content)
tracking.Upload()
tracking = None
# Core stuff
def monitor(data):
global drive
track, appsToMonitor, appNames, appIcons, appColors, timeout, uploadPerEpoch, drive = data
try:
monitored = 0 # Iteration of monitor's
while True:
runningApps = re.findall(r"\w{1,}.exe", subprocess.check_output(
"tasklist").decode()) # Get output for tasklist from cmd
start = time.time()
for app in runningApps:
for key, value in appsToMonitor.items():
if app in value:
# print(app, [creationTime.strip(".") for creationTime in re.findall(r"\d{1,}\.", subprocess.check_output(f"wmic process where Name='{app}' get CreationDate").decode())])
try: # Somtimes it causes an error
date = str(min([int(creationTime.strip(".")) for creationTime in re.findall(r"\d{1,}\.", subprocess.check_output(
f"wmic process where Name='{app}' get CreationDate").decode())])) # Get only the max time creation among all child processes
except ValueError:
continue
year = int(date[0:4])
month = int(date[4:6])
day = int(date[7:8])
creationTime = datetime.datetime.strptime(
f"{date[0:4]}/{date[4:6]}/{date[7:8]} {date[8:10]}-{date[10:12]}-{date[12:-1]}", "%Y/%m/%d %H-%M-%S").strftime("%d-%m-%Y %H:%M:%S") # Generate clean date and time
track[app] = {
"creationTime": creationTime,
# Calculate total time spent and use divmod on timedelta object with 60 to get (min's, sec's)[0] -> Take only the minutes
"timeSpentOnWork": divmod((datetime.datetime.now() - datetime.datetime.strptime(creationTime, "%d-%m-%Y %H:%M:%S")).total_seconds(), 60)[0],
"category": key
}
monitored += 1
print(
f"[{monitored:<3}] Processes calculated: {time.time() - start} [{len(track.keys())}]") # Some logging
with open("tracking.json", "r") as file: # Update track dictionary with existing data
try: # If there is no process before adding the value raises error
tracking = json.load(file)
for key, value in tracking.items():
if track.get(key) == None:
track[key] = value
else:
track[key] += value
except:
pass
with open("tracking.json", "w+") as file: # Store the data on each iteration
for key in track.keys():
if key == "lastUpdated":
continue
track[key]["appName"] = appNames[key]
track[key]["appIcon"] = appIcons[appNames[key]]
track[key]["appColor"] = appColors[appNames[key]]
track["lastUpdated"] = datetime.datetime.now().strftime(
"%d-%m-%Y %H:%M:%S")
json.dump(track, file)
if monitored % uploadPerEpoch == 0:
print("Epoch completed, Uploading...", end=" ")
upload()
print("Done.")
time.sleep(timeout)
except KeyboardInterrupt:
return