Skip to content

Instantly share code, notes, and snippets.

@empirasign
Last active February 24, 2022 22:06
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/0835086dc08aec6e767a7bfc6f96c85c to your computer and use it in GitHub Desktop.
Save empirasign/0835086dc08aec6e767a7bfc6f96c85c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
parse_bwic.py
example script showing how to parse a structured product BWIC embedded in an
email via the Empirasign Parser as a Service API endpoint.
To get set up with a Free Trial, contact us at:
info@empirasign.com
+1 646-837-8849
https://gist.github.com/empirasign/0835086dc08aec6e767a7bfc6f96c85c
example usage:
./parse_bwic.py hf_bwic.eml # eml/rfc822 formated run
./parse_bwic.py reit_bwic.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
API_URL = 'https://api.empirasign.com/v1/parse-bwic/'
def main():
"""
the main event
"""
parser = argparse.ArgumentParser(description='parse BWIC email message in eml/msg format')
parser.add_argument("infile", help="input msg/eml filename")
parser.add_argument('-o', dest="outfile", help="output filename [not required]")
args = parser.parse_args()
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, timeout=3.5)
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(obj)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment