0

I am trying to get all email address of subscribers from a specific list (which has a unique listid) in Mailchimp.

If I print body, the output is in json format as below.

I'm trying to convert the json to dictionary.

After it was converted to dictionary, I would like to get all the email_address.

After I get all the email address, I would like to encrypt it using md5.

However I run into error 'TypeError: expected string or buffer'.

I am really new to python, try to solve it but could not. Thanks for looking at my question.

/* My python code */

params = { 
   'apikey': 'xyz',
   'listId':  'abc' }

config = MailChimpConfig() 
endpoint = "https://us5.api.mailchimp.com/3.0/lists/'listId'/members?   
apikey='apikey'&status=subscribed"

while True: 
   response = requests.get(endpoint, auth=('apikey', config.apikey),
                           params=params, verify=False)
   try:
     response.raise_for_status() 

     body = response.json
     dict = json.loads(body) 
     print(dict.members[0].email_address)
     break
   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
   /* end of my python code */



/* If I print body, the output is in json format as below:*/

{
- members: [
  - {
       id: "",
        email_address: "x@hotmail.com",
        etc:""
    },
  - {
       id: "",
       email_address: "y@gmail.com",
       etc:""
    }

 /* end of json format */
user21
  • 1,261
  • 5
  • 20
  • 41
  • *Aside*: Try not to use the names of built-in functions as variables in your program. `dict` is the factory function for building dictionary objects. If you co-opt that name, you won't be able to call `dict()` subsequently. – Robᵩ Mar 30 '16 at 05:48
  • Also, are those `-` literally present in the JSON text? If so, I don't think that is valid JSON. – Robᵩ Mar 30 '16 at 05:50
  • If you use v3 of the API, the ID of the subscriber is the md5 has of the email address. – TooMuchPete Mar 30 '16 at 17:12
  • Thanks for the input. I have to rewrite the above code in aws lambda python. Do you have any knowledge of it? – user21 Mar 30 '16 at 17:56

1 Answers1

1

This isn't right:

 body = response.json
 dict = json.loads(body) 

response.json isn't a JSON object or an str, it is a function. When called, it returns a Python object that represents the data from the response's JSON.

Try this:

# UNTESTED

# Interpret the JSON string:
data = response.json()

# Print one of the email addresses:
print(data['members'][0]['email_address'])

# Print all of the email addresses
addresses = [x['email_address'] for x in data['members']]
print(addresses)

Once you have the list of addresses, you can print the MD5 digest of each address thusly:

# UNTESTED
for address in addresses:
    print(hashlib.md5(address.encode('utf-8')).hexdigest())

If you would rather print one MD5 which represents all of the addresses:

# UNTESTED
md5 = hashlib.md5()
for address in sorted(addresses):
    md5.update(address.encode('utf-8'))
print(md5.hexdigest())
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Thanks for your suggestion. I have edited it but after compile, it has TypeError: 'instancemethod' object has no attribute '__getitem__' – user21 Mar 30 '16 at 05:51
  • You seem to have missed my major point: you must put parentheses after the word "json": `data = response.json()` – Robᵩ Mar 30 '16 at 05:53
  • Hi Rob, thanks! I am so happy that the email address was finally printed out! Do you have any idea of how to convert it to md5 – user21 Mar 30 '16 at 05:57
  • Try this: http://stackoverflow.com/questions/5297448/how-to-get-md5-sum-of-a-string and this: https://docs.python.org/2/library/hashlib.html#module-hashlib – Robᵩ Mar 30 '16 at 06:01
  • Do you want the md5 of each address, or the md5 of all the addresses? – Robᵩ Mar 30 '16 at 06:02
  • is it possible I can do that in both method, md5 of each address, and all addresses – user21 Mar 30 '16 at 06:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107689/discussion-between-user21-and-rob). – user21 Mar 30 '16 at 06:22