Skip to content

Instantly share code, notes, and snippets.

@anemitz
Created August 4, 2013 17:31
Show Gist options
  • Save anemitz/6151094 to your computer and use it in GitHub Desktop.
Save anemitz/6151094 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import csv
import argparse
from flask_common.utils import CsvWriter
from closeio_api_client.api import CloseIO_API, APIError
HEADERS = ['display_name', 'addresses']
parser = argparse.ArgumentParser(description='Export Leads to CSV')
parser.add_argument('--api_key', '-k', required=True, help='API Key')
parser.add_argument('--output', '-o', required=True, help='Output Filename')
parser.add_argument('--query', '-q', required=True, help='Search Query')
args = parser.parse_args()
with open(args.output, 'wb') as f:
writer = CsvWriter(f)
api = CloseIO_API(args.api_key)
writer.writerow(HEADERS)
skip = 0
has_more = True
while has_more:
resp = api.get('lead', data={'query': args.query, '_skip': skip})
leads = resp['data']
for lead in leads:
row = []
for header in HEADERS:
row.append(lead.get(header) or '')
writer.writerow(row)
skip += len(leads)
has_more = resp['has_more']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment