Skip to content

Instantly share code, notes, and snippets.

@shercoder
Created July 9, 2015 22:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shercoder/d6b1884099c3189fa705 to your computer and use it in GitHub Desktop.
Save shercoder/d6b1884099c3189fa705 to your computer and use it in GitHub Desktop.
Script to parse Localytics data and display in readable format when using mitmproxy server
######################################################################
### How to run this script: mitmdump -q -s mitmproxy_localytics.py ###
######################################################################
import re
import gzip
from StringIO import StringIO
import json
from pprint import pprint
class color:
BLUE = '\033[94m'
YELLOW = '\033[93m'
GREEN = '\033[92m'
RED = '\033[91m'
END = '\033[0m'
def request(ctx, flow):
if 'analytics.localytics.com' not in flow.request.host:
return
content = flow.request.content
if content:
readContent = gzip.GzipFile(fileobj=StringIO(content)).read()
contentArray = readContent.split('\n')
for i in contentArray:
if not i or i == '':
continue
d = json.loads(i)
# print flow
if 'fl' in d:
print("%sScreen Flow:%s %s"%(color.RED, color.END, d['fl']))
# print event data
if 'n' in d:
event_name = d.get('n', 'Unknown')
print("%s%s%s %s[Event: %s]%s %s%s%s"%(color.RED, '='*25, color.END,
color.BLUE, event_name, color.END,
color.RED, '='*25, color.END))
customDimensionPattern = re.compile("^c[0-9]$")
for key in d:
# we do not need to print the event name again
if key == 'n':
continue
value = d[key]
if key == 'attrs':
for customAttrKey in value:
print("%sAttribute %s =%s %s"%(color.YELLOW, customAttrKey, color.END, value[customAttrKey]))
elif customDimensionPattern.match(key):
print("%sDimension %s =%s %s"%(color.GREEN, key, color.END, value))
else:
print("%sAttribute %s =%s %s"%(color.YELLOW, key, color.END, value))
print("%s%s%s\n\n"%(color.RED, '='*100, color.END))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment