Created
August 4, 2013 17:31
-
-
Save anemitz/6151094 to your computer and use it in GitHub Desktop.
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
#!/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