Last active
August 29, 2015 14:24
-
-
Save Jamedjo/aac9a3ec993e97c65f34 to your computer and use it in GitHub Desktop.
Script to create sendwithus customers from csv with data
This file contains hidden or 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
def batch(iterable, n=1): | |
l = len(iterable) | |
for ndx in range(0, l, n): | |
yield iterable[ndx:min(ndx+n, l)] | |
import sendwithus | |
import csv | |
max_count = 100 # keep this as or around 100 for batch requests | |
csv_location = './customers.csv' | |
api_key = 'SWU_PRODUCTION_API_KEY' | |
data = {} | |
api = sendwithus.api(api_key=api_key) # | |
batch_request = api.start_batch() | |
users_list = [] | |
with open(csv_location, 'rU') as csvfile: | |
users = csv.DictReader(csvfile) | |
for user in users: | |
email = user['email'] | |
del user['email'] | |
users_list.append((email, user)) | |
print '%d customers in list' % len(users_list) | |
for index, user_batch in enumerate(batch(users_list, max_count)): | |
requests_list = [] | |
for user_email, user_data in user_batch: | |
print '%s user with data %s' % (user_email, user_data) | |
batch_request.customer_create(user_email, data=user_data) | |
print 'Starting batch %s!' % index | |
print batch_request.execute() |
This file contains hidden or 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
first_name | last_name | ||
---|---|---|---|
[email protected] | Jon | Smith | |
[email protected] | Test | Er | |
[email protected] | James | Jones |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yep, thought it best to leave that out when sharing!
Thanks again for the original