forked from curioe/snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
receive_email.py
42 lines (30 loc) · 1.4 KB
/
receive_email.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import datetime
import email
import logging
import re
from google.appengine.ext import webapp
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
from google.appengine.ext.webapp import util
from dateutil import date_for_new_snippet
from model import user_from_email, create_or_replace_snippet
class ReceiveEmail(InboundMailHandler):
"""Receive a snippet email and create or replace snippet for this week."""
def receive(self, message):
user = user_from_email(email.utils.parseaddr(message.sender)[1])
for content_type, body in message.bodies('text/plain'):
# http://stackoverflow.com/questions/4021392/how-do-you-decode-a-binary-encoded-mail-message-in-python
if body.encoding == '8bit':
body.encoding = '7bit'
content = body.decode()
sig_pattern = re.compile(r'^(\-\-|\=\=)\s*$', re.MULTILINE)
split_email = re.split(sig_pattern, content)
content = split_email[0]
reply_pattern = re.compile(r'^(On.*at|2\d{3}).*snippets', re.MULTILINE)
split_email = re.split(reply_pattern, content)
content = split_email[0]
create_or_replace_snippet(user, content, date_for_new_snippet())
def main():
application = webapp.WSGIApplication([ReceiveEmail.mapping()], debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()