diff options
Diffstat (limited to 'python/markit/cds.py')
| -rw-r--r-- | python/markit/cds.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/python/markit/cds.py b/python/markit/cds.py new file mode 100644 index 00000000..7fede498 --- /dev/null +++ b/python/markit/cds.py @@ -0,0 +1,46 @@ +import io
+import logging
+import os
+import requests
+import shutil
+import zipfile
+from common import root
+from pandas.tseries.offsets import BDay
+
+logger = logging.getLogger(__name__)
+
+def convertToNone(v):
+ return v if v else None
+
+def download_cds_data(payload):
+ r = requests.get('https://www.markit.com/export.jsp', params=payload)
+ f2 = open(os.path.join(root, "Tranche_data", "CDS", "cds eod {0}.csv".format(payload['date'])), "wb")
+ with zipfile.ZipFile(io.BytesIO(r.content)) as z:
+ for f in z.namelist():
+ if "csv" in f:
+ f1 = z.open(f)
+ next(f1)
+ next(f1)
+ shutil.copyfileobj(f1, f2)
+ f1.close()
+ f2.close()
+
+def download_composite_data(payload, historical=False):
+ ## if historical, we want to maintain the invariant mtime(f)== payload['date'] + BDay(1)
+ if historical:
+ ts = datetime.datetime.strptime(payload['date'], "%Y%m%d") + BDay(1)
+ ts = ts.timestamp()
+ for report in ['COMPOSITES', 'TRANCHE_COMPOSITES']:
+ for family in ['CDX', 'ITRAXX-EUROPE']:
+ payload.update({'family': family, 'report': report})
+ r = requests.get('https://www.markit.com/export.jsp', params=payload)
+ try:
+ with zipfile.ZipFile(io.BytesIO(r.content)) as z:
+ for f in z.namelist():
+ if "csv" in f:
+ path = z.extract(f, path=os.path.join(root, "Tranche_data", "Composite_reports"))
+ if historical:
+ os.utime(path, (ts, ts))
+ except zipfile.BadZipfile:
+ logger.error(r.content)
+ continue
|
