Skip to content

Instantly share code, notes, and snippets.

@empirasign
Last active March 30, 2022 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save empirasign/78ab77a1e947ab57f3bf4eaaaea85b8e to your computer and use it in GitHub Desktop.
Save empirasign/78ab77a1e947ab57f3bf4eaaaea85b8e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
parse_run.py
example script showing how to parse a runs message embedded in an
email via the Empirasign Parser as a Service API endpoint.
Supported Sectors: Structured Products, Corporates, and Loans
To get set up with a Free Trial, contact us at:
info@empirasign.com
+1 646-837-8849
https://gist.github.com/empirasign/78ab77a1e947ab57f3bf4eaaaea85b8e
example usage:
./parse_run.py dealer_run_abs.eml # eml/rfc822 formated run
./parse_run.py dealer_run_cmbs.msg # MS Outlook formated run
notes on email file formats
https://www.msoutlookware.com/difference/msg-vs-eml.html
https://www.w3.org/Protocols/rfc822/
https://blogs.msdn.microsoft.com/openspecification/2009/11/06/msg-file-format-part-1/
"""
import argparse
import base64
import json
import pprint
import requests
API_KEY = "MY_API_KEY" # provided by Empirasign
API_SECRET = "MY_API_SECRET" # provided by Empirasign
def main():
"""
the main event
"""
parser = argparse.ArgumentParser(description='parse email runs message in eml or msg format')
parser.add_argument(dest="infile", help="input msg/eml filename")
parser.add_argument('-s',
dest="sector",
choices=["sp", "corp", "loan"],
default="sp",
help="sector parser endpoint to hit, deafault is structured products")
parser.add_argument('-o', dest="outfile", help="output filename [not required]")
args = parser.parse_args()
paths = {"sp": "parse-run", "corp": "parse-corp", "loan": "parse-loan"}
api_url = 'https://api.empirasign.com/v1/{}/'.format(paths[args.sector])
infile = args.infile
if not args.outfile:
outfile = infile.rsplit(".", 1)[0] + ".json"
elif not args.outfile.endswith(".json"):
outfile = args.outfile + ".json"
else:
outfile = args.outfile
post_data = {'api_key': API_KEY, 'api_secret': API_SECRET}
if args.infile.lower().endswith(".msg"):
post_data["msg"] = base64.b64encode(open(infile, "rb").read()).decode("ascii")
else:
post_data["rfc"] = open(infile).read()
res = requests.post(api_url, json=post_data)
if res.status_code == 200:
obj = res.json()
pprint.pprint(obj)
with open(outfile, "w") as f:
f.write(json.dumps(obj, sort_keys=True, indent=2, ensure_ascii=False))
print("\nresults file saved: {}".format(outfile))
else:
print("some sort of error occurred, http status code: {}".format(res.status_code))
pprint.pprint(res.json())
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment