0

I have searched and searched but I'm not able to figure this out. Below is the JSON data I get back from the web service, API:

{
  "total_count": 673, 
  "items": [
    {
      "hap": "delivered", 
      "message": "Delivered: [email protected] \u2192 [email protected] 'Some Email subject'", 
      "type": "info", 
      "created_at": "Wed, 19 Aug 2015 18:38:54 GMT", 
      "message_id": "[email protected]"
    }, 
    {
      "hap": "accepted", 
      "message": "Accepted: [email protected] \u2192 [email protected] 'Subject of this email here'", 
      "type": "info", 
      "created_at": "Wed, 19 Aug 2015 18:38:53 GMT", 
      "message_id": "[email protected]"
    }, 
    {
      "hap": "delivered", 
      "message": "Delivered: [email protected] \u2192 [email protected] 'Subject Line here'", 
      "type": "info", 
      "created_at": "Wed, 19 Aug 2015 18:37:50 GMT", 
      "message_id": "[email protected]"
    },

The challenge is that I am trying to search the "message": block for the TO email address which comes after the "\u2192" inside the "message": location.

I have created this python script that dumps all entries inside "message": but I have not been able to filter this with a specific email address.

import requests, json
print("Connecting to the URL...")
r = requests.get("https://api:[email protected]/v3/example.com/log")
j = r.json()
for data in j['items']:
    print data['message']
5
  • Will there only be 1 "To" email address in the message or can there be multiple "To" email addresses in a message? Commented Aug 19, 2015 at 23:00
  • And you want to be able to look for a specific email address such as "[email protected]" and then only do something with the message from that specific email? Commented Aug 19, 2015 at 23:05
  • There will only be one TO email there. I need to show everything inside "message": that matches the email address searched for. There could be 1 or 50 results with that TO email address. Commented Aug 19, 2015 at 23:10
  • Ok, Do you know the email address you will be searching for to start off with or will you need to read some of the messages to find the address first? hope question that makes sense :) Commented Aug 19, 2015 at 23:16
  • I'll know what the email address I'll be searching for. This is the mailgun api that pulls logs to show if an email has been delivered for a given address. Commented Aug 19, 2015 at 23:17

3 Answers 3

1

Either of these "should" work.

Since you already know the email address, you only need to search the string for the exact email addresss. There are a couple of options here. You can use regular expressions (maybe overkill since this isnt a pattern but a known string). You can also just search the string for the known email address.

You determine if you should use the message based of its boolean value in both cases.

Option 1

Regular Expressions

https://docs.python.org/3.5/library/re.html#match-objects

import re

email_address = "[email protected]"

for data in j['items']:
    match = re.search(email_address, data['message'])
    if match:
        print data['message']

Option 2

Search the message for the email address string

email_address = "[email protected]"

for data in j['items']:
    if email_address in data['message']:
        print data['message']
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Dan! I used Option 2! I'm a network engineer by trade trying to get some daily log viewing off my plate and into a web page.
Oh great, glad that helped. Option 2 was for sure the more appropriate approach for your case now that I realize the details of the task you were doing. Thanks for coming over from the network side to contribute :)
0

Try:

re.findall("[^@ ]+@[^@]+\.[^@ ]+", data['message'].split("\u2192")[1])[0]

First I split data['message'] on two by the character \u2192 than take the second part. I try to find all emails in this second part, and choose only the first one because it is what you are looking for.

Comments

0

Use the json.loads to solve that.

>>> json.loads('"Delivered: [email protected] \u2192 [email protected]"')
'Delivered: [email protected][email protected]'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.