Skip to content

Instantly share code, notes, and snippets.

@danielgross
Created June 23, 2019 00:29
Show Gist options
  • Save danielgross/f196ee1e88448948a7e844d918c06b7e to your computer and use it in GitHub Desktop.
Save danielgross/f196ee1e88448948a7e844d918c06b7e to your computer and use it in GitHub Desktop.
Convert a Mixmax Poll to CSV
"""This script converts MixMax poll responses into a CSV.
Apologies in advance for not using requests or any delightful
Python. This file is meant to properly run as a gist with no libraries."""
# TODO: Paginate when MixMax's API is fixed.
import urllib2
import json
import csv
import StringIO
import os
BASE_URL = 'https://api.mixmax.com/v1'
POLL_ID = # Your Poll ID
SEQUENCE_ID = # Your Sequence ID
API_TOKEN = None
with open(os.path.expanduser('~/.mixmax-token')) as fhandle:
API_TOKEN = fhandle.read().strip()
def to_string(thing):
"""Return a string."""
return str(thing) if isinstance(thing, int) else thing.encode('utf8')
def api_get(endpoint):
"""Perform a GET, JSON decode and return."""
url = BASE_URL + endpoint
req = urllib2.Request(url)
req.add_header('X-API-Token', API_TOKEN)
resp = urllib2.urlopen(req)
return json.loads(resp.read())
def get_sequence_recipients(sequence_id):
"""Get the sequence recipients."""
content = api_get('/sequences/' + SEQUENCE_ID + '/recipients?limit=300')
return set([x['to']['email'] for x in content])
recipients = {k: {'email': k} for k in get_sequence_recipients(SEQUENCE_ID)}
content = api_get('/polls/' + POLL_ID + '?limit=300')
keys = sorted(content['options'][0]['respondents'][0].keys())
fieldnames = keys + ['text']
for option in content['options']:
for resp in option['respondents']:
encoded_response = {k: to_string(v) for k, v in resp.items()}
encoded_response['text'] = to_string(option['text'])
if resp['email'] in recipients:
recipients[resp['email']].update(encoded_response)
else: # TODO: This shouldn't be possible and is broken due to MixMax API not paginating or ?limiting properly.
recipients[resp['email']] = encoded_response
sio = StringIO.StringIO()
writer = csv.DictWriter(sio, fieldnames=fieldnames)
writer.writeheader()
for row in recipients.values():
writer.writerow(row)
print sio.getvalue()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment