This repository was archived by the owner on Sep 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathlists.py
More file actions
54 lines (42 loc) · 1.46 KB
/
lists.py
File metadata and controls
54 lines (42 loc) · 1.46 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
""" This example demonstrates showing some basic details about your lists
Below you can see examples of pagination as well as partial response
"""
import requests
import json
try:
import urlparse
except ImportError:
from urllib import parse as urlparse
from config import MailChimpConfig
config = MailChimpConfig()
endpoint = urlparse.urljoin(config.api_root, 'lists')
params = {
# With Partial Response, you choose which fields you want to see
'fields': 'lists.id,lists.name,lists.stats.member_count',
# Pagination in API v3.0 is always done with count and offset
'count': 10, 'offset': 0
}
total_lists = 0
while True:
response = requests.get(endpoint, auth=('apikey', config.apikey),
params=params, verify=False)
try:
response.raise_for_status()
body = response.json()
except requests.exceptions.HTTPError as err:
print "Error: {} {}".format(str(response.status_code), err)
print json.dumps(response.json(), indent=4)
break
except ValueError:
print "Cannot decode json, got %s" % response.text
break
if len(body['lists']) == 0:
break
total_lists += len(body['lists'])
for user_list in body['lists']:
print u'%s: %s (Subscribers: %s)' % (
user_list['id'],
user_list['name'],
user_list['stats']['member_count'])
params['offset'] += params['count']
print "\n" + str(total_lists) + " lists found."