forked from KalleHallden/AutoTimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autotimer.py
110 lines (93 loc) · 3.75 KB
/
autotimer.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
from __future__ import print_function
import time
from os import system
from activity import *
import json
import datetime
import sys
if sys.platform in ['Windows', 'win32', 'cygwin']:
import win32gui
import uiautomation as auto
elif sys.platform in ['Mac', 'darwin', 'os2', 'os2emx']:
from AppKit import NSWorkspace
from Foundation import *
elif sys.platform in ['linux', 'linux2']:
import linux as l
active_window_name = ""
activity_name = ""
start_time = datetime.datetime.now()
activeList = AcitivyList([])
first_time = True
def url_to_name(url):
string_list = url.split('/')
return string_list[2]
def get_active_window():
_active_window_name = None
if sys.platform in ['Windows', 'win32', 'cygwin']:
window = win32gui.GetForegroundWindow()
_active_window_name = win32gui.GetWindowText(window)
elif sys.platform in ['Mac', 'darwin', 'os2', 'os2emx']:
_active_window_name = (NSWorkspace.sharedWorkspace()
.activeApplication()['NSApplicationName'])
else:
print("sys.platform={platform} is not supported."
.format(platform=sys.platform))
print(sys.version)
return _active_window_name
def get_chrome_url():
if sys.platform in ['Windows', 'win32', 'cygwin']:
window = win32gui.GetForegroundWindow()
chromeControl = auto.ControlFromHandle(window)
edit = chromeControl.EditControl()
return 'https://' + edit.GetValuePattern().Value
elif sys.platform in ['Mac', 'darwin', 'os2', 'os2emx']:
textOfMyScript = """tell app "google chrome" to get the url of the active tab of window 1"""
s = NSAppleScript.initWithSource_(
NSAppleScript.alloc(), textOfMyScript)
results, err = s.executeAndReturnError_(None)
return results.stringValue()
else:
print("sys.platform={platform} is not supported."
.format(platform=sys.platform))
print(sys.version)
return _active_window_name
try:
activeList.initialize_me()
except Exception:
print('No json')
try:
while True:
previous_site = ""
if sys.platform not in ['linux', 'linux2']:
new_window_name = get_active_window()
if 'Google Chrome' in new_window_name:
new_window_name = url_to_name(get_chrome_url())
if sys.platform in ['linux', 'linux2']:
new_window_name = l.get_active_window_x()
if 'Google Chrome' in new_window_name:
new_window_name = l.get_chrome_url_x()
if active_window_name != new_window_name:
print(active_window_name)
activity_name = active_window_name
if not first_time:
end_time = datetime.datetime.now()
time_entry = TimeEntry(start_time, end_time, 0, 0, 0, 0)
time_entry._get_specific_times()
exists = False
for activity in activeList.activities:
if activity.name == activity_name:
exists = True
activity.time_entries.append(time_entry)
if not exists:
activity = Activity(activity_name, [time_entry])
activeList.activities.append(activity)
with open('activities.json', 'w') as json_file:
json.dump(activeList.serialize(), json_file,
indent=4, sort_keys=True)
start_time = datetime.datetime.now()
first_time = False
active_window_name = new_window_name
time.sleep(1)
except KeyboardInterrupt:
with open('activities.json', 'w') as json_file:
json.dump(activeList.serialize(), json_file, indent=4, sort_keys=True)