#!/bin/python import httplib, urllib, sys, md5, subprocess, datetime, ftplib, os #filenamePDF = sys.argv[1] pathTiff = sys.argv[1] fileExtPDF = os.path.split(sys.argv[2])[1] recvq = '/var/spool/hylafax/recvq/' webserver = "cha078.cha.toplingo.com" webserverExt = "/webservice.asmx/SaveFax" orphanExt = "/webservice.asmx/SaveOrphanFax" ftpServer = "4.79.143.4" ftpUser = "cha_fax" ftpPwd = "noMore7inux" ftpFolder = "/cha/cha078.cha.toplingo.com/fax/" faxMachine = "b" #GET ALL BARCODES FROM FAX def getBarcodes(): print "\nDecoding Barcodes" tiffName = recvq + fileExtPDF.split('.')[0] + "%d.tif" command = [r'/var/spool/hylafax/bin/bardecoder_6_2_1_lic/bin/bardecode','-m','-P','-d','1','-n','30','-S', tiffName, '-t','any','-f', pathTiff] p = subprocess.Popen(command, stdout=subprocess.PIPE) b = p.stdout.read() barcodes = b.split("\n") print "\n" return barcodes[:-1] def tiff2pdf(fileName): print "Converting to pdf: " + fileName name = fileName.split('.') name = name[0]+".pdf" command =[r'/usr/bin/tiff2pdf',fileName,'-o',name] p = subprocess.Popen(command, stdout=subprocess.PIPE).wait() return name def tiff2pdfOrphan(fileName): print "Converting to pdf: " + fileName name = fileName.split('.') name = name[0]+faxMachine+".pdf" command =[r'/usr/bin/tiff2pdf',fileName,'-o',name] p = subprocess.Popen(command, stdout=subprocess.PIPE).wait() return name #Move File to Ftp Share Folder def ftpFile(fileName): print "FTP ATTEMPT: " + fileName ftp = ftplib.FTP() try: ftp.connect(ftpServer) ftp.login(ftpUser,ftpPwd) ftp.cwd(ftpFolder) name = os.path.split(fileName)[1] f = open(fileName, "rb") ftp.storbinary('STOR ' + name, f) f.close() print "FTP DONE" ftp.quit() return 1 except Exception, e: ftp.quit() print e return -1 #Log Barcode Error def barcodeError(): file = open("/var/spool/hylafax/log/BarcodeError.log", 'a') file.write("\n----------------------------------------------------------\n") file.write(str(datetime.datetime.now()) + "\n") file.write("Error while finding barcode. Filenames: " + pathTiff + "\n") file.close() #Log Ftp Error def ftpError(): print "ftp error" file = open("/var/spool/hylafax/log/FtpError.log", 'a') file.write("\n-----------------------------------------------------------\n") file.write(str(datetime.datetime.now()) + "\n") file.write("Error in FTP. Filenames: " + pathTiff + "\n") file.close() #Log Script Error def scriptError(e): print e file = open("/var/spool/hylafax/log/scriptError.log", 'a') file.write("\n-----------------------------------------------------------\n") file.write(str(datetime.datetime.now()) + "\n") file.write("Error in Webservice.py scrit. Filenames: " + pathTiff + "\n") file.close() def orphan(): file = open("/var/spool/hylafax/log/Orphan.log", 'a') file.write("\n----------------------------------------------------\n") file.write(str(datetime.datetime.now()) + "\n") file.write("Orphan found: " + pathTiff + "\n") file.close() name = tiff2pdfOrphan(pathTiff) imagepath = "fax/" + os.path.split(name)[1] ftpFile(name) print "HTTP POST Orphan" print imagepath params = urllib.urlencode({'ImagePath':imagepath}) headers = {"Content-type": "application/x-www-form-urlencoded"} conn = httplib.HTTPConnection(webserver) conn.request("POST", orphanExt, params, headers) resp = conn.getresponse() print resp.read() conn.close() #MAKE HTTP POST TO SAVE_FAX WEBSERVICE def ftpAndPost(barcode, fileName): fileN = fileExtPDF.split('.')[0] name = tiff2pdf(recvq+ fileN + str(fileName) +".tif") imagepath = "fax/" + os.path.split(name)[1] returnVal = ftpFile(name) print "HTTP POST Attempt\n" params = urllib.urlencode({'ImagePath':imagepath,'BarCodeGUID':barcode}) headers = {"Content-type": "application/x-www-form-urlencoded"} conn = httplib.HTTPConnection(webserver) conn.request("POST", webserverExt ,params, headers) resp = conn.getresponse() print resp.read() conn.close() #SCRIPT STARTS HERE try: #GET ALL BARCODES FROM FAX barcodes = getBarcodes() print barcodes #If no barcodes, record error, orphan file and exit if(len(barcodes)<1 or barcodes[0][:1]!="1"): barcodeError() orphan() sys.exit() i=1 for barcode in barcodes: if (len(barcode)>1): ftpAndPost(barcode[2:], i) i = i + 1 except Exception, e: scriptError(e)