Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Expand a HAR dump containing OpenERP traffic out.
"""Expand OpenERP dump.
Usage:
expand_openerp_dump.py <traffic.har>
expand_openerp_dump.py (-h | --help | --version)
Options:
-h, --help Show this screen and exit.
"""
from __future__ import print_function
import json
from docopt import docopt
import xmltodict
from pprint import pprint
from base64 import b64decode
import sys
if __name__ == '__main__':
arguments = docopt(__doc__, version='CouchDB Upload 0.1')
if '<traffic.har>' in arguments:
filename = arguments['<traffic.har>']
with open(filename, 'r') as f:
har = json.load(f)
entries = har['log']['entries']
line = 1
for entry in entries:
entry['type'] = 'HAR'
entry['filename'] = filename
entry['line'] = line
line = line + 1
request = entry['request']
postData = request.get('postData')
if postData:
data = postData.get('text')
if postData.get('encoding') == 'base64':
data = b64decode(data)
if data:
if postData.get('mimeType') == 'text/xml':
xml_dict = xmltodict.parse(data)
postData['text'] = xml_dict
elif postData.get('mimeType') == 'application/json':
postData['text'] = json.loads(data)
response = entry['response']
content = response.get('content')
if content:
text = content.get('text')
if text:
if content.get('encoding') == 'base64':
text = b64decode(text)
content['text'] = text
if content.get('mimeType') == 'text/xml':
try:
xml_dict = xmltodict.parse(text)
content['text'] = xml_dict
except Exception, e:
print(e, file=sys.stderr)
print(text, file=sys.stderr)
print(json.dumps(entry), file=sys.stderr)
elif content.get('mimeType') == 'application/json':
try:
content['text'] = json.loads(text)
except Exception, e:
print(e, file=sys.stderr)
print(text, file=sys.stderr)
print(json.dumps(entry), file=sys.stderr)
print(json.dumps(entries, sort_keys=True, indent=2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment