-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclockify.py
More file actions
339 lines (295 loc) · 9.46 KB
/
clockify.py
File metadata and controls
339 lines (295 loc) · 9.46 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env python
import csv
import os
import re
import time
import six
from PyInquirer import (
Token,
ValidationError,
Validator,
print_json,
prompt,
style_from_dict
)
from helium import *
from selenium.common.exceptions import SessionNotCreatedException
from pyfiglet import figlet_format
try:
import colorama
colorama.init()
except ImportError:
colorama = None
try:
from termcolor import colored
except ImportError:
colored = None
style = style_from_dict({
Token.QuestionMark: '#fac731 bold',
Token.Answer: '#4688f1 bold',
Token.Instruction: '', # default
Token.Separator: '#cc5454',
Token.Selected: '#0abf5b', # default
Token.Pointer: '#673ab7 bold',
Token.Question: '',
})
def log(string, color, font="slant", figlet=False):
if colored:
if not figlet:
six.print_(colored(string, color))
else:
six.print_(colored(figlet_format(
string, font=font), color))
else:
six.print_(string)
class EmailValidator(Validator):
pattern = r"\"?([-a-zA-Z0-9.`?{}]+@\w+\.\w+)\"?"
def validate(self, email):
if len(email.text):
if re.match(self.pattern, email.text):
return True
else:
raise ValidationError(
message="Invalid email",
cursor_position=len(email.text)
)
else:
raise ValidationError(
message="You can't leave this blank",
cursor_position=len(email.text)
)
class EmptyValidator(Validator):
def validate(self, value):
if len(value.text):
return True
else:
raise ValidationError(
message="You can't leave this blank",
cursor_position=len(value.text)
)
class FilePathValidator(Validator):
def validate(self, value):
if len(value.text):
if os.path.isfile(value.text):
return True
else:
raise ValidationError(
message="File not found",
cursor_position=len(value.text)
)
else:
raise ValidationError(
message="You can't leave this blank",
cursor_position=len(value.text)
)
def login(basicinfo):
log("Opening a browser...", "yellow")
login_url = "https://clockify.me/login"
browser = basicinfo.get('browser')
login_method = basicinfo.get('login_method')
if login_method == 'google':
email = basicinfo.get('google_email')
password = basicinfo.get('google_password')
else:
email = basicinfo.get('regular_email')
password = basicinfo.get('regular_password')
# Open Clockify Login Page
if (browser == 'firefox'):
try:
start_firefox(login_url)
except SessionNotCreatedException:
log("Firefox browser not found. Try again.", "red")
exit()
else:
try:
start_chrome(login_url)
except SessionNotCreatedException:
log("Chrome browser not found. Try again.", "red")
exit()
time.sleep(5) # Wait
log("Opened, Now logging you in...", "yellow")
if (login_method == 'google'): # Google Login
highlight("Continue with Google")
click("Continue with Google")
write(email, into="Email or phone")
click('Next')
time.sleep(5) # Wait
write(password, into="Enter your password")
click('Next')
time.sleep(5) # Wait
else: # Regular Login
write(email, into="Enter email")
write(password, into="Enter password")
press(ENTER)
time.sleep(5) # Wait
log("Logged in", "yellow")
return True
def bulkInsert(basicinfo):
log("Loading csv file...", "yellow")
csv_file = basicinfo.get('data')
# Process CSV File
log("Reading....", "yellow")
reader = csv.DictReader(csv_file)
log("Start Processing....", "green")
totalrows = 0
for row in reader:
# Enter Task Description
log(row['Description'], "magenta")
write(row['Description'], into="What have you worked on?")
time.sleep(1) # Wait
# Select Project
log(row['Project'], "magenta")
press(TAB)
write(row['Project'])
time.sleep(1) # Wait
press(DOWN)
press(ENTER)
time.sleep(1) # Wait
# Select Tag
log(row['Tag'], "magenta")
press(TAB)
tags = row['Tag'].split(',')
for tag in tags:
click(tag)
time.sleep(1) # Wait
# Fill Start Time
log(row['Start_time'], "magenta")
press(TAB)
press(TAB)
write(row['Start_time'])
time.sleep(1) # Wait
# Fill End Time
log(row['End_time'], "magenta")
press(TAB)
write(row['End_time'])
time.sleep(1) # Wait
# Fill Date
log(row['Date'], "magenta")
press(TAB)
press(TAB)
write(row['Date'])
time.sleep(1) # Wait
# Add'em
log('Inserting....', "magenta")
click("ADD")
time.sleep(5) # Wait
log('Inserted', "green")
totalrows += 1
return totalrows
def getLoginMethod(answer, login_method):
return answer.get("login_method").lower() == login_method.lower()
def askBasicInformation():
questions = [
{
'type': 'list',
'name': 'browser',
'message': 'Your Preferred Browser:',
'choices': ['Chrome', 'Firefox'],
'filter': lambda val: val.lower()
},
{
'type': 'list',
'name': 'login_method',
'message': 'Choose Login Method:',
'choices': ['Regular', 'Google'],
'filter': lambda val: val.lower()
},
{
'type': 'input',
'name': 'google_email',
'message': 'Enter Google Email:',
'when': lambda answers: getLoginMethod(answers, "google"),
'validate': EmailValidator
},
{
'type': 'password',
'name': 'google_password',
'message': 'Enter Google Password:',
'when': lambda answers: getLoginMethod(answers, "google"),
'validate': EmptyValidator
},
{
'type': 'input',
'name': 'regular_email',
'message': 'Enter Regular Email:',
'when': lambda answers: getLoginMethod(answers, "regular"),
'validate': EmailValidator
},
{
'type': 'password',
'name': 'regular_password',
'message': 'Enter Regular Password:',
'when': lambda answers: getLoginMethod(answers, "regular"),
'validate': EmptyValidator
},
{
'type': 'input',
'name': 'data',
'message': 'Enter CSV File Path: (Caution : Please double check your data)',
'validate': FilePathValidator,
'filter': lambda val: open(val, newline=''),
},
{
'type': 'confirm',
'name': 'ready',
'message': 'Lets Begin with Loggin in.. Keep checking this terminal..'
}
]
answers = prompt(questions, style=style)
return answers
def takeConfirmation():
questions = [
{
'type': 'confirm',
'name': 'is_loggedin',
'message': 'Are you able to login in to system ? If not, please try to login manually, and let me know when you\'re done.'
},
{
'type': 'confirm',
'name': 'is_maximize',
'message': 'Is your browser is Maximized ?',
'when': lambda answers: answers.get("is_loggedin", False)
},
{
'type': 'confirm',
'name': 'ready',
'message': 'Have you double checked your data ? Ready to insert data ?',
'when': lambda answers: answers.get("is_maximize", False)
}
]
answers = prompt(questions, style=style)
return answers
def cli():
"""
Simple CLI for automate bulk insertion of clockify enteries.
"""
log("CLOCKIFY", color="blue", figlet=True)
log("Welcome to Clockify Bulk Insert", "blue")
basicinfo = askBasicInformation()
if basicinfo.get("ready", False):
try:
loggedin = login(basicinfo)
except Exception as e:
raise Exception("An error occured: %s" % (e))
if loggedin:
c = takeConfirmation()
l = c.get("is_loggedin", False)
m = c.get("is_maximize", False)
r = c.get("ready", False)
if l and m and r:
log("Looks like you're ready to go!!", "green")
try:
response = bulkInsert(basicinfo)
if response > 0:
log("Insertion Successful", "blue")
log("Total {} Entries Inserted".format(response), "green")
else:
log("An error while trying to insert.", "red")
except Exception as e:
log("Something went wrong, Please try again.", "red")
raise Exception("An error occured: %s" % (e))
else:
log("Phew!! You need to re-run the process again.", "green")
log("Never mind!! Better luck next time.", "green")
if __name__ == '__main__':
cli()