Skip to content

Instantly share code, notes, and snippets.

@sharoonthomas

sharoonthomas/README.md

Last active Sep 29, 2016
Embed
What would you like to do?
Get addresses with contact information over fulfil.io api

Set the environment variables:

export FULFIL_SUBDOMAIN=your_subdomain
export FULFIL_API_KEY=a-long-api-key-which-is-secret

Install the dependencies

$ pip install -r requirements.txt

Run the script

python get_addresses.py
import os
from fulfil_client import Client
from unicodecsv import DictWriter
client = Client(os.environ['FULFIL_SUBDOMAIN'], os.environ['FULFIL_API_KEY'])
Contact = client.model('party.party')
Address = client.model('party.address')
def get_addresses(fields, writer):
contact_count = Contact.search_count([])
address_count = Address.search_count([])
print "Total contacts in DB: %s" % contact_count
print "Total addresses in DB: %s" % address_count
addresses = []
batch_size = 1000
for offset in xrange(0, address_count, batch_size):
print "Fetching %s to %s" % (
offset + 1, offset + batch_size
)
addresses = Address.search_read(
[], offset, batch_size, None, fields
)
writer.writerows(addresses)
return addresses
if __name__ == '__main__':
fields = [
'id',
'party.name',
'name',
'street',
'streetbis',
'city',
'zip',
'country.code',
'subdivision.code',
'party.average_sale_order_value',
'party.total_sale_order_value',
'party.sale_order_count',
'party.sale_order_frequency',
]
with open('addresses1.csv', 'w') as csv_file:
writer = DictWriter(csv_file, fields)
writer.writeheader()
get_addresses(fields, writer)
fulfil_client
unicodecsv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.