Feedparser解析条目中的特定文本

时间:2014-11-24 07:05:41

标签: python email raspberry-pi feedparser

更新 在尝试下面的答案之后,我能够实现我想要实现的部分目标。我无法让程序循环并不断寻找新邮件。这是代码段。

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)

原始问题:

我正在尝试使用python脚本文件来解析来自gmail的新邮件。如何仅在收件箱中发送包含特定主题的电子邮件时触发事件?

更清楚的是,当我收到主题为“Some Text Here”的电子邮件时,我想触发一个LED与Raspberry Pi一起闪烁。这将是一个循环,以便每次我收到一个具有相同主题的电子邮件时,LED应闪烁,表示X秒关闭。

这是我一直在尝试的剧本,它总是显示“不工作”

#!/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 个答案:

答案 0 :(得分:0)

我自己从未使用过feed解析器,但这是另一种方法。我建议您查看imaplib - 它提供了访问电子邮件帐户和检索电子邮件的工具。另请查看email模块 - 您可以使用它来解析电子邮件并获取主题,发件人等。 看一下这个例子:

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。