Feedparser to Parse Specific Text in Entries

711 views Asked by At

UPDATE: After trying the below answer, I was able to achieve partially of what I intend to achieve. I was not able to get the program to loop and constantly look for new mails. Here is the code snippet.

import imaplib
import email
import RPi.GPIO as GPIO, time


MAIL_CHECK_FREQ = 2      # check mail every X seconds

#Setting up pin modes
GPIO.setmode(GPIO.BCM)
GREEN_LED = 18
RED_LED = 23
GPIO.setup(GREEN_LED, GPIO.OUT)
GPIO.setup(RED_LED, GPIO.OUT)


#Mail to alert & credentials
GREETINGS = "Merry Christmas"
USERNAME = "username"
PASSWORD = "password"


# Connect
imapClient = imaplib.IMAP4_SSL("imap.gmail.com")
# Login
imapClient.login(USERNAME, PASSWORD)
# Choose folder inbox
imapClient.select("INBOX")

while True:
    # Fetch unseen messages
    _, message_ids = imapClient.search(None, "UNSEEN")
    for msg_id in message_ids[0].split():
        # Download the message
        _, data = imapClient.fetch(msg_id, "(RFC822)")
        # Parse data using email module
        msg = email.message_from_string(data[0][1])

        if msg["subject"] == GREETINGS:
            # do something...
            print "Working"
        else:
            print "Not working"
        # do something else...

    time.sleep(2)

ORIGINAL QUESTION:

I'm trying to use a python script file to parse new mails from gmail. How can I trigger an event only when an email with certain subject gets delivered in the inbox?

To be more clear, I want to trigger an LED to blink with Raspberry Pi when I receive an email with Subject "Some Text Here". This will be in a loop so that each time I receive an email with the same subject, the LED should blink for say X seconds to turn OFF.

Here is the script I've been trying and it always shows "Not Working"

#!/usr/bin/env python

import RPi.GPIO as GPIO, feedparser, time
from BrickPi import *

BrickPiSetup() #setup the serial port for cummunication

BrickPi.MotorEnable[PORT_A] = 1 # Enable the Motor A
BrickPi.MotorEnable[PORT_B] = 1 #Enable the Motor B

BrickPiSetupSensors() #Send the properties of sensors to BrickPi

DEBUG = 1

USERNAME = "username"     # just the part before the @ sign, add yours here
PASSWORD = "password"     

#GREETINGS = "Merry Christmas"

NEWMAIL_OFFSET = 0        # my unread messages never goes to zero, yours might
MAIL_CHECK_FREQ = 2      # check mail every 60 seconds

GPIO.setmode(GPIO.BCM)
GREEN_LED = 18
RED_LED = 23
GPIO.setup(GREEN_LED, GPIO.OUT)
GPIO.setup(RED_LED, GPIO.OUT)



try:
    while True:
        GREETINGS = 'Merry Christmas'
        newmails = feedparser.parse('https://" + USERNAME + ":" + PASSWORD +"@mail.google.com/gmail/feed/atom/inbox')
        entry = newmails.entries

        if newmails.entries == GREETINGS:
            print "Working"
        else:
            print "Not Working"
        time.sleep(MAIL_CHECK_FREQ)

except KeyboardInterrupt:
      GPIO.cleanup()
1

There are 1 answers

5
Victor On BEST ANSWER

Never used feed parser myself, but here is an alternative way. I suggest you take a look at the imaplib - it provides tools for accessing email accounts and retrieving emails. Also take a look at the email module - you can use it to parse an email and get subject, sender, etc. Take a look at this example:

import imaplib
import email
import time

GREETINGS = "Merry Christmas"

while True:
    # Connect
    imapClient = imaplib.IMAP4_SSL("imap.gmail.com")
    # Login
    imapClient.login(USERNAME, PASSWORD)
    # Choose folder inbox
    imapClient.select("INBOX")

    # Fetch unseen messages
    x, message_ids = imapClient.search(None, "UNSEEN")
    for msg_id in message_ids[0].split():
        # Download the message
        _, data = imapClient.fetch(msg_id, "(RFC822)")
        # Parse data using email module
        msg = email.message_from_string(data[0][1])

        if msg["subject"] == GREETINGS:
            # do something...
            print "Working"
        else:
            print "Not working"
        # do something else...

    # Log out
    imapClient.close()
    imapClient.logout()

    time.sleep(2)

HTH.