使用 Google App Engine 从电子邮件收集数据

发布于 2024-11-06 09:00:29 字数 2136 浏览 0 评论 0原文

我对 Google App Engine(和 python)相当陌生,我正在实现一个系统,该系统基本上可以解析传入的电子邮件并将详细信息存储在数据存储中。现在我确实可以使用它,但我觉得应该有一个比我现在更好的方法。

基本上,进入系统的电子邮件总是如下所示:

Order Details: Random Batch Name here

Order Status: 74 of 131 Shipped In Total

Message ID: 123456

Message Date: 21/04/2011 16:13:00

Mobile Number: 01234567890

Message: message would be here

我用来解析它的代码如下所示:

class LogSenderHandler(InboundMailHandler):
    def receive(self, message):

        # Get the body text from the e-mail
        plaintext_bodies = message.bodies('text/plain')
        for content_type, body in plaintext_bodies:
            body_text = body.decode().split('\n')

            # Loop through each line in the e-mail and discard a line if it is blank
            for line in body_text:
                if line != "":

                    # I'm sure there's a better way of doing this, just don't know how right now!
                    # Split the current line based on the ": " value and only let it be done once
                    splitline = line.split(': ', 1)

                    # Check to see which line we now have the details for and place value into the correct variable
                    if splitline[0] == "Order Details":
                        batch = splitline[1]
                    if splitline[0] == "Message ID":
                        messageID = splitline[1]
                    if splitline[0] == "Message Date":
                        messageDate = splitline[1]
                    if splitline[0] == "Mobile Number":
                        mobileNumber = splitline[1]
                    if splitline[0] == "Message":
                        theMessage = splitline[1]


        newNumber = SMSNumber( status = "Waiting",
                               batch = common.slugify(batch),
                               messageID = messageID,
                               messageDate = messageDate,
                               sentMessage = theMessage )

        newNumber._key_name = mobileNumber
        newNumber.put()

有更好的方法来处理这个问题吗?如果有人有任何意见,我们将不胜感激! :)

问候

I'm reasonably new to Google App Engine (and python) I'm implementing a system that will basically parse an incoming e-mail and store the details in the Data Store. Now I do have it working but I feel like there should be a better way to do it that I currently am.

Basically, the e-mail that comes in to the system always looks like the following:

Order Details: Random Batch Name here

Order Status: 74 of 131 Shipped In Total

Message ID: 123456

Message Date: 21/04/2011 16:13:00

Mobile Number: 01234567890

Message: message would be here

The code I am using to parse it looks like this:

class LogSenderHandler(InboundMailHandler):
    def receive(self, message):

        # Get the body text from the e-mail
        plaintext_bodies = message.bodies('text/plain')
        for content_type, body in plaintext_bodies:
            body_text = body.decode().split('\n')

            # Loop through each line in the e-mail and discard a line if it is blank
            for line in body_text:
                if line != "":

                    # I'm sure there's a better way of doing this, just don't know how right now!
                    # Split the current line based on the ": " value and only let it be done once
                    splitline = line.split(': ', 1)

                    # Check to see which line we now have the details for and place value into the correct variable
                    if splitline[0] == "Order Details":
                        batch = splitline[1]
                    if splitline[0] == "Message ID":
                        messageID = splitline[1]
                    if splitline[0] == "Message Date":
                        messageDate = splitline[1]
                    if splitline[0] == "Mobile Number":
                        mobileNumber = splitline[1]
                    if splitline[0] == "Message":
                        theMessage = splitline[1]


        newNumber = SMSNumber( status = "Waiting",
                               batch = common.slugify(batch),
                               messageID = messageID,
                               messageDate = messageDate,
                               sentMessage = theMessage )

        newNumber._key_name = mobileNumber
        newNumber.put()

Is there a better way to handle this? If anyone has any input it would be gratefully received! :)

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隐诗 2024-11-13 09:00:29

一种更简洁的方法是迭代文件的行,在冒号上分割,并将行添加到 Map 中。然后,从映射中读出相关条目并将它们传递给构造函数 - 指定默认值,以防它不存在。

A neater approach would be to iterate through the lines of the file, splitting on the colon, and adding the lines to a Map. Then, read out the relevant entries from the map and pass them to the constructor - specifying a default in case it wasn't present.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文