diff options
| -rw-r--r-- | python/collateral_calc.py | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/python/collateral_calc.py b/python/collateral_calc.py index 39c03340..af2e49c3 100644 --- a/python/collateral_calc.py +++ b/python/collateral_calc.py @@ -9,10 +9,17 @@ from ftplib import FTP from pathlib import Path from time import sleep from pandas.tseries.offsets import BDay +from paramiko import Transport, SFTPClient DAILY_DIR = Path(os.environ["DAILY_DIR"]) +def get_sftp_client(): + transport = Transport(('prmssp.amer.sgcib.com', 22)) + transport.connect(username='SerenitasGamma@USA', password='SSqrrLL99') + return SFTPClient.from_transport(transport) + + def download_files(d=None, report_types=["OTC_CASH_ACTIVITY", "OTC_POSITIONS", "OTC_MARGIN"], @@ -45,6 +52,35 @@ def download_files(d=None, ftp.retrbinary('RETR ' + f, fh.write) +def download_sftp_files(d=None, + report_types=["OTC_CASH_ACTIVITY", "OTC_POSITIONS", + "OTC_MARGIN"], + retry_count=0): + if retry_count > 20: + return + DATA_DIR = DAILY_DIR / "SG_reports" + sftp = get_sftp_client() + if d is None: + for f in sftp.listdir('OTC'): + if f.endswith("csv"): + for report_type in report_types: + if f.endswith(f"{report_type}.csv"): + sftp.get(f"OTC/{f}", localpath=DATA_DIR / f) + else: + file_list = sftp.listdir('OTC') + for report_type in report_types: + f = f"{d:%Y%m%d}_{report_type}.csv" + if f not in file_list: + logging.info("File not here yet, trying again in 500s...") + logging.info(f"Try count: {retry_count}") + sleep(500) + sftp.close() + download_sftp_files(d, report_types, retry_count + 1) + else: + sftp.get(f"OTC/{f}", localpath=DATA_DIR / f) + sftp.close() + + def download_ms_emails(count=20): emails = get_msgs(path=["NYops"], subject_filter="SERCX **Daily", count=count) @@ -52,7 +88,7 @@ def download_ms_emails(count=20): for msg in emails: for attach in msg.attachments: if 'NETSwaps' in attach.name: - fname = "Trade_Detail_" + attach.name.split("_")[1] + fname = "Trade_Detail_" + attach.name.split("_")[1] elif 'NET_Collateral' in attach.name: fname = "Collateral_Detail_" + attach.name.rsplit("_", 1)[1] else: |
