-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcsv.py
executable file
·75 lines (66 loc) · 1.77 KB
/
csv.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
#!/usr/bin/env python
import sys
import json
from datetime import datetime
from datetime import timedelta
DATEFORMAT = '%Y%m%dT%H%M%SZ'
# Extract the configuration settings.
header = 1
configuration = dict()
body = ''
for line in sys.stdin:
if header:
if line == '\n':
header = 0
else:
fields = line.strip().split(': ', 2)
if len(fields) == 2:
configuration[fields[0]] = fields[1]
else:
configuration[fields[0]] = ''
else:
body += line
# Sum the second tracked by tag.
totals = dict()
j = json.loads(body)
for object in j:
start = datetime.strptime(object['start'], DATEFORMAT)
if 'end' in object:
end = datetime.strptime(object['end'], DATEFORMAT)
else:
end = datetime.utcnow()
tracked = end - start
# print(object['tags'])
for tag in object['tags']:
# print(tag)
if tag in totals:
totals[tag] += tracked
else:
totals[tag] = tracked
# Extract the JSON.
# doc = ''
# for line in sys.stdin:
# doc += line
total_active_time = 0
j = json.loads(body)
for object in j:
fix = timedelta(hours=3)
start = object['start']
if 'end' in object:
end = object['end']
end_time = datetime.strptime(end, DATEFORMAT)
end_time += fix
else:
end = datetime.now().strftime(DATEFORMAT)
end_time = datetime.strptime(end, DATEFORMAT)
start_time = datetime.strptime(start, DATEFORMAT)
start_time += fix
line = '"%s",' % start_time
line += '"%s"' % end_time
spent = end_time - start_time
line += ',"%s"' % spent
if 'tags' in object:
for tag in object['tags']:
li = tag
line += ',"%s"' % str(li)
print(line)