Why is this better than just using Growl?
I can't speak for Growl on the Mac, but the Windows Gmail Growl notifier polls the atom feed. Google recommends only doing this every 10 minutes. This means you get notified within 5 minutes of new emails on average.
If you like getting notified faster, this is almost instant. It uses IDLE which means the IMAP server will notify the script instead of it having to poll. It's more like "push"
I hacked it together modifying some guy's python idle script.
It requires prowlpy.py: http://github.com/jacobb/prowlpy/tree/master
and imaplib2: http://www.janeelix.com/piers/python/imaplib2_pp.html
Oh, and prowlpy requires httplib2 which you can "python setup.py install". http://code.google.com/p/httplib2/
Feel free to improve it. I kept it running in a thread so it can be shut down. If you use the synchronous version of idle, it will block until activity on the account, which means you can't shut down the script cleanly. I don't think.
Also, you might want to change line 25 in prowlpy.py to just h = httplib2.Http() instead of using ".cache" because you'll get errors with longer descriptions. I haven't investigated why this is the case. It'll still work OK.
Here it is:
Code: Select all
from threading import *
prowl_apikey = "APIKEYHERE"
IMAP_USERNAME = "USERNAME"
IMAP_PASSWORD = "PASSWORD"
# This is the threading object that does all the waiting on
# the event
def __init__(self, conn):
self.thread = Thread(target=self.idle)
self.M = conn
self.event = Event()
self.last_notify = ""
# This is a neat trick to make thread end. Took me a
# while to figure that one out!
# Starting an unending loop here
# This is part of the trick to make the loop stop
# when the stop() command is given
self.needsync = False
# A callback method that gets called when a new
# email arrives. Very basic, but that's good.
if not self.event.isSet():
self.needsync = True
# Do the actual idle call. This returns immediately,
# since it's asynchronous.
# This waits until the event is set. The event is
# set by the callback, when the server 'answers'
# the idle call and the callback function gets
# Because the function sets the needsync variable,
# this helps escape the loop without doing
# anything if the stop() is called. Kinda neat
# The method that gets called when a new email arrives.
print "Got an event!"
retcode, messages = self.M.search(None, 'UNSEEN')
if len(messages) >= 1 and len(messages.split()) >= 1:
ret, msginfo = self.M.fetch(messages.split()[-1], '(BODY[HEADER.FIELDS (SUBJECT FROM)] BODY)')
if ret == 'OK':
header = msginfo.split('\r\n')
sender = header
subject = header[9:]
body = msginfo[:100]
notify = sender+subject+body
if notify != self.last_notify:
self.last_notify = notify
print "New email %s %s %s" % (sender,subject,body)
p = prowlpy.Prowl(prowl_apikey)
print 'Posted to Prowl'
print "Already notified"
idler = None
last_notify = ""
# loop forever, timing out faster than IMAP will time you out
# Set the following two lines to your creds and server
M = imaplib2.IMAP4_SSL("imap.gmail.com")
# defaults to inbox
# Start the Idler thread
idler = Idler(M)
idler.last_notify = last_notify
print "Connected. Waiting on IDLE."
# sleep 20 minutes. Idle can time out in 30 minutes.
# a hack to make sure you only get notified once per email
last_notify = idler.last_notify
idler = None
# Clean up.
if idler is not None:
print "Shutting down"