-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackend.py
More file actions
52 lines (40 loc) · 1.32 KB
/
backend.py
File metadata and controls
52 lines (40 loc) · 1.32 KB
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
import os
import re
import sys
import time
import pygsheets
GDOC = "deepworklog"
USER = os.environ.get('SLACK_DW_USER')
def get_sheet():
gc = pygsheets.authorize(service_file='client_secret.json')
sh = gc.open(GDOC)
return sh.sheet1
def calc_seconds(hours, minutes):
return hours * 60 * 60 + minutes * 60
def convert_time(time):
time = str(time).split(' ', 1)[0]
if re.match(r'^\d+$', time):
return int(time) * 60 * 60
if ':' and time.count(':') == 1:
hours, minutes = time.split(':')
try:
hours = int(hours)
minutes = int(minutes)
except ValueError('cannot convert hours / minutes to ints'):
raise
return calc_seconds(hours, minutes)
raise ValueError('not a supported time format, supported = digit or hh:mm')
if __name__ == "__main__":
wks = get_sheet()
if len(sys.argv) < 2:
print('Run as {} hh:mm (activity)'.format(sys.argv[0]))
print('\nCurrently in gdoc:')
print([i[:4] for i in (list(wks)[1:])])
else:
now = int(time.time())
entered_time = sys.argv[1]
seconds = convert_time(entered_time)
activity = sys.argv[2] if len(sys.argv) > 2 else ''
row = [USER, now, seconds, activity]
print(row)
wks.insert_rows(row=2, number=1, values=row)