def fuelsort(thisdate, testpdf, nbill): s = addpath3('emaildocs/msfleet/') #for file in os.listdir(s): if 1 == 1: #fname=thisfile.replace('.pdf','') testbase = testpdf.replace('.pdf', '') filebase = testbase.split('msfleet/', 1)[1] filebase = filebase + '.pdf' #testpdf=testbase+'.pdf' testtxt = testbase + '.txt' avgprice = 'off' totqty = 'off' #tj=subprocess.check_output(['pdf2txt.py', '-o', testtxt, testpdf]) ctest = subprocess.check_output(['pdftotext', testpdf, testtxt]) np = 0 np2 = 0 gettotal = 0 getinfo = 0 amount = 0.00 fuelqty = [] ppg = [] isocards = [] cardtrigger = 'off' totaltrigger = 'off' fueltrigger = 'on' with open(testtxt) as openfile: for line in openfile: nl = line.strip() nla = 'a' + nl + 'a' if 'MS Local Card' in line and 'Current Purchases' in line: npurchases = line.split('Current', 1)[0] npurchases = npurchases.split('Card', 1)[1] npurchases = npurchases.strip() np = int(npurchases) #Define Triggers for Sequential Assumptioning: if 'Dyed Bio' in line: print('Potential Problem with extra Dyed Bio Purchase') if 'Card' in line: cardtrigger = 'on' if 'Total Amount' in line: totaltrigger = 'on' if totaltrigger == 'on': tottry = money_d2.findall(nla) if tottry: tottry = nl tottry = tottry.replace(',', '') total = float(tottry) if total > 1.0: amount = total #print('fuelamt=',amount) #Perform if Triggers applied: if cardtrigger == 'on': cardtry = card_d1.findall(nla) if len(isocards) < np and cardtry: cardtry = nl ctest = int(cardtry) if ctest > 0 and ctest < 9: isocards.append(cardtry) if fueltrigger == 'on': if nl == 'Fuel': nextline = next(openfile).strip() if nextline == 'Quantity': totqty = 'on' if nl == 'Avg.': nextline = next(openfile).strip() if nextline == 'Fuel': avgprice = 'on' #print(nl) if len(nl) > 0: fueltry = fuel_d4.findall(nla) if len(fuelqty) < np and fueltry: fueltry = nl ftest = float(fueltry) if ftest > 1.0: if totqty == 'off': fuelqty.append(fueltry) else: totqty = 'off' ppgtry = fuel_d3.findall(nla) if len(ppg) < np and ppgtry: ppgtry = nl ppgf = float(ppgtry) if ppgf > 1.0: if avgprice == 'off': ppg.append(ppgtry) else: avgprice = 'off' #print('ppg=',ppgtry) w = 14 header = [] cards = [] dates = [] with open(testtxt) as openfile: for line in openfile: if 'Transaction Count:' in line: npurchases = line.split('Count:', 1)[1] npurchases = npurchases.strip() np2 = int(npurchases) #print('Number Purchases:',np2) if 'Page' in line and np2 > 0: nl = next(openfile) for j in range(w): nl = next(openfile).strip() nln = next(openfile).strip() if len(nln) < 1: header.append(nl) else: header.append(nl + ' ' + nln) waste = next(openfile).strip() #print('w',waste) k = 0 #print(header) while len(cards) < np2 and k < 50: nl = next(openfile).strip() #print(nl) if len(nl) > 0: cards.append(nl) k = k + 1 #print(cards) k = 0 while len(dates) < np2 and k < 70: try: nl = next(openfile).strip() #print(nl) if len(nl) > 0: datetry = date_y4.findall(nl) if datetry: datetry = datetry[0] dates.append(datetry) except: print('Could not find all dates') k = k + 1 print('BillNo=', nbill) print('Number of Purchases is:', np, np2) print('Total Amount is:', amount) print('Cards=', cards) print('Isocards=', isocards) print('Dates=', dates) print('FuelQty=', fuelqty) print('PPG=', ppg) amtnew = [] for j in range(np2): q = float(fuelqty[j]) p = float(ppg[j]) p = p + .009 a = p * q amtnew.append(nodollar(a)) print(amtnew) try: patternlines = np2 * 4 patternvec = [1, 1, 1, 0] * np with open(testtxt) as openfile: for k, line in enumerate(openfile): cvec = getpattern(k, testtxt, patternlines) #print(cvec) if cvec == patternvec: print('They match at k=', k) addresses = getdata(k, testtxt, patternlines) print(addresses) break except: print('Out of file lines') addresses = '' #os.remove(testpdf) #os.remove(testtxt) print('Example Database Entries') print('FELBill Entered for Total Fuel This Week') print(' Date:', thisdate) print(' Amount:', amount) print(' ') billno = 'fleet' + str(nbill) company = 'MSFleet' pdat = People.query.filter(People.Company == company).first() aid = pdat.id sdate = datetime.datetime.strptime(thisdate, '%Y-%m-%d').date() incode = scac[0] + 'B' nextjo = newjo(incode, thisdate) input = Bills(Jo=nextjo, Pid=aid, Company=company, Memo='Weekly Fuel Bill', Description='Weekly Fuel Bill ' + thisdate, bAmount=amount, Status='Paid', Cache=0, Original=filebase, Ref='', bDate=sdate, pDate=sdate, pAmount=amount, pMulti=None, pAccount='FEL CitiBank', bAccount='Fuel', bType='Expense', bCat='Direct', bSubcat='Trucking', Link=billno, User='******', Co='F', Temp1='', Temp2='', Recurring=0, dDate=sdate, pAmount2='0.00', pDate2=None, Code1=None, Code2=None, CkCache=0, QBi=0, iflag=0, PmtList=None, PacctList=None, RefList=None, MemoList=None, PdateList=None, CheckList=None, MethList=None) db.session.add(input) db.session.commit()
if gbook > 0: if gbook == 1: usernamelist = [usernames['info']] password = passwords['info'] dayback = 14 if gbook == 2: usernamelist = [usernames['info']] password = passwords['info'] dayback = 450 datefrom = (datetime.date.today() - datetime.timedelta(dayback)).strftime("%d-%b-%Y") print('Running GBook from...', datefrom) att_dir = addpath3('emaildocs/globalbook') txt_file = addpath3('emaildocs/global_bookings.txt') for username in usernamelist: con = imaplib.IMAP4_SSL(imap_url) con.login(username, password) con.select('INBOX') msgs = get_emails( search_from_date('FROM', '@gblna.com', con, datefrom), con) flist = os.listdir(att_dir) loadconsadd = [] loadconsupdate = [] bookcheck = [] for j, msg in enumerate(msgs): raw = email.message_from_bytes(msg[0][1]) thesefiles = get_attachment_filename(raw, 'pdf', 'DB')
from viewfuncs import stripper, newjo, hasinput import os movem = 0 daybackfrom = 10 daybackto = 0 printif = 0 today = datetime.datetime.today() cutoffdate = today - timedelta(3) print(f'Running pdf builder for SCAC {scac} off Machine {machine}') print(addpath1('tmp/')) odata = Orders.query.filter(Orders.Date > cutoffdate).all() for odat in odata: if hasinput(odat.Container): idata = Interchange.query.filter( Interchange.Container == odat.Container).all() for idat in idata: print(idat.Container, idat.Original) if movem == 1: pythonline = websites[ 'ssh_data'] + f'vinterchange/{idat.Original}' placefile = addpath3(f'interchange/{idat.Original}') copyline1 = f'scp {pythonline} {placefile}' print(copyline1) os.system(copyline1) tunnel.stop()
def gatescraper(printif, dayback): username = usernames['gate'] password = passwords['gate'] print('username,password='******'interchange/') print('Entering Firefox') if printif == 1 else 1 yesterday = datetime.strftime(datetime.now() - timedelta(dayback), '%m/%d/%Y') todaystr = datetime.strftime(datetime.now() - timedelta(dayback), '%m/%d/%Y') today = datetime.today() cutoff = datetime.now() - timedelta(30) cutoff = cutoff.date() #todaystr = datetime.today().strftime('%m/%d/%Y') startdate = yesterday enddate = todaystr consets = [] print('startdate is:', yesterday) print('enddate is:', todaystr) # for j,startdate in enumerate(startdates): # enddate=enddates[j] #with Display(): if 1 == 1: url1 = websites['gate'] browser = webdriver.Firefox() browser.get(url1) print('Got url1') if printif == 1 else 1 time.sleep(4) print('Done Sleeping') if printif == 1 else 1 print('Getting xpath') if printif == 1 else 1 selectElem = browser.find_element_by_xpath('//*[@id="UserName"]') print('Got xpath for Username') if printif == 1 else 1 selectElem.clear() selectElem.send_keys(username) selectElem = browser.find_element_by_xpath('//*[@id="Password"]') print('Got xpath for Password') if printif == 1 else 1 selectElem.clear() selectElem.send_keys(password) time.sleep(1) selectElem.submit() time.sleep(4) newurl = browser.current_url print('newurl=', newurl, flush=True) if printif == 1 else 1 newurl = newurl + '#/Report/GateActivity' if 2 == 2: browser.get(newurl) time.sleep(2) print('newurl=', newurl, flush=True) try: selectElem = browser.find_element_by_xpath( '//*[@id="StartDate"]') selectElem.clear() selectElem.send_keys(startdate) except: print('Could not find StartDate Box') selectElem = browser.find_element_by_xpath('//*[@id="EndDate"]') selectElem.clear() selectElem.send_keys(enddate) time.sleep(1) selectElem.submit() time.sleep(7) try: contentstr = f'//*[@id="completed"]/div/div[1]' selectElem = browser.find_element_by_xpath(contentstr) con = selectElem.text res = [int(i) for i in con.split() if i.isdigit()] except: res = [0] print('No gate transactions reported') if len(res) > 0: numrec = int(res[0]) else: numrec = 0 print('Number of Elements in Table = ', numrec) if printif == 1 else 1 #containers = browser.find_elements_by_xpath('//a[contains(@href,"ticket")]') conrecords = [] for i in range(1, numrec + 1): cr = [] for j in range(1, 12): contentstr = f'//*[@id="completed"]/div/div[3]/table/tbody/tr[{i}]/td[{j}]' selectElem = browser.find_element_by_xpath(contentstr) con = selectElem.text if j == 1: movetyp = selectElem.text.strip() movetyp = movetyp.replace('Full', 'Load') movetyp = movetyp.replace('Export Dray-Off', 'Load Out') con = movetyp cr.append(con) if j == 3: nc = browser.find_element_by_xpath( f'//*[@id="completed"]/div/div[3]/table/tbody/tr[{i}]/td[{j}]/a' ) clink = nc.get_attribute('href') cr.append(clink) thiscon = selectElem.text.strip() print(cr) if printif == 1 else 1 dpt = cr[1].split() print('dpt=', dpt) if printif == 1 else 1 mydate = datetime.strptime(dpt[0], '%m/%d/%Y') mydate = mydate.date() mytime = f'{dpt[1]} {dpt[2]}' mytimedt = datetime.strptime(mytime, '%I:%M %p') mytime = mytimedt.strftime('%H:%M') print('mytime =', mytime) if printif == 1 else 1 print('cutoff =', cutoff) if printif == 1 else 1 idat = Interchange.query.filter( (Interchange.Container == thiscon) & (Interchange.Type == movetyp) & (Interchange.Date > cutoff)).first() if idat is None: contype = f'{cr[4]} {cr[5]} {cr[6]}' input = Interchange(Container=thiscon, TruckNumber='NAY', Driver='NAY', Chassis=cr[8], Date=mydate, Release=cr[11], GrossWt='NAY', Seals='NAY', ConType=contype, CargoWt='NAY', Time=mytime, Status='AAAAAA', Original='NAY', Path=cr[7], Type=movetyp, Jo='NAY', Company='NAY', Other=None) db.session.add(input) db.session.commit() print( f'***Adding {thiscon} {movetyp} on {mydate} at {mytime} to database' ) conrecords.append(cr) else: print( f'Record for {thiscon} {movetyp} on {mydate} at {mytime} already in database' ) #These are the records that will be put in database for rec in conrecords: thiscon = rec[2] movetyp = rec[0] clink = rec[3] browser.get(clink) time.sleep(2) conset = {} con_data = browser.page_source page_soup = soup(con_data, 'html.parser') keys = [ 'TRUCK NUMBER:', 'CHASSIS:', 'GROSS WT:', 'CARGO WT:', 'SEALS:' ] labels = [ 'TruckNumber', 'Chassis', 'GrossWt', 'CargoWt', 'Seals' ] namelist = page_soup.findAll('span', {'class': 'info-item-label'}) itemlist = page_soup.findAll('span', {'class': 'info-item-value'}) for jx, key in enumerate(keys): for j, name in enumerate(namelist): thisname = name.text.strip() if thisname == key: item = itemlist[j].text.strip() if len(item) > 1: newkey = labels[jx] conset.update({newkey: item}) print(key, item) else: print('Not valid:', key, item) if printif == 1 else 1 idat = Interchange.query.filter( (Interchange.Container == thiscon) & (Interchange.Type == movetyp)).first() if idat is not None: type = movetyp.upper() type = type.replace(' ', '_') viewfile = thiscon + '_' + type + '.pdf' idat.Chassis = conset.get("Chassis") idat.TruckNumber = conset.get("TruckNumber") idat.GrossWt = conset.get("GrossWt") idat.CargoWt = conset.get("CargoWt") idat.Seals = conset.get("Seals") idat.Original = viewfile db.session.commit() pdfkit.from_string(con_data, outpath + viewfile) outpath = addpath3('interchange/') newfile = outpath + viewfile copyline = f'scp {newfile} {websites["ssh_data"]+"vinterchange"}' print('copyline=', copyline) os.system(copyline) os.remove(newfile) browser.quit() return
#_____________________________________________________________________________________________________________ if fleet > 0: if fleet == 1: usernamelist = [usernames['inf2']] password = passwords['inf2'] dayback = 14 if fleet == 2: usernamelist = [usernames['inf2']] password = passwords['inf2'] dayback = 410 datefrom = (datetime.date.today() - datetime.timedelta(dayback)).strftime("%d-%b-%Y") print('Running Fleet from...', datefrom) att_dir = addpath3('emaildocs/msfleet') txt_file = addpath3('emaildocs/fleet.txt') for username in usernamelist: con = imaplib.IMAP4_SSL(imap_url) con.login(username, password) con.select('INBOX') msgs = get_emails( search_from_date('FROM', '*****@*****.**', con, datefrom), con) flist = os.listdir(att_dir) nbills = len(flist) fleetbillsadd = [] fleetbillsupdate = [] for j, msg in enumerate(msgs):
def report_contents(file4, item): ltm, rtm, bump, tb, ctrall, left_ctr, right_ctr, dl, dh, tdl, hls, m1, m2, m3, m4, m5, m6, m7, n1, n2, n3 = reportsettings( 1) pages = [file4] page = 1 c = canvas.Canvas(file4, pagesize=letter) c.setLineWidth(1) #Main Items Listing c.setFont('Helvetica-Bold', 14, leading=None) mid = int(ltm + rtm) / 2 if item == 'drivers': c.drawCentredString(mid, 535, 'Active Driver Data') if item == 'trucks': c.drawCentredString(mid, 535, 'Active DOT Truck Data') c.line(ltm, 550, rtm, 550) c.line(ltm, 530, rtm, 530) c.setFont('Helvetica', 10, leading=None) top = 510 leftw1 = ltm + 10 leftw2 = ltm + 110 leftw3 = ltm + 320 leftw4 = ltm + 490 if item == 'introduction': c.drawCentredString(mid, 535, 'Introduction') ddata = KeyInfo.query.all() presents = [ 'overview', 'focus', 'truckstart', 'logging', 'rod', 'maintenance', 'testing', 'accidents' ] for present in presents: for dd in ddata: if dd.Type == present: c.drawString(leftw1, top, dd.Type.title()) slist = parseline(dd.Description, 90) for sl in slist: c.drawString(leftw2, top, sl) top = top - dl * .9 top = top - dl * .5 if top < n3: c, page, top, pages = topcheck(file4, n2, dh, c, page, pages) if item == 'drivers': ddata = Drivers.query.filter(Drivers.JobEnd > today).all() for dd in ddata: c.drawString(leftw1, top, dd.Name) c.drawString(leftw2, top, f'{dd.Addr1}, {dd.Addr2}') top = top - dl c.drawString(leftw2, top, f'{dd.Phone} {dd.Email}') top = top - dl c.drawString(leftw2, top, f'Started with Company: {dd.JobStart}') c.drawString(leftw3, top, f'DOB: {dd.DOB}') top = top - dl c.drawString( leftw2, top, f'CDL Information: {dd.CDLstate} #{dd.CDLnum} Expires:{dd.CDLexpire}' ) top = top - dl c.drawString( leftw2, top, f'Medical Expires: {dd.MedExpire} TWIC Expires: {dd.TwicExpire}' ) top = top - dl c.drawString( leftw2, top, f'Pre-Employment Screening: {dd.PreScreen} Last Screening Completed: {dd.LastTested}' ) top = top - dl * 2 if top < n3: c, page, top, pages = topcheck(file4, n2, dh, c, page, pages) if item == 'trucks': ddata = Vehicles.query.filter(Vehicles.DOTNum != None).all() for dd in ddata: c.drawString(leftw1, top, f'Unit #{dd.Unit}') c.drawString( leftw2, top, f'{dd.Year} {dd.Make} {dd.Model} Place in Service:{dd.ServStr}' ) top = top - dl c.drawString( leftw2, top, f'Color: {dd.Color} VIN: {dd.VIN} MD Plate:{dd.Plate}') top = top - dl c.drawString( leftw2, top, f'MD Title: {dd.Title} EmptyWt: {dd.EmpWeight} GrossWt: {dd.GrossWt}' ) top = top - dl c.drawString( leftw2, top, f'EzPass Transponder: {dd.Ezpassxponder} Port Transponer: {dd.Portxponder}' ) top = top - dl * 2 if top < n3: c, page, top, pages = topcheck(file4, n2, dh, c, page, pages) addtickets = 0 if addtickets == 1: first = 0 last = 0 for ix, val in enumerate(valticket): if ix == 0: tlist = val.Time.split(':') firsttime = int(tlist[0]) lasttime = firsttime else: tlist = val.Time.split(':') thistime = int(tlist[0]) if thistime < firsttime: first = ix firsttime = thistime elif thistime >= lasttime: last = ix lasttime = thistime for ix, val in enumerate(valticket): if ix == first or ix == last: placefile = addpath3(f'interchange/{val.Original}') if os.path.isfile(placefile): print( f'Have interchange ticket for {thisdate} {plate}' ) else: pythonline = websites[ 'ssh_data'] + f'vinterchange/{val.Original}' placefile = addpath3(f'interchange/{val.Original}') copyline1 = f'scp {pythonline} {placefile}' print(copyline1) os.system(copyline1) if ix == first: placefile1 = placefile if ix == last: placefile2 = placefile blendfile = addpath3(f'interchange/BLEND_{val.Original}') if os.path.isfile(blendfile): print('Have this blend file') else: blendticks(placefile1, placefile2, blendfile) valpdfs.append(blendfile) c.showPage() c.save() if len(pages) > 1: pdfcommand = ['pdfunite'] for page in pages: pdfcommand.append(page) multioutput = addpath1(f'reports/multioutput' + '.pdf') pdfcommand.append(multioutput) tes = subprocess.check_output(pdfcommand) else: multioutput = ' ' return pages, multioutput
newname = filename.replace('.jpeg', '.jpg') os.rename(s1 + filename, s1 + newname) folderlist = [ 'bills', 'pods', 'general', 'titles', 'dispatch', 'interchange', 'tjobs', 'orders', 'bookings' ] if 1 == 1: # Keep this in case want to convert back to a looping script nfiles = 0 processlist = [] for folder in folderlist: i5s = addpath2(folder + '/') s5s = addpath3(folder + '/') filefix(i5s) filelist = os.listdir(i5s) #print(folder,i5s,filelist) if filelist: for filename in filelist: nfiles += 1 processlist.append(filename) shutil.move(i5s + filename, s5s + filename) print('Number of files processed:', nfiles) if nfiles > 0: for folder in folderlist: s5 = addpath3(folder) s5s = s5 + '/'