''' Created on Nov 3, 2015 @author: wes ''' import ftplib import sys, sqlite3 import logging import os.path from glob import glob #logger = None LOG_LEVEL = logging.DEBUG ################################################################################ def load_ftp_credentials(code): vault = sqlite3.connect('D:\\FTP\\ftp.db') cursor = vault.execute("SELECT * from ftp_auth where code='" + code + "'") for row in cursor: return [row[1], row[3], row[4]] def gettext(ftp, filename, outfile=None): if outfile is None: outfile = sys.stdout # use a lambda to add newlines to the lines read from the server ftp.retrlines("RETR " + filename, lambda s, w=outfile.write: w(s+"\n")) def getbinary(ftp, filename, outfile=None): if outfile is None: outfile = sys.stdout ftp.retrbinary("RETR " + filename, outfile.write) def getFileList(ftp): log = [] ftp.retrlines('LIST', callback=log.append) files = (line.rsplit(None, 1)[1] for line in log) return list(files) def upload(ftp, f): ext = os.path.splitext(f)[1] if ext in (".txt", ".htm", ".html", ".xml"): ftp.storlines("STOR " + f.split('\\')[-1], open(f)) else: ftp.storbinary("STOR " + f.split('\\')[-1], open(f, "rb"), 1024) def initializeLogger(name='log'): #global logger #logger = logging.getLogger(__name__) logger = logging.getLogger(name) if len(logger.handlers) > 0: return logger logger.setLevel(LOG_LEVEL) handler = logging.FileHandler('D:\\FTP\\logs\\' + name + '.log') handler.setLevel(LOG_LEVEL) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) #Also send logs > DEBUG to stdout ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) logger.addHandler(ch) return logger ################################################################################ def kravetOrderDownload(): logger = initializeLogger('kravet_850') KRAVET = load_ftp_credentials('kf') logger.info('Connecting to ' + KRAVET[0] + ' as ' + KRAVET[1]) ftp = ftplib.FTP(KRAVET[0]) ftp.login(KRAVET[1], KRAVET[2]) for f in getFileList(ftp): #logger.debug(f) if(f.lower().startswith('kravet_to_vendor')): if(os.path.isfile(f)): logger.info('Already downloaded:' + f) else: logger.info('Downloading: ' + f) gettext(ftp, f, open(f, 'w')) logger.info('Closing connection to ' + KRAVET[0]) ftp.quit() def maharamOrderDownload(): logger = initializeLogger('Maharam_850') MAHARAM = load_ftp_credentials('mf') logger.info('Connecting to ' + MAHARAM[0] + ' as ' + MAHARAM[1]) ftp = ftplib.FTP(MAHARAM[0]) ftp.login(MAHARAM[1], MAHARAM[2]) logger.info('cwd OUTGOING') ftp.cwd('OUTGOING') for f in getFileList(ftp): #logger.debug(f) if(f.lower().startswith('mah_850_')): if(os.path.isfile(f)): logger.info('Already downloaded:' + f) else: logger.info('Downloading: ' + f) gettext(ftp, f, open(f, 'w')) logger.info('Closing connection to ' + MAHARAM[0]) ftp.quit() def maharamInvoiceUpload(): logger = initializeLogger('Maharam_810') MAHARAM = load_ftp_credentials('mf') logger.info('Connecting to ' + MAHARAM[0] + ' as ' + MAHARAM[1]) ftp = ftplib.FTP(MAHARAM[0]) ftp.login(MAHARAM[1], MAHARAM[2]) logger.info('cwd Incoming') ftp.cwd('Incoming') files = glob('D:\\FTP\\SEND_MF\\*.xml') for f in files: logger.info('Uploading ' + f.split('\\')[-1]) upload(ftp, f) logger.info('Moving ' + f + ' to D:\\FTP\\SEND_MF\\SENT') os.rename(f, 'SEND_MF\\SENT\\' + f.split('\\')[-1]) logger.info('Closing connection to ' + MAHARAM[0]) ftp.quit() def maharamASNUpload(): logger = initializeLogger('Maharam_856') MAHARAM = load_ftp_credentials('mf') logger.info('Connecting to ' + MAHARAM[0] + ' as ' + MAHARAM[1]) ftp = ftplib.FTP(MAHARAM[0]) ftp.login(MAHARAM[1], MAHARAM[2]) logger.info('cwd Incoming') ftp.cwd('Incoming') files = glob('D:\\FTP\\SEND_MF\\STM_856_*.txt') for f in files: logger.info('Uploading ' + f.split('\\')[-1]) upload(ftp, f) logger.info('Moving ' + f + ' to D:\\FTP\\SEND_MF\\SENT') os.rename(f, 'SEND_MF\\SENT\\' + f.split('\\')[-1]) logger.info('Closing connection to ' + MAHARAM[0]) ftp.quit() def momentumOrderDownload(): logger = initializeLogger('Momentum_850') MOMENTUM = load_ftp_credentials('mg') logger.info('Connecting to ' + MOMENTUM[0] + ' as ' + MOMENTUM[1]) ftp = ftplib.FTP(MOMENTUM[0]) ftp.login(MOMENTUM[1], MOMENTUM[2]) logger.info('cwd outbound') ftp.cwd('outbound') for f in getFileList(ftp): #logger.debug(f) if(f.lower().startswith('sbr_850_') and f.lower().endswith('.txt')): if(os.path.isfile(f)): logger.info('Already downloaded:' + f) else: logger.info('Downloading: ' + f) gettext(ftp, f, open(f, 'w')) logger.info('Closing connection to ' + MOMENTUM[0]) ftp.quit() def momentumInvoiceUpload(): logger = initializeLogger('Momentum_810') MOMENTUM = load_ftp_credentials('mg') logger.info('Connecting to ' + MOMENTUM[0] + ' as ' + MOMENTUM[1]) ftp = ftplib.FTP(MOMENTUM[0]) ftp.login(MOMENTUM[1], MOMENTUM[2]) logger.info('cwd inbound') ftp.cwd('inbound') files = glob('D:\\FTP\\SEND_MG\\SBR_810_*.txt') for f in files: logger.info('Uploading ' + f.split('\\')[-1]) upload(ftp, f) #Create an empty file of the same name with '.done' extension and upload #They require it for some reason.... doneFile = f.split('.')[0] + '.done' file(doneFile, 'w').writelines([]) logger.info('Uploading ' + doneFile.split('\\')[-1]) upload(ftp, doneFile) logger.info('Deleting ' + doneFile) os.remove(doneFile) logger.info('Moving ' + f + ' to D:\\FTP\\SEND_MG\\SENT') os.rename(f, 'SEND_MG\\SENT\\' + f.split('\\')[-1]) logger.info('Closing connection to ' + MOMENTUM[0]) ftp.quit() def momentumASNUpload(): logger = initializeLogger('Momentum_856') MOMENTUM = load_ftp_credentials('mg') logger.info('Connecting to ' + MOMENTUM[0] + ' as ' + MOMENTUM[1]) ftp = ftplib.FTP(MOMENTUM[0]) ftp.login(MOMENTUM[1], MOMENTUM[2]) logger.info('cwd inbound') ftp.cwd('inbound') files = glob('D:\\FTP\\SEND_MG\\SBR_856_*.txt') for f in files: logger.info('Uploading ' + f.split('\\')[-1]) upload(ftp, f) #Create an empty file of the same name with '.done' extension and upload #They require it for some reason.... doneFile = f.split('.')[0] + '.done' file(doneFile, 'w').writelines([]) logger.info('Uploading ' + doneFile.split('\\')[-1]) upload(ftp, doneFile) logger.info('Deleting ' + doneFile) os.remove(doneFile) logger.info('Moving ' + f + ' to D:\\FTP\\SEND_MG\\SENT') os.rename(f, 'SEND_MG\\SENT\\' + f.split('\\')[-1]) logger.info('Closing connection to ' + MOMENTUM[0]) ftp.quit() def glenravenASNDownload(): logger = initializeLogger('GR_INCOMING__856') #They're using our FTP site for this STM_FTP = load_ftp_credentials('stm') logger.info('Connecting to %s as %s', STM_FTP[0], STM_FTP[1]) ftp = ftplib.FTP(STM_FTP[0]) ftp.login(STM_FTP[1], STM_FTP[2]) logger.info('cwd glenraven') ftp.cwd('glenraven') logger.info('cwd to_STM') ftp.cwd('to_STM') for f in getFileList(ftp): if f in ['.', '..']: continue f_local = 'INCOMING\\' + f.replace('sunbury', 'GR_ASN') if(os.path.isfile(f_local)): logger.info('Already downloaded: %s', f) else: logger.info('Downloading: %s -> %s', f, f_local) gettext(ftp, f, open(f_local, 'w')) logger.info('Deleting: %s from FTP site', f) ftp.delete(f) logger.info('Closing connection to %s', STM_FTP[0]) ftp.quit() def UNIFI_846_Download(): logger = initializeLogger('GR_INCOMING__856') #They're using our FTP site for this STM_FTP = load_ftp_credentials('stm') logger.info('Connecting to %s as %s', STM_FTP[0], STM_FTP[1]) ftp = ftplib.FTP(STM_FTP[0]) ftp.login(STM_FTP[1], STM_FTP[2]) logger.info('cwd unifi') ftp.cwd('unifi') logger.info('cwd to_STM') ftp.cwd('to_STM') for f in getFileList(ftp): if f in ['.', '..']: continue f_local = 'INCOMING\\' + f.replace('ipnet', 'UNIFI_846').replace('.file846', '') f_local = f_local[:f_local.index('.csv') + 4] if(os.path.isfile(f_local)): logger.info('Already downloaded: %s', f) else: logger.info('Downloading: %s -> %s', f, f_local) gettext(ftp, f, open(f_local, 'w')) logger.info('Deleting: %s from FTP site', f) ftp.delete(f) logger.info('Closing connection to %s', STM_FTP[0]) ftp.quit() if __name__ == '__main__': if(len(sys.argv) < 2): print 'Missing parameter!' sys.exit(0) if(len(sys.argv) == 3): l = sys.argv[2].lower() if(l.startswith('debug')): LOG_LEVEL = logging.DEBUG elif(l.startswith('info')): LOG_LEVEL = logging.INFO elif(l.startswith('warning')): LOG_LEVEL = logging.WARNING elif(l.startswith('error')): LOG_LEVEL = logging.ERROR elif(l.startswith('critical')): LOG_LEVEL = logging.CRITICAL s = sys.argv[1].lower() if(s.startswith('kf850')): kravetOrderDownload() if(s.startswith('mf850')): maharamOrderDownload() if(s.startswith('mg850')): momentumOrderDownload() if(s.startswith('mf810')): maharamInvoiceUpload() if(s.startswith('mf856')): maharamASNUpload() if(s.startswith('mg810')): momentumInvoiceUpload() if(s.startswith('mg856')): momentumASNUpload() if(s.startswith('gr856')): glenravenASNDownload() if(s.startswith('un846')): UNIFI_846_Download()