Last active
December 9, 2015 14:20
-
-
Save ejucovy/6cc1c16a4bff62244647 to your computer and use it in GitHub Desktop.
nationbuilder developer exercises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import os | |
ACCESS_TOKEN = os.environ['NB_ACCESS_TOKEN'] | |
SLUG = os.environ['NB_SITE_PREFIX'] | |
data = { | |
"email": "[email protected]", | |
"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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python-dateutil | |
requests |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage notes:
python person.py MY_NB_SLUG MY_NB_ACCESS_TOKEN
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.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.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