-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·47 lines (40 loc) · 1.37 KB
/
main.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
import configparser
import xml.etree.ElementTree as ET
import urllib.request as request
from mailchimp3 import MailChimp
config = configparser.ConfigParser()
config.read('mailchimp.config')
mailchimp_api_key = config['mailchimp']['api_key']
mailchimp_api_user = config['mailchimp']['api_user']
mailchimp_list_id = config['mailchimp']['list_id']
su_api_key = config['su']['api_key']
su_url = 'https://www.warwicksu.com/membershipapi/listmembers/' + su_api_key + '/'
members_xml = request.urlopen(su_url).read()
root = ET.fromstring(members_xml)
members = {}
for member in root:
info = {}
for part in member:
info[part.tag] = part.text
members[info['UniqueID']] = info
mc = MailChimp(mc_api=mailchimp_api_key, mc_user=mailchimp_api_user)
list_members = mc.lists.members.all(mailchimp_list_id, get_all=True, fields='members.email_address,members.merge_fields')
for id, data in members.items():
found = False
for list_member in list_members['members']:
if list_member['email_address'].lower() == data['EmailAddress'].lower():
found = True
break
if not found:
print('Added the following to the mailing list', data, sep='\n')
try:
mc.lists.members.create(mailchimp_list_id, {
'email_address': data['EmailAddress'],
'status': 'subscribed',
'merge_fields': {
'FNAME': data['FirstName'],
'LNAME': data['LastName']
}
})
except Exception as e:
print(e)