-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailchimp-restore.py
executable file
·113 lines (98 loc) · 3.17 KB
/
mailchimp-restore.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
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
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python
"""MailChimp list restore script."""
import argparse
import csv
import json
import os
import re
import requests
from mailchimp3 import MailChimp
from mailchimp3.helpers import get_subscriber_hash
def _client(key):
"""Return MailChimp API client object."""
headers = requests.utils.default_headers()
headers['User-Agent'] = (
'Mailchimp Backup script '
'(https://github.com/max-arnold/mailchimp-backup)'
)
return MailChimp(mc_api=key)
def get_lists(key):
"""Return lists info."""
return _client(key).lists.all(get_all=True)
def show_lists(key):
"""Display lists info."""
lists = get_lists(key)
for lst in lists['lists']:
print(
'ID: {}, Name: "{}", Members: {}'.format(
lst['id'], lst['name'], lst['stats']['member_count']
)
)
def restore(key, options):
with open(options._in, 'r') as fp:
cr = csv.DictReader(fp)
subscribers = []
for row in cr:
row['merge_fields'] = {}
row['location'] = {}
for k, v in list(row.items()):
if k.startswith('merge_fields.'):
row.pop(k)
row['merge_fields'][k[len('merge_fields.'):]] = v
if k.startswith('location.'):
row.pop(k)
row['location'][k[len('location.'):]] = v
row['tags'] = json.loads(row['tags'])
row['vip'] = row['vip'] == 'True'
row['timestamp_signup'] = re.sub('\+00:00$', "Z", row['timestamp_signup'])
if 'timestamp_opt' not in row:
row['timestamp_opt'] = row['timestamp_signup']
if 'ip_opt' not in row:
row['ip_opt'] = row['ip_signup']
subscribers.append(row)
client = _client(key)
for sub in subscribers:
sub['status_if_new'] = sub['status']
client.lists.members.create_or_update(
options.list,
get_subscriber_hash(sub['email_address']),
sub
)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='MailChimp list restore script'
)
parser.add_argument('--key', type=str, help='API key')
parser.add_argument(
'--show-lists', action='store_true', help='Show available lists'
)
parser.add_argument('--list', type=str, help='List ID')
parser.add_argument('--in', dest='_in', type=str, help='Input file')
options = parser.parse_args()
key = options.key or os.environ.get('MAILCHIMP_KEY')
if key is None:
parser.exit(
status=1,
message=(
'Please specify either the MAILCHIMP_KEY '
'environment variable or the --key argument\n'
),
)
if options.show_lists:
show_lists(key)
parser.exit()
if not options._in:
parser.exit(
status=1,
message=(
'Please specify input file\n'
),
)
if not options.list:
parser.exit(
status=1,
message=(
'Please specify list id\n'
),
)
restore(key, options)