Query builder

This commit is contained in:
Martin Rusev
2013-07-31 13:34:39 +03:00
parent 4d1f736948
commit e7901308ae
4 changed files with 31 additions and 13 deletions

View File

@@ -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:
........ ........

View File

@@ -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
View 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)

View File

@@ -1,7 +1,7 @@
from setuptools import setup from setuptools import setup
import os import os
version = '0.5' version = '0.5.1'
def read(filename): def read(filename):