浏览代码

Adding library to parse email and gitignore

Joe Ceresini 7 年之前
父节点
当前提交
6c390f7a8a
共有 3 个文件被更改,包括 52 次插入0 次删除
  1. 1 0
      .gitignore
  2. 40 0
      mailparse/mailparse/__init__.py
  3. 11 0
      mailparse/setup.py

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+*.pyc

+ 40 - 0
mailparse/mailparse/__init__.py

@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+import mailbox
+from datetime import datetime
+import json
+import re
+
+# Helper to get plain-text payload from a given email
+def get_message_body(msg):
+	for part in msg.walk():
+		if part.get_content_type() == 'text/plain':
+			return part.get_payload(decode=True)
+	return None
+
+# Open a maildir inbox, and run provided parsers
+def parse_maildir_inbox(inbox,parsers):
+
+	mail = mailbox.Maildir(inbox,None)
+
+	messages = {}
+
+	for msgid in mail.keys():
+
+		subject = mail[msgid]['subject']
+		to_addr = mail[msgid]['to']
+		from_addr = mail[msgid]['from']
+		cur_time = datetime.now().strftime("%b %d %H:%M:%S")
+
+		for regex,action in parsers:
+
+			msg_details = {'subject': subject, 'to': to_addr, 'from': from_addr}
+
+			m = re.match(regex,subject)
+			if m:
+				parse_success = action(mail[msgid])
+
+				if parse_success:
+					print "%s: Parse successful: %s: %s" % (cur_time, action.__name__, json.dumps(msg_details))
+					mail.remove(msgid)
+			else:
+				print "%s: No parser match: %s" % (cur_time, json.dumps(msg_details))

+ 11 - 0
mailparse/setup.py

@@ -0,0 +1,11 @@
+from setuptools import setup
+
+setup(
+    name='mailparse',
+    packages=['mailparse'],
+    version='0.0.1',
+    description='Simple mail parser',
+    author='Joe Ceresini',
+    author_email='[email protected]',
+    install_requires=['mailbox'],
+)