zstumgoren/django-sacredheartcs — Issue #1: Synchronize contacts
Author: zstumgoren
Created: 2012-02-03 14:37:02+00:00
State: open

## Overview
Figure out optimal strategy for synchronizing contacts between CC and backup db. Recommended approach appears to be bulk export activities:
* http://community.constantcontact.com/t5/Getting-Started-with-API-s/Question-about-best-practice-in-syncing-new-and-existing/m-p/28749#M1005

## Strategy
* Bulk export on a weekly/bi-weekly cron.
* Create bulk_export activity (see "Creating a Export Contacts Activity" in the [API docs](http://www.ctctlabs.com/index.php/blog/detail/ctct_web_services_python_client_library/)
* Check status for 201 (which denotes export succeeded)
* Once status is 201, call get_activity and extract file URI for the file
* Download file using requests with Basic Auth (CTCT API does not provide a call for downloading bulk exports)
* Archive contacts whose email is longer among Active contacts; create contact entries for new emails.

Alternate strategy: Create backup db using an initial bulk export, then use [API call for searching for contacts by last updated](http://community.constantcontact.com/t5/Documentation/Searching-for-Contacts-by-Last-Updated-Date-Synchronizing/ba-p/25053)

## Code
To download bulk export CSV, need to use requests with Basic Auth

```python
import requests
from requests.auth import HTTPBasicAuth
from sacredheartcs.api_key import API_KEY, USER, PASSWORD
cc_user = '%'.join((API_KEY, USER))
#TODO: Get activity record, which contains URI to bulk export CSV
file_name = activity['file_name']
url = url = 'https://api.constantcontact.com' + file_name
response = requests.get(url, auth=HTTPBasicAuth(cc_user, PASSWORD))
```