-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathget-clicks
More file actions
executable file
·102 lines (83 loc) · 3.24 KB
/
Copy pathget-clicks
File metadata and controls
executable file
·102 lines (83 loc) · 3.24 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
from __future__ import print_function
import json
from requests_oauthlib import OAuth2Session
with open('credentials.json', 'rt') as f:
credentials = json.load(f)
TOKEN_URL = "https://auth.aweber.com/oauth2/token"
client_id = credentials['client_id']
client_secret = credentials['client_secret']
token = credentials['token']
extra = {
'client_id': client_id,
'client_secret': client_secret,
'token': token
}
def token_updater(token):
with open('credentials.json', 'wt') as creds_file:
json.dump(
{
'client_id': client_id,
'client_secret': client_secret,
'token': token
}, creds_file)
print('Token was refreshed.\n'
'Updated credentials.json with your new credentials')
session = OAuth2Session(client_id=credentials['client_id'],
token=credentials['token'],
auto_refresh_url=TOKEN_URL,
auto_refresh_kwargs=extra,
token_updater=token_updater)
def get_collection(url):
collection = []
while url:
response = session.get(url)
response.raise_for_status()
body = response.json()
collection.extend(body['entries'])
# if there is a next link, there are more pages to retrieve
next_link = body.get('next_collection_link')
url = next_link if next_link else None
return collection
def campaign_type_name(campaign_type):
if campaign_type == 'b':
return 'broadcast'
elif campaign_type == 'f':
return 'followup'
else:
return '"{}" type campaign'.format(campaign_type)
# get an account to search on
account_url = 'https://api.aweber.com/1.0/accounts'
accounts = get_collection(account_url)
account = accounts[0] # choose the first account
# get a list to find messages on
lists_url = account['lists_collection_link']
lists = get_collection(lists_url)
list_info = lists[0] # choose the first list
# get a followup or broadcast to get links for
campaigns_url = list_info['campaigns_collection_link']
campaigns = get_collection(campaigns_url)
campaign = campaigns[0] # choose the first campaign
ctype = campaign_type_name(campaign['campaign_type'])
print('Clicks for {} "{}" by link:'.format(ctype, campaign['subject']))
# cache subscriber email addresses to avoid looking them up repeatedly
subscriber_emails = {}
# get all the links included in the message
links_url = campaign['links_collection_link']
links = get_collection(links_url)
for link in links:
print(link['url'])
# get all the clicks for each link
clicks_url = link['clicks_collection_link']
clicks = get_collection(clicks_url)
for click in clicks:
# look up the email address of each subscriber who clicked
subscriber_url = click['subscriber_link']
email = subscriber_emails.get(subscriber_url)
if not email:
# first time looking up a subscriber: save the email for next time
subscriber_response = session.get(subscriber_url)
subscriber = subscriber_response.json()
email = subscriber['email']
subscriber_emails[subscriber_url] = email
print(' {}: {}'.format(click['event_time'], email))