-
Notifications
You must be signed in to change notification settings - Fork 0
/
time-tracker-bot.py
191 lines (149 loc) · 5.77 KB
/
time-tracker-bot.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# core modules
import os
import time
import json
import datetime
import sys
# extra modules
from absl import app, flags, logging
# own modules
from activity import Activity, ActivityList, TimeEntry
from window_handler import Window, WindowLinux
# app flags
FLAGS = flags.FLAGS
flags.DEFINE_string('activities_filepath', 'activities.json',
'Path of the file to read and save activities.')
class TimeTrackerBot:
"""
A bot to track user activities.
Parameters
----------
filepath: string
Path of the file where to save activities
window_handler: Window
The window handler for that tracker, only Linux available yet
"""
def __init__(self, filepath, window_handler):
self.window_handler = window_handler
self.filepath = filepath
self.activities = self.read_activities_from_json()
self.last_activity_name = window_handler.get_active()
self.last_activity_time = datetime.datetime.now()
self.last_time_since_saving = time.time()
def write_activities_to_json(self, activities):
"""
Write activities to a JSON file.
Parameters
----------
activities: array-like
The activities to write
"""
with open(self.filepath, 'w+') as f:
json.dump(activities, f, indent=4, sort_keys=True)
def read_activities_from_json(self):
"""
Read activities from a JSON file.
Returns
-------
array-like: The activities as an array of ``Activity``
"""
try:
with open(self.filepath, 'r') as f:
logging.debug("Reading activities from %s.", self.filepath)
data = json.load(f)
except (IOError, json.JSONDecodeError):
logging.debug("%s is either empty or does not exist.", self.filepath)
data = []
return ActivityList(data).activities
def serialize_activities(self):
"""
Serialize all the activities.
Returns
-------
dict: The activities serialized as JSON object
"""
return [activity.serialize() for activity in self.activities]
def _is_new_activity(self, activity_name):
"""
Check if the current window is different from the last saved.
Parameters
----------
activity_name: string
The name of the activity to check
Returns
-------
bool: True if the current window is new, False otherwise
"""
return activity_name != self.last_activity_name
def _add_activity(self, activity_name, time_entry):
"""
Add the activity with its time entry to the activity list.
If activity was already present in the list, only update the duration
and add the time entry. If activity was not present, add it.
Parameters
----------
activity_name: string
The name of the activity to add
time_entry: TimeEntry
The time entry of the activity to add
Returns
-------
bool: True
"""
for activity in self.activities:
if activity_name == activity.name:
activity.duration += time_entry.delta
activity.entries.append(time_entry)
return True
activity = Activity(name=activity_name,
duration=time_entry.delta,
entries=[time_entry])
self.activities.append(activity)
return True
def run(self, save_frequency=30):
"""
Main infinite loop.
Get the current active window and add it when it changes. As long as a
window stays active, just sleeps for 2 seconds.
Save activities every ``save_frequency`` seconds.
TODO : use signals instead of active loops.
"""
try:
while True:
current_activity_name = self.window_handler.get_active()
if not self._is_new_activity(current_activity_name):
time.sleep(2)
continue
# get the current time of the activity and add activity to the list
current_activity_time = datetime.datetime.now()
current_time_entry = TimeEntry(self.last_activity_time, current_activity_time)
self._add_activity(current_activity_name, current_time_entry)
# restart time counter and active window name
self.last_activity_time = datetime.datetime.now()
self.last_activity_name = current_activity_name
# save activities every 30 seconds
t = time.time()
if t - self.last_time_since_saving > save_frequency:
self.last_time_since_saving = t
self.write_activities_to_json(self.serialize_activities())
logging.info("Saved %s activities.", len(self.activities))
# save activities on exception
except (KeyboardInterrupt, Exception):
self.write_activities_to_json(self.serialize_activities())
logging.info("Saved %s activities.", len(self.activities))
def main(argv):
del argv
logging.get_absl_handler().use_absl_log_file(
program_name='time-tracker-bot',
log_dir='./logs/'
)
logging.set_verbosity(logging.INFO)
logging.info("Starting time-tracker-bot.")
logging.info("sys.platform=%s", sys.platform)
logging.info("python.version=%s.", sys.version)
window_handler = WindowLinux()
bot = TimeTrackerBot(filepath=FLAGS.activities_filepath, window_handler=window_handler)
bot.run()
logging.info("Closing time-tracker-bot.")
if __name__ == '__main__':
app.run(main) # , argv=('--log-dir=./logs', ))