Skip to content

Instantly share code, notes, and snippets.

@ejucovy
Last active December 9, 2015 14:20
Show Gist options
  • Save ejucovy/6cc1c16a4bff62244647 to your computer and use it in GitHub Desktop.
Save ejucovy/6cc1c16a4bff62244647 to your computer and use it in GitHub Desktop.
nationbuilder developer exercises
import requests
import json
import os
ACCESS_TOKEN = os.environ['NB_ACCESS_TOKEN']
SLUG = os.environ['NB_SITE_PREFIX']
data = {
"survey": {
"slug": "survey",
"name": "Survey",
"status": "published",
"tags": ["funny"],
"questions": [{
"prompt": "What issue is more important to you?",
"external_id": None,
"slug": "important_issue",
"type": "multiple",
"status": "published",
"choices": [{
"name": "Daily mail service to all homes. Yeah right....",
"feedback": "really now...?"
}, {
"name": "Opposition to any change in our naturalization laws",
"feedback": None
}, {
"name": "foobar",
"feedback": "foobar"
}, {
"name": "Kansas should be admitted as a state",
"feedback": None
}]
}]
}
}
resp = requests.get(
"https://%s.nationbuilder.com/api/v1/sites?access_token=%s" % (
SLUG, ACCESS_TOKEN),
headers={"Content-type": "application/json", "Accept": "application/json"}
)
slug = resp.json()['results'][0]['slug']
resp = requests.post(
"https://%s.nationbuilder.com/api/v1/sites/%s/pages/surveys?access_token=%s" % (SLUG, slug, ACCESS_TOKEN),
headers={"Content-type": "application/json", "Accept": "application/json"},
data=json.dumps(data))
assert resp.status_code == 200, resp.text
survey_id = resp.json()['survey']['id']
print "Survey id: %s" % survey_id
import requests
import json
import os
ACCESS_TOKEN = os.environ['NB_ACCESS_TOKEN']
SLUG = os.environ['NB_SITE_PREFIX']
data = {
"email": "person@example.com",
"first_name": "Joe",
"last_name": "Person",
"phone": "1112223456",
}
resp = requests.put(
"https://%s.nationbuilder.com/api/v1/people/push?access_token=%s" % (
SLUG, ACCESS_TOKEN),
headers={"Content-type": "application/json", "Accept": "application/json"},
data=json.dumps({"person": data})
)
assert resp.status_code == 200, resp.text
addr = {"address1": "123 Example Street",
"city": "Omaha",
"state": "NE",
"country_code": "US"}
data = {"email": data['email'],
"home_address": addr,
"tags": ["tag1", "tag2"]}
resp = requests.put(
"https://%s.nationbuilder.com/api/v1/people/push?access_token=%s" % (
SLUG, ACCESS_TOKEN),
headers={"Content-type": "application/json", "Accept": "application/json"},
data=json.dumps({"person": data})
)
assert resp.status_code == 200, resp.text
person_id = resp.json()['person']['id']
url = "https://%s.nationbuilder.com/api/v1/people/%s?access_token=%s" % (
SLUG, person_id, ACCESS_TOKEN)
resp = requests.delete(url, headers={"Accept": "application/json"})
assert resp.status_code == 204, resp.text
import requests
import json
import os
import sys
import time
from dateutil.parser import parse
ACCESS_TOKEN = os.environ['NB_ACCESS_TOKEN']
SLUG = os.environ['NB_SITE_PREFIX']
data = {"survey_id": sys.argv[1], "limit": 100}
contact_type = sys.argv[2]
contact_method = sys.argv[3]
contact_sender_id = sys.argv[4]
try:
min_id = int(sys.argv[5]) + 1
except Exception:
min_id = 1
resp = requests.get(
"https://%s.nationbuilder.com/api/v1/survey_responses?access_token=%s" % (
SLUG, ACCESS_TOKEN),
headers={"Content-type": "application/json", "Accept": "application/json"},
data=json.dumps(data)
).json()
for submission in resp['results']:
if submission['id'] < min_id:
break
person_id = submission['person_id']
data = {"contact": {"type_id": contact_type, "method": contact_method, "sender_id": int(contact_sender_id)}}
requests.post("https://%s.nationbuilder.com/api/v1/people/%s/contacts?access_token=%s" % (
SLUG, person_id, ACCESS_TOKEN),
headers={"Content-type": "application/json", "Accept": "application/json"},
data=json.dumps(data)
).json()
if len(resp['results']):
print resp['results'][0]['id']
python-dateutil
requests
@ejucovy
Copy link
Author

ejucovy commented Dec 9, 2015

Usage notes:

  • pip install requirements.txt before running either
  • Person exercise creates, updates, and deletes a person all in one script. Run with python person.py MY_NB_SLUG MY_NB_ACCESS_TOKEN
  • Survey exercise is in two parts. First run python create_survey.py SLUG TOKEN to create a sample survey. Output will be a survey_id. Note that and then use when running the second script.
  • Second script can be run with python poll_survey.py SLUG TOKEN survey_id contact_type_id contact_method_name contact_sender_person_id. The contact type, contact method, and contact sender are assumed to be configured already on the nationbuilder site.
  • When it finishes, the script will output the ID of the most recent survey response to have been processed into a contact. If you subsequently re-run the script with that ID as an additional command-line argument (``python poll_survey.py SLUG TOKEN survey_id contact_type_id contact_method_name contact_sender_person_id last_processed_response_id`) it will skip creating duplicate contacts for responses that have already been processed.
  • The intent here is to wire up this script to a cronjob that pipes the script's output to a text file and reads its last argument from that text file, e.g. python poll_survey.py SLUG TOKEN survey_id contact_type_id contact_method_name contact_sender_person_id $(cat /tmp/poll_survey_status.txt) > /tmp/poll_survey_status.txt
  • In a production setting I would adjust the poll_survey code to follow pagination links if the last processed ID isn't on page 1 of the results (i.e. if more than 100 new responses came in after the last time the cronjob ran)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment