Created
July 31, 2013 05:53
-
-
Save anemitz/6119641 to your computer and use it in GitHub Desktop.
Close.io Opportunities to CSV
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
#!/usr/bin/env python | |
import csv | |
import argparse | |
from flask_common.utils import CsvWriter | |
from closeio_api_client.api import CloseIO_API, APIError | |
HEADERS = ['date_created', 'lead_name', 'status', 'date_won', 'date_lost', 'confidence', 'user_name', 'note', 'value', 'value_period'] | |
parser = argparse.ArgumentParser(description='Export Opportunities to CSV') | |
parser.add_argument('--api_key', '-k', required=True, help='API Key') | |
parser.add_argument('--output', '-o', required=True, help='Output filename') | |
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('opportunity', data={'_skip': skip}) | |
opportunities = resp['data'] | |
for opportunity in opportunities: | |
row = [] | |
for header in HEADERS: | |
row.append(opportunity.get(header) or '') | |
writer.writerow(row) | |
skip += len(opportunities) | |
has_more = resp['has_more'] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment