Skip to content

Instantly share code, notes, and snippets.

@rlr
Created March 8, 2013 16:59
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 rlr/5117972 to your computer and use it in GitHub Desktop.
Save rlr/5117972 to your computer and use it in GitHub Desktop.
Grab emails from SUMO's email collection survey and add them to the Exit Survey email campaign.
#!/usr/bin/env python
import json
import optparse
import sys
from datetime import datetime
try:
import requests
except ImportError:
print 'You need to install requests. Do:'
print ''
print ' pip install requests'
sys.exit()
USAGE = 'usage: %prog [STARTDATE (YYYY-MM-DD)] [ENDDATE (YYYY-MM-DD)] [USERNAME] [PASSWORD]'
EMAIL_COLLECTION_SURVEY_ID = 1002970
EXIT_SURVEY_ID = 1002970
EXIT_SURVEY_CAMPAIGN_ID = 639640
def get_email_addresses(startdate, enddate, user, password):
"""Get the email addresses collected between startdate and enddate."""
emails = []
page = 1
more_pages = True
while more_pages:
response = requests.get(
'https://restapi.surveygizmo.com/v2/survey/{survey}'
'/surveyresponse?'
'filter[field][0]=datesubmitted'
'&filter[operator][0]=>=&filter[value][0]={start}+0:0:0'
'&filter[operator][1]=<&filter[value][1]={end}+0:0:0'
'&filter[field][1]=status&filter[operator][1]=='
'&filter[value][1]=Complete'
'&resultsperpage=500'
'&page={page}'
'&user:pass={user}:{password}'.format(
survey=EMAIL_COLLECTION_SURVEY_ID, start=startdate,
end=enddate, page=page, user=user, password=password))
results = json.loads(response.content)
total_pages = results['total_pages']
more_pages = page < total_pages
emails = emails + [r['[question(13)]'] for r in results['data']]
return emails
def add_email_to_campaign(email, user, password):
response = requests.put(
'https://restapi.surveygizmo.com/v2/survey/{survey}'
'/surveycampaign/{campaign}/contact?'
'semailaddress={email}'
'&user:pass={user}:{password}'.format(
survey=EXIT_SURVEY_ID, campaign=EXIT_SURVEY_CAMPAIGN_ID,
email=email, user=user, password=password))
if response.status_code == 200:
print 'Added: %s' % email
else:
print 'Error adding: %s' % email
if __name__ == '__main__':
parser = optparse.OptionParser(usage=USAGE)
(options, args) = parser.parse_args()
if not args or len(args) != 4:
parser.print_help()
sys.exit(1)
startdate = datetime.strptime(args[0], '%Y-%m-%d').date()
enddate = datetime.strptime(args[1], '%Y-%m-%d').date()
user = args[2]
password = args[3]
emails = get_email_addresses(
startdate, enddate, user, password)
print '%s emails collected...' % len(emails)
for email in emails:
add_email_to_campaign(email, user, password)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment