aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/collateral_calc.py36
1 files changed, 31 insertions, 5 deletions
diff --git a/python/collateral_calc.py b/python/collateral_calc.py
index 241e3972..97a446d5 100644
--- a/python/collateral_calc.py
+++ b/python/collateral_calc.py
@@ -9,6 +9,7 @@ from pathlib import Path
from time import sleep
from pandas.tseries.offsets import BDay
from paramiko import Transport, SFTPClient
+from download_emails import GmailMessage
DAILY_DIR = Path(os.environ["DAILY_DIR"])
@@ -47,9 +48,34 @@ def download_sftp_files(d=None,
sftp.get(f"OTC/{f}", localpath=DATA_DIR / f)
sftp.close()
+def download_ms_emails_from_gmail():
+ DATA_DIR = DAILY_DIR / "MS_reports"
+ for msg in GmailMessage.List_msg_ids('Globeop/Operations'):
+ try:
+ message = GmailMessage.from_id(msg['id'])
+ subject = message['subject']
+ if 'SERCX **Daily' in subject:
+ for attach in message.iter_attachments():
+ fname = attach.get_filename()
+ if 'NETSwaps' in fname:
+ fname = "Trade_Detail_" + fname.split("_")[1]
+ elif 'NET_Collateral' in fname:
+ fname = "Collateral_Detail_" + fname.rsplit("_", 1)[1]
+ else:
+ continue
+ p = DATA_DIR / fname
+ if p.exists():
+
+ continue
+ else:
+ p.write_bytes(part.get_payload(decode=True))
+ except (KeyError, UnicodeDecodeError) as e:
+ logging.error("error decoding " + msg['id'])
+ continue
def download_ms_emails(count=20):
- emails = get_msgs(path=["NYops", "Margin calls MS"], subject_filter="SERCX **Daily",
+ emails = get_msgs(path=["NYops", "Margin calls MS"],
+ subject_filter="SERCX **Daily",
count=count)
DATA_DIR = DAILY_DIR / "MS_reports"
for msg in emails:
@@ -60,8 +86,9 @@ def download_ms_emails(count=20):
fname = "Collateral_Detail_" + attach.name.rsplit("_", 1)[1]
else:
continue
- with open(DATA_DIR / fname, "wb") as fh:
- fh.write(attach.content)
+ p = DATA_DIR / fname
+ if not p.exists():
+ p.write_bytes(attach.content)
def download_gs_emails(count=20):
@@ -75,8 +102,7 @@ def download_gs_emails(count=20):
if fname.endswith('xls'):
p = DATA_DIR / fname
if not p.exists():
- with open(DATA_DIR / fname, "wb") as fh:
- fh.write(attach.content)
+ p.write_bytes(attach.content)
def sg_collateral(d):