Skip to content

Commit 75d5582

Browse files
committed
This commit Adds new time_tracker in Python
1 parent 1a6ecc3 commit 75d5582

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

Python/Desktop Time Tracker/Readme.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Time Tracker
2+
## Description
3+
This script is written in python and can be used to track user time used on Desktop Applications in a pie chart format
4+
* The script is mainly built using [xdotool](https://www.freebsd.org/cgi/man.cgi?query=xdotool&apropos=0&sektion=1&manpath=FreeBSD+8.1-RELEASE+and+Ports&format=html)
5+
6+
### Language
7+
- [X] Python
8+
9+
### Installation
10+
```bash
11+
$ sudo apt-get install xdotool
12+
$ pip install subprocess
13+
$ pip install time
14+
$ pip install matplotlib
15+
$ pip install yaml
16+
```
17+
18+
### Usage
19+
Run
20+
```bash
21+
python time_tracker.py
22+
```
23+
The outpus should be in script directory:
24+
25+
![pie_chart_example](time_tracking_pie_chart.png)
26+
#### Steps to create the virtual environment (for windows)
27+
1. pip install virtualenv
28+
2. pip install pipenv
29+
3. pipenv --help (for help)
30+
4. pipenv install
31+
5. pipenv shell
32+
6. pip install the packages
33+
7. execute the program
34+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
update_frequency: 1
2+
pie_chart_update_frequency: 1
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import subprocess
4+
import time
5+
from argparse import ArgumentParser
6+
7+
import matplotlib.pyplot as plt
8+
import yaml
9+
10+
11+
def execute_terminal_command(command):
12+
"""
13+
THIS FUNCTION EXECUTES A TERMINAL COMMAND USING SUBPROCESS MODULE
14+
"""
15+
try:
16+
return subprocess.check_output(command).decode("utf-8").strip()
17+
except subprocess.CalledProcessError:
18+
pass
19+
20+
21+
def update_pie_chart(labels, sizes, log_dir):
22+
"""
23+
THIS FUNCTION SAVES/UPDATES PIE CHART USING: labels, sizes
24+
AND SAVES IT INTO log_dir
25+
"""
26+
# Plotting pie chart
27+
fig = plt.figure()
28+
plt.pie(sizes, labels=labels, autopct="%1.1f%%", shadow=True, startangle=140)
29+
plt.axis("equal")
30+
plt.savefig(logdir + "/time_tracking_pie_chart.png")
31+
plt.close(fig)
32+
33+
34+
if __name__ == "__main__":
35+
parser = ArgumentParser()
36+
parser.add_argument(
37+
"--config",
38+
type=str,
39+
required=False,
40+
default="config.yaml",
41+
help="path to config",
42+
)
43+
args = parser.parse_args()
44+
45+
with open(args.config, mode="r") as fp:
46+
config = yaml.safe_load(fp)
47+
48+
update_frequency = config["update_frequency"] # Uodate time in seconds
49+
pie_chart_update_frequency = config[
50+
"pie_chart_update_frequency"
51+
] # Uodate time in seconds
52+
applist = [] # List that stores all apps names
53+
winlist = [] # List that stores corresponding information to those apps in applist
54+
55+
logdir = os.getcwd() # Logs folder within the same working directory
56+
57+
while True:
58+
time.sleep(update_frequency) # Wait for the update frequency
59+
frpid = execute_terminal_command(
60+
["xdotool", "getactivewindow", "getwindowpid"]
61+
) # Get the current opened window process id
62+
frname = execute_terminal_command(
63+
["xdotool", "getactivewindow", "getwindowname"]
64+
) # Get the current opened window name
65+
66+
app = execute_terminal_command(
67+
["ps", "-p", frpid, "-o", "comm="]
68+
) # Obtain the app name using ps terminal command
69+
70+
# adding the app to the app list
71+
if not app in applist:
72+
applist.append(app)
73+
checklist = [item[1] for item in winlist]
74+
if not frname in checklist:
75+
winlist.append([app, frname, 1 * update_frequency])
76+
else:
77+
winlist[checklist.index(frname)][2] = (
78+
winlist[checklist.index(frname)][2] + 1 * update_frequency
79+
)
80+
81+
# TODO: Make pie-chart update is based on pie_chart_update_frequency variable
82+
# TODO: Organize pie charts and maybe don't delete previous pie charts and appreviate from them instead
83+
update_pie_chart(applist, [x[-1] for x in winlist], logdir)
Loading

0 commit comments

Comments
 (0)