-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe_members_and_prospects.py
72 lines (57 loc) · 2.27 KB
/
subscribe_members_and_prospects.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
import json
from aweber_api import AWeberAPI, APIException
consumer_key = os.environ['AWEBER_CONSUMER_KEY']
consumer_secret = os.environ['AWEBER_CONSUMER_SECRET']
# create new instance of AWeberAPI
application = AWeberAPI(consumer_key, consumer_secret)
# get a request token
request_token, token_secret = application.get_request_token('oob')
# pylint: disable=E1601
print 'Go to this url in your browser: %s' % application.authorize_url
# prompt for verifier code
code = raw_input('Type code here: ')
# exchange request token + verifier code for access token
application.user.request_token = request_token
application.user.token_secret = token_secret
application.user.verifier = code
access_key, access_secret = application.get_access_token()
members_list_id = os.environ['MEMBERS_LIST_ID']
prospects_list_id = os.environ['PROSPECTS_LIST_ID']
data = json.load(open('members-and-prospects.json'))
aweber = AWeberAPI(consumer_key, consumer_secret)
account = aweber.get_account(access_key, access_secret)
for member in data['members']:
try:
print 'Member: %s' % member['personal']['email']
list_url = '/accounts/%s/lists/%s' % (account.id, members_list_id)
list_ = account.load_from_url(list_url)
# create a subscriber
params = {
'email': member['personal']['email'],
}
subscribers = list_.subscribers
new_subscriber = subscribers.create(**params)
# success!
# pylint: disable=E1601
print "A new subscriber was added to the %s list!" % (list_.name)
except (KeyError, APIException), exc:
print 'Error: %s' % str(exc)
continue
for prospect in data['prospects']:
try:
print 'Prospect: %s' % prospect['personal']['email']
list_url = '/accounts/%s/lists/%s' % (account.id, prospects_list_id)
list_ = account.load_from_url(list_url)
# create a subscriber
params = {
'email': prospect['personal']['email'],
}
subscribers = list_.subscribers
new_subscriber = subscribers.create(**params)
# success!
# pylint: disable=E1601
print "A new subscriber was added to the %s list!" % (list_.name)
except (KeyError, APIException), exc:
print 'Error: %s' % str(exc)
continue