Query builder
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
Imbox - Python IMAP for Humans
|
Imbox - Python IMAP for Humans
|
||||||
=======
|
=======
|
||||||
|
|
||||||
Python library for reading IMAP mailboxes and converting the email content to human readable data
|
Python library for reading IMAP mailboxes and converting email content to machine readable data
|
||||||
|
|
||||||
Installation
|
Installation
|
||||||
============
|
============
|
||||||
@@ -32,6 +32,12 @@ all_messages = imbox.messages()
|
|||||||
# Unread messages
|
# Unread messages
|
||||||
unread_messages = imbox.messages(unread=True)
|
unread_messages = imbox.messages(unread=True)
|
||||||
|
|
||||||
|
# Messages sent FROM
|
||||||
|
messages_from = imbox.mesages(sent_from='martin@amon.cx')
|
||||||
|
|
||||||
|
# Messages sent TO
|
||||||
|
messages_from = imbox.mesages(sent_to='martin@amon.cx')
|
||||||
|
|
||||||
|
|
||||||
for message in all_messages:
|
for message in all_messages:
|
||||||
........
|
........
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from imbox.imap import ImapTransport
|
from imbox.imap import ImapTransport
|
||||||
from imbox.parser import parse_email
|
from imbox.parser import parse_email
|
||||||
|
from imbox.query import build_search_query
|
||||||
|
|
||||||
class Imbox(object):
|
class Imbox(object):
|
||||||
|
|
||||||
@@ -9,7 +10,7 @@ class Imbox(object):
|
|||||||
self.connection = server.connect(username, password)
|
self.connection = server.connect(username, password)
|
||||||
|
|
||||||
def fetch_by_uid(self, uid):
|
def fetch_by_uid(self, uid):
|
||||||
message, data = self.connection.uid('fetch', uid, '(BODY.PEEK[])') # Don't mark the messages as read
|
message, data = self.connection.uid('fetch', uid, '(BODY.PEEK[])') # Don't mark the messages as read, save bandwidth with PEEK
|
||||||
raw_email = data[0][1]
|
raw_email = data[0][1]
|
||||||
|
|
||||||
email_object = parse_email(raw_email)
|
email_object = parse_email(raw_email)
|
||||||
@@ -24,16 +25,7 @@ class Imbox(object):
|
|||||||
|
|
||||||
def messages(self, *args, **kwargs):
|
def messages(self, *args, **kwargs):
|
||||||
|
|
||||||
query = "ALL"
|
query = build_search_query(**kwargs)
|
||||||
|
|
||||||
# Parse keyword arguments
|
|
||||||
unread = kwargs.get('unread', False)
|
|
||||||
folder = kwargs.get('folder', False)
|
|
||||||
sent_from = kwargs.get('sent_from', False)
|
|
||||||
sent_to = kwargs.get('sent_to', False)
|
|
||||||
|
|
||||||
if unread != False:
|
|
||||||
query = "UNSEEN"
|
|
||||||
|
|
||||||
message, data = self.connection.uid('search', None, query)
|
message, data = self.connection.uid('search', None, query)
|
||||||
|
|
||||||
|
|||||||
20
imbox/query.py
Normal file
20
imbox/query.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
def build_search_query(**kwargs):
|
||||||
|
|
||||||
|
# Parse keyword arguments
|
||||||
|
unread = kwargs.get('unread', False)
|
||||||
|
sent_from = kwargs.get('sent_from', False)
|
||||||
|
sent_to = kwargs.get('sent_to', False)
|
||||||
|
|
||||||
|
query = "(ALL)"
|
||||||
|
|
||||||
|
if unread != False:
|
||||||
|
query = "(UNSEEN)"
|
||||||
|
|
||||||
|
if sent_from:
|
||||||
|
query = '{0} (FROM "{1}")'.format(query, sent_from)
|
||||||
|
|
||||||
|
if sent_to:
|
||||||
|
query = '{0} (TO "{1}")'.format(query, sent_to)
|
||||||
|
|
||||||
|
|
||||||
|
return str(query)
|
||||||
Reference in New Issue
Block a user