-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSR-Automation .py
596 lines (554 loc) · 30.2 KB
/
SR-Automation .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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#! python3
import openpyxl, pprint, os, glob, time, shutil, re, zipfile, PyPDF2, sys
import xml.etree.ElementTree
import pandas as pd
from datetime import datetime, date
from docx import Document
from pathlib import Path
from smtplib import SMTP
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from secrets import jobber_us, jobber_pw, onedrive_us, onedrive_pw
start = time.perf_counter()
os.chdir('C:\\Users\\info\\OneDrive\\1. M2M Administration\\AGED CARE\\Jobber Bot')
#DOB Calculation (to be used in data extraction
def calculate_age(born):
today = date.today()
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
class DataExtraction():
def checkFolder(self):
global document, latest_file, read_pdf, list_of_files, last_file, ext
#Latest File in Directory - MIGHT NEED TO BE CHNAGED TO THE OLDEST FILE IN DIRECTORY IF RUNNING ON SCHED
#NEED TO ADD IN FUNCTIONALITY FOR RESTRICTED .DOCX FILES &
#.PDF versions of the document
list_of_files = glob.glob(os.getcwd() + '\\*')
#Ignore debug.log file
list_of_files.remove('C:\\Users\\info\\OneDrive\\1. M2M Administration\\AGED CARE\\Jobber Bot\\debug.log')
list_of_files.remove('C:\\Users\\info\\OneDrive\\1. M2M Administration\\AGED CARE\\Jobber Bot\\Jobber-Client.xlsx')
last_file = max(list_of_files, key=os.path.getctime)
#Just get the file name
latest_file = last_file.split('\\')[-1]
ext = latest_file.split('.')[-1]
# print(latest_file)
def ExtractDocxData(self):
# Designed to extact data ONLY FROM May 2020 Service Request form
# NEED TO SIMPLIFY THIS -- WORK SMARTER NOT HARDER -- NESTED LOOP?
# These need to be global to pass to other functions
try:
global data, info, provider, manager, loc, m_phone, email, name, name_fn, name_ln,\
dob, street1, city, state, postcode, number, med_detail, overview,\
nok, nok_contact, nok_rel, start, reason, date_of_birth, age, prim_contact,\
freq, postcode, docxExtractError, s, i, address, details, headings, all_details, headers, radio, notes, l1, remove, z, c
## Politely borrowed from:
## https://stackoverflow.com/questions/22756344/how-do-i-extract-data-from-a-doc-docx-file-using-python
WORD_NAMESPACE = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
PARA = WORD_NAMESPACE + 'p'
TEXT = WORD_NAMESPACE + 't'
TABLE = WORD_NAMESPACE + 'tbl'
ROW = WORD_NAMESPACE + 'tr'
CELL = WORD_NAMESPACE + 'tc'
with zipfile.ZipFile(latest_file) as docx:
tree = xml.etree.ElementTree.XML(docx.read('word/document.xml'))
info = []
for table in tree.iter(TABLE):
for row in table.iter(ROW):
for cell in row.iter(CELL):
info.append(''.join(node.text for node in cell.iter(TEXT)))
#Remove duplicates
#Replace empty fields with 'Blank'
remove = ['Click or tap here to enter text.', 'Click or tap to enter a date', '']
for detail in info:
if detail in remove:
z = [z for z, n in enumerate(info) if n == detail]
for y in z:
info[y] = 'Blank'
#Show Data Extracted
for detail in enumerate(info):
print(detail)
#Find selected radio buttons and add notes if neccessary
freq = []
notes = []
radio = []
for item in info:
if u"\u2612" in item:
l1 = []
l1.append(item)
for j in l1:
i = j.index(u"\u2612")
radio.append(j[i+2])
if 'female only' in item.lower():
notes.append('Female Only')
# Change radio[0] into full word
if radio[0] == 'C':
radio[0] = 'Client'
notes.append(f'Please Contact {radio[0]}')
prim_contact = radio[0]
elif radio[0] == 'N':
radio[0] = 'NOK'
notes.append(f'Please Contact {radio[0]}')
prim_contact = radio[0]
else:
prim_contact = ''
if len(radio) >= 3:
freq = radio[1] + '(' + radio[2] + ')'
elif len(radio) >= 2:
freq = radio[-1]
else:
freq = 'Blank'
freq = str(freq).strip("[|]|''|''")
#Remove item from headings if not in the document
# Get Address as BEST AS POSSIBLE
statereg = re.compile(r'NSW|VIC|TAS|QLD|NT|ACT|WA')
s = statereg.search(info[24])
if s == None:
if len(re.findall('[0-9]{4}$', info[24])) == 1:
postcode = ''.join(re.findall('[0-9]{4}$', info[24]))
address = info[24].split(' ')
city = address[-2].strip(',|\\')
street1 = ' '.join(address[:-2]).strip(',|\\')
state = 'Blank'
else:
street1 = info[24]
state = 'Blank'
city = 'Blank'
postcode = 'Blank'
# Else use regex to get perfect data from contact
else:
state = s.group()
#Format the data correctly
i = info[24].split(state)
x = i[0].split(' ')
s = x[:-2]
s2 = ' '.join(s)
street1 = s2.strip(',')
c = x[-2]
city = c.strip(',')
postcode = i[1]
# Serv provider info
provider = info[4]
manager = info[7]
loc = info[10]
m_phone = info[13]
email = info[16]
# Get client details
name = info[19].split(' ')
name_fn = str(name[:-1]).strip("[|''|''|]")
name_ln = name[-1]
number = info[26]
nok = info[33]
nok_rel = info[37]
nok_contact = info[35]
dob = info[21]
med_detail = info[28]
overview = info[30]
start = info[52].strip('**mark today for ASAP')
reason = info[54]
freq = str(freq).strip("[|]|''|''")
# Use function to get Age
date_of_birth = datetime.strptime(dob, "%d/%m/%Y")
#Add start date to notes
if start != 'Blank':
notes.append(f'Please begin services on/after {start}')
notes = ' - '.join(notes)
print(notes)
def calculate_age(born):
today = date.today()
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
age = calculate_age(date_of_birth)
except Exception as e:
docxExtractError = e
print(f'Error Extracting Data From {latest_file}')
def ExtractPdfData(self):
global page, page2, provider, manager, loc, m_phone, email, name, name_fn, name_ln,\
dob, address, street1, city, state, number, med_detail, overview,\
nok, nok_contact, nok_rel, start, reason, date_of_birth, age, notes, \
prim_contact, freq, postcode, pdfExtractError
#remove unecessary elements and empty spaces
read_pdf = PyPDF2.PdfFileReader(last_file)
page = read_pdf.getPage(0).extractText()
page2 = page.split('\n')
while(' ' in page2):
page2.remove(' ')
##page2 = list(filter(lambda a: a != '\n', page))
try:
#Assign page elements to their right position
#Company/Case Manager Details
provider = page2[12]
manager = page2[14]
loc = page2[16]
m_phone = page2[18]
email = page2[20]
# Get Client Details
name = page2[30].split()
name_fn = name[0]
name_ln = name[1]
dob = page2[34]
#PDF requires different method to extract address
## address = page2[36].split(' ')
## street1 = street1 = ' '.join([str(elem) for elem in address[:3]])
## city = address[3]
## state = re.sub('[\W_]+', '', address[-2])
## postcode = address[-1]
#Using Regex to find address and SPLIT it up based on state
#Hopefully somewhat correct
statereg = re.compile(r'NSW|VIC|TAS|QLD|NT|ACT|WA')
s = statereg.search(page2[36])
state = s.group()
#Format the data correctly
i = page2[36].split(state)
x = i[0].split(' ')
s = x[:-2]
s2 = ' '.join(s)
street1 = s2.strip(',')
c = x[-2]
city = c.strip(',')
postcode = i[1]
number = page2[41]
med_detail = page2[49]
overview = page2[53]
nok = page2[59]
nok_contact = page2[65]
nok_rel = page2[62]
start = page2[70]
reason = page2[101]
# Use function to get Age
date_of_birth = datetime.strptime(dob, "%d/%m/%Y")
def calculate_age(born):
today = date.today()
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
age = calculate_age(date_of_birth)
# Work out how to get radio buttons, find state & PC
prim_contact = ''
freq = ''
notes = ''
except Exception as e:
pdfExtractError = e
print(f'There was an error extracting data from {latest_file}\n\n\{pdfExtractError}')
def ExtractBilling(self):
## SEARCH BILLING INFORMATION SPREADSHEET
## WORK OUT HOW TO CHECK IF THE CLIENT IS NOT IN SPREADSHEET AT ALL
global billing, res1
df = pd.read_excel('C:\\Users\\info\\OneDrive\\1. M2M Administration\\PROPOSALS\\Jarrods Folder\\Automation\\Accounts_Contacts.xlsx')
res1 = df[df['Company'].str.contains(provider, na=False)]
billing = []
if len(res1.index) > 1:
billing = (df[df['Company'].str.contains(loc, na=False)])
else:
billing = res1
class FileManip():
def FileMove(self):
global new_folder
new_folder = 'C:\\Users\\info\\OneDrive\\1. M2M Administration\\AGED CARE\\AGED CARE CLIENT PROVIDERS\\'+ \
state + '\\' + provider + '\\' + name_fn + ' ' + name_ln
Path(new_folder).mkdir(parents=True, exist_ok=True)
#Rename file and move to directory under the right name
os.rename(latest_file, new_folder + '\\' + name_fn + ' ' + name_ln + '- Service Request ' +\
date.today().strftime("%d-%m-%Y") + '.' + ext)
def WriteToExcel(self):
global details, sheet, wb
##Write name to cells --
## CHECK IF ENTRY IS DUPLICATED -- DO NOT LET DUPLICATED DATA PASS ON TO .XLSX FILE AND CRM
wb = openpyxl.load_workbook('C:\\Users\\info\\OneDrive\\1. M2M Administration\\AGED CARE\\Jobber Bot\\Jobber-Client.xlsx')
sheet = wb['Imports']
## For Loop to insert new data below existing data
details = [[datetime.now().strftime("%d/%m/%Y %H:%M:%S"), name_fn, name_ln, provider, number,\
street1, city, state, postcode, dob, age, prim_contact, freq, med_detail, overview, nok, nok_contact,\
nok_rel, start, reason, notes, billing['Billing Street 1'].values[0], billing['Billing Street 2'].values[0],\
billing['Billing City'].values[0], billing['Billing State'].values[0], str(billing['Postcode'].values[0])]]
for detail in details:
sheet.append(detail)
wb.save('C:\\Users\\info\\OneDrive\\1. M2M Administration\\AGED CARE\\Jobber Bot\\Jobber-Client.xlsx')
print('Data written to Jobber-client.xlsx')
class JobberBot():
def __init__(self):
self.driver = webdriver.Chrome()
def login(self):
self.driver.get('https://secure.getjobber.com/login')
time.sleep(1) # Let the user actually see something!
#LOGGING INTO JOBBER
email_box = self.driver.find_element_by_name('user_session[login]')
email_box.send_keys(jobber_us)
password_box = self.driver.find_element_by_name('user_session[password]')
password_box.send_keys(jobber_pw)
self.driver.find_element_by_name('commit').click()
#Create 'New Client' button
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[1]/div/div[2]/div[2]/div/div/div/div[1]/a').click()
time.sleep(1.5)
def FieldPopulation(self):
# Dropdown the 'Additional Client details' menu
self.driver.find_element_by_xpath('//*[@id="new_client"]/div/div/div/div[1]/div[1]/sg-accordion/sg-accordion-section[2]/sg-accordion-section-header/div/div[2]/div/div').click()
time.sleep(1.5)
# FIELDS TO FILL
#Name
time.sleep(1.5)
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[1]/div[2]/label/div[1]/div/div/div[1]/div[2]/placeholder-field/input').send_keys(name_fn)
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[1]/div[2]/label/div[1]/div/div/div[1]/div[3]/placeholder-field/input').send_keys(name_ln)
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[1]/div[2]/label/div[1]/div/div/div[2]/div/placeholder-field/input').send_keys(provider)
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[1]/div[2]/div[2]/ul/li/div/div[2]/div/div/div[2]/div/div[1]/placeholder-field/input').send_keys(number)
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[1]/div[2]/div[3]/ul/li/div/div[2]/div/div/div[2]/div/div[1]/placeholder-field/input').send_keys(billing['Email'].values[0])
#Uncheck 'Billing address is the same...' checkbox
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[2]/div[4]/div/div[1]/label/sg-icon').click()
time.sleep(1.5)
#Address
# Dropdown opens - SEND 'ESCAPE' KEY TO EXIT'
time.sleep(2)
self.driver.find_element_by_xpath('//*[@id="new_client"]/div/div/div/div[1]/div[2]/div[3]/div[2]/div/div[1]/div/div[1]/div/placeholder-field/input').send_keys(street1)
self.driver.find_element_by_xpath('//*[@id="new_client"]/div/div/div/div[1]/div[2]/div[3]/div[2]/div/div[1]/div/div[1]/div/placeholder-field/input').send_keys(webdriver.common.keys.Keys.ESCAPE)
time.sleep(1.5)
#City
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[2]/div[3]/div[2]/div/div[1]/div/div[3]/div[1]/placeholder-field/input').send_keys(city)
#State
self.driver.find_element_by_xpath('//*[@id="new_client"]/div/div/div/div[1]/div[2]/div[3]/div[2]/div/div[1]/div/div[3]/div[2]/placeholder-field/input').send_keys(state)
#Postcode
self.driver.find_element_by_xpath('//*[@id="new_client"]/div/div/div/div[1]/div[2]/div[3]/div[2]/div/div[1]/div/div[4]/div[1]/placeholder-field/input').send_keys(postcode)
#Contact details
#driver.find_element_by_xpath('//*[@id="client_phones_attributes_1587200583889_number"]').sendkeys('9844 3360')
#Case Manager
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[1]/sg-accordion/sg-accordion-section[2]/sg-accordion-section-body/div[1]/placeholder-field[1]/input').send_keys(manager)
#Case Manager Contact
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[1]/sg-accordion/sg-accordion-section[2]/sg-accordion-section-body/div[1]/placeholder-field[3]/input').send_keys(m_phone)
#DOB
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[1]/sg-accordion/sg-accordion-section[2]/sg-accordion-section-body/div[1]/placeholder-field[7]/input').send_keys(dob)
#Age
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[1]/sg-accordion/sg-accordion-section[2]/sg-accordion-section-body/div[1]/placeholder-field[8]/input').send_keys(age)
#Home/Mobile
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[1]/sg-accordion/sg-accordion-section[2]/sg-accordion-section-body/div[1]/placeholder-field[9]/input').send_keys(number)
#Next of Kin
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[1]/sg-accordion/sg-accordion-section[2]/sg-accordion-section-body/div[1]/placeholder-field[10]/input').send_keys(nok)
#Next of Kin Contact
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[1]/sg-accordion/sg-accordion-section[2]/sg-accordion-section-body/div[1]/placeholder-field[11]/input').send_keys(nok_contact)
#Availability
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[1]/sg-accordion/sg-accordion-section[2]/sg-accordion-section-body/div[1]/placeholder-field[12]/input').send_keys(start)
#Medical Notes
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[1]/sg-accordion/sg-accordion-section[2]/sg-accordion-section-body/div[1]/placeholder-field[13]/input').send_keys(med_detail)
time.sleep(0.5)
#Additional Notes
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[1]/sg-accordion/sg-accordion-section[2]/sg-accordion-section-body/div[1]/placeholder-field[14]/input').send_keys(overview)
#Massage details
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[1]/sg-accordion/sg-accordion-section[2]/sg-accordion-section-body/div[1]/placeholder-field[15]/input').send_keys(reason)
time.sleep(2)
#Enter Billing Details
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[2]/div[4]/div/div[2]/div/div[1]/div/placeholder-field/input').send_keys(billing['Billing Street 1'].values[0])
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[2]/div[4]/div/div[2]/div/div[2]/div/placeholder-field/input').send_keys(billing['Billing Street 2'].values[0])
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[2]/div[4]/div/div[2]/div/div[3]/div[1]/placeholder-field/input').send_keys(billing['Billing City'].values[0])
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[2]/div[4]/div/div[2]/div/div[3]/div[2]/placeholder-field/input').send_keys(billing['Billing State'].values[0])
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[2]/div[4]/div/div[2]/div/div[4]/div[1]/placeholder-field/input').send_keys(str(billing['Postcode'].values[0]))
#Select Australia from dropdown
select_fr = Select(self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[1]/div[2]/div[4]/div/div[2]/div/div[4]/div[2]/div/select'))
select_fr.select_by_index(0)
# CREATE CLIENT
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form/div/div/div/div[2]/div[2]/button').click()
print('Successfully created client ' + str(name_fn) + str(name_ln) + 'in Jobber')
time.sleep(2)
### Need to get past possible 'duplicate' pop up:
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[3]/div/div[2]/div/div[2]/form/input[96]').click()
time.sleep(2)
## Add AgedCare Tag
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/div[2]/div[2]/div[1]/div[2]/a').click()
time.sleep(1.5)
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/div[2]/div[2]/div[2]/form/div/div[1]/placeholder-field/input').send_keys('AgedCare')
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/div[2]/div[2]/div[2]/form/div/div[2]/a').click()
print('Added AgedCare Tag')
time.sleep(1)
## CREATE JOB
#Open Dropdown
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/div[1]/div[4]/div[1]/div/div/button').click()
time.sleep(1)
#Select 'job'
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/div[1]/div[4]/div[1]/div/div/div[1]/nav/a[3]').click()
#Add freq to job title
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form[1]/div/div/div/div[1]/div/div[2]/div[1]/div[2]/div[1]/div/placeholder-field/input').send_keys(freq)
#Add notes to job
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form[1]/div/div[1]/div/div[1]/div/div[2]/div[1]/div[2]/div[2]/div/placeholder-field/textarea').send_keys(notes)
#Check 'Schedule Later'
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form[1]/div/div[1]/div/div[2]/div[2]/div[1]/div/div[1]/div/div[2]/div/div[4]/div/label/sg-icon').click()
#Save Job
self.driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[2]/div/form[1]/div/div[1]/div/div[2]/div[5]/div[2]/div/div/a').click()
print(f'Successfully created an unassigned job for {name_fn} {name_ln} in Jobber.')
time.sleep(1)
self.driver.close()
self.driver.quit()
class ConfEmail():
def SuccessEmail(self):
## SEND AN EMAIL TO NOTIFY STAFF
debuglevel = 1
smtp = SMTP()
smtp.set_debuglevel(debuglevel)
smtp.connect('awcp027.server-cpanel.com', 587)
smtp.login('[email protected]', 'magnoliA24@')
from_addr = "Automated Bot <[email protected]>"
to_addr = "[email protected]"
subj = F'SYNC COMPLETED FOR {name_fn} {name_ln}'
date = datetime.now().strftime( "%d/%m/%Y %H:%M" )
message_text = f'Hello,\n\n\
A service request for {name_fn} {name_ln} has been recieved, processed and moved to the following file location:\n\n\
{new_folder}\n\n\n\
The following billing details were entered:\n\
Company: {billing["Company"].values[0]}\n\
Address: {billing["Billing Street 1"].values[0]}, {billing["Billing Street 2"].values[0]}, {billing["Billing City"].values[0]}, {billing["Billing State"].values[0]} {str(billing["Postcode"].values[0])}\n\n\
This task was completed at {datetime.now().strftime("%d/%m/%Y %H:%M:%S")}\n\n\n\
Kind regards,\n\n\
Your Friendly Automated Bot.'
msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" \
% ( from_addr, to_addr, subj, date, message_text )
smtp.sendmail(from_addr, to_addr, msg)
smtp.quit()
def ErrorEmail(self):
## SEND AN EMAIL TO NOTIFY STAFF
debuglevel = 1
smtp = SMTP()
smtp.set_debuglevel(debuglevel)
smtp.connect('awcp027.server-cpanel.com', 587)
smtp.login('[email protected]', 'magnoliA24@')
from_addr = "Automated Bot <[email protected]>"
to_addr = "[email protected]"
subj = F'ERROR COMPLETING SYNC FOR {name_fn} {name_ln}'
date = datetime.now().strftime( "%d/%m/%Y %H:%M" )
message_text = f'Hello,\n\n\
An error has occured while processing the service request for {name_fn} {name_ln}. \n\n\
The Service Request has been moved to the following location:\n\n\
{new_folder}\n\n\n\
Error message:\n\
{error}\n\n\n\
This task was completed at {datetime.now().strftime("%d/%m/%Y %H:%M:%S")}\n\n\n\
Kind regards,\n\n\
Your Friendly Automated Bot.'
msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" \
% ( from_addr, to_addr, subj, date, message_text )
smtp.sendmail(from_addr, to_addr, msg)
smtp.quit()
def WrongVersion(self):
## SEND AN EMAIL TO NOTIFY STAFF ThAT THE DOCUMENT IS NOT THE CORRECT FORMAT
debuglevel = 1
smtp = SMTP()
smtp.set_debuglevel(debuglevel)
smtp.connect('awcp027.server-cpanel.com', 587)
smtp.login('[email protected]', 'magnoliA24@')
from_addr = "Automated Bot <[email protected]>"
to_addr = "[email protected]"
subj = F'ERROR PARSING {latest_file}'
date = datetime.now().strftime( "%d/%m/%Y %H:%M" )
message_text = f'Hello,\n\n\
An error has occured while parsing a the following .docx file: {latest_file}\n\n\n\
Please only place Service Requests with "Latest Form Issued May 2020" under the M2M logo in the Jobber Bot folder\n\n\n\n\n\
This task was completed at {datetime.now().strftime("%d/%m/%Y %H:%M:%S")}\n\n\n\
Kind regards,\n\n\
Your Friendly Automated Bot.'
msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" \
% ( from_addr, to_addr, subj, date, message_text )
smtp.sendmail(from_addr, to_addr, msg)
smtp.quit()
#Initialize prog to check folder
#Start building a structured conditional list of actions for program
#i.e. if x failes, program has done y so far - passed on into en email
DataExt = DataExtraction()
Conf = ConfEmail()
manip = FileManip()
#Check folder for new files
try:
DataExt.checkFolder()
except Exception:
os._exit(0)
missing_billing = ['Blank', '[email protected]', 'Blank', 'Blank', 'Blank', 'Blank', 'Blank', 'Blank']
if '.pdf' in latest_file:
print('Latest file is a .pdf')
# Try to run program and log error if error along the way
try:
DataExt.ExtractPdfData()
except Exception as error:
Conf.ErrorEmail()
os._exit(0)
try:
DataExt.ExtractBilling()
if len(billing) == 0:
df_length = len(billing)
billing.loc[df_length] = missing_billing
except Exception as error:
Conf.ErrorEmail()
os._exit(0)
try:
manip.FileMove()
except Exception as error:
Conf.ErrorEmail()
os._exit(0)
try:
manip.WriteToExcel()
except Exception as error:
Conf.ErrorEmail()
os._exit(0)
## Start Bot
try:
bot = JobberBot()
bot.login()
try:
bot.FieldPopulation()
except Exception as error:
Conf.ErrorEmail()
os._exit(0)
except Exception as error:
Conf.ErrorEmail()
os._exit(0)
# If Program makes it to here: success email will be sent!!
try:
Conf.SuccessEmail()
except Exception as error:
Conf.ErrorEmail()
os._exit(0)
elif '.docx' in latest_file:
document = Document(latest_file)
print('Latest file is a .docx')
# Try to extract data else send us an error message
# Only extract data if document is May 2020 version -- else send error message
if document.sections[0].header.paragraphs[1].text == 'Latest Form Issued May 2020':
print('Document is correct version issued in May 2020')
try:
DataExt.ExtractDocxData()
except Exception as error:
Conf.ErrorEmail()
os._exit(0)
try:
DataExt.ExtractBilling()
if len(billing) == 0:
df_length = len(billing)
billing.loc[df_length] = missing_billing
except Exception as error:
Conf.ErrorEmail()
os._exit(0)
try:
manip.FileMove()
except Exception as error:
Conf.ErrorEmail()
os._exit(0)
try:
manip.WriteToExcel()
except Exception as error:
Conf.ErrorEmail()
os._exit(0)
## Start Bot
try:
bot = JobberBot()
bot.login()
try:
bot.FieldPopulation()
except Exception as error:
Conf.ErrorEmail()
os._exit(0)
except Exception as error:
Conf.ErrorEmail()
os._exit(0)
## # If Program makes it to here: success email will be sent!!
try:
Conf.SuccessEmail()
except Exception as error:
Conf.ErrorEmail()
os._exit(0)
else:
Conf.WrongVersion()
os._exit(0)
else:
print('The latest document is not a .pdf or .docx')
os._exit(0)