December 8, 2007

Gmail notify for dzen

16 June 2008 Update: I have updated the notifier - please read here for details


Notification of new mail in my gmail account is very important to me on my desktop and therefore that was the first thing I tried to solve once I set up xmonad and dzen. Fortunately, it wasn't that difficult. The following script checks the mail and formats the output for the latest development version of dzen2 (see previous post).
To use it, add your username and password and edit the other variables to change the color of the output, number of new mails to show and words to show in the subject for each mail. Note that I have opted to put the password in a hidden file with proper permissions and the script reads the file for the password.

You can read more about this and download the script from the dzen wiki here.




#!/usr/bin/env python

#========================================================================
# Copyright 2007 Raja <rajajs@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#==========================================================================

# ======================================================================
# Modified from code originally written by Baishampayan Ghose
# Copyright (C) 2006 Baishampayan Ghose <b.ghose@ubuntu.com>
# ======================================================================


import urllib
import feedparser

_url = "https://mail.google.com/gmail/feed/atom"

################## Edit here #######################

#pwd = xxxx # pwd stored in script
_pwdfile = '/path_to/hidden/pwd_file' # pwd stored in a file
_username = 'XXXX'
_calmcolor = 'white'
_alertcolor = 'red'
_maxmails = 5 # maximum new mails to show
_maxwords = 3 # maximum words to show in each mail header

###########################################################

class GmailRSSOpener(urllib.FancyURLopener):
'''Logs on with stored password and username
Password is stored in a hidden file in the home folder'''

def prompt_user_passwd(self, host, realm):
#uncomment line below if password directly entered in script.
pwd = open(_pwdfile).read()
return (_username, pwd)

def auth():
'''The method to do HTTPBasicAuthentication'''
opener = GmailRSSOpener()
f = opener.open(_url)
feed = f.read()
return feed

def showmail(feed):
'''Parse the Atom feed and print a summary'''
atom = feedparser.parse(feed)
newmails = len(atom.entries)
if newmails == 0:
title = "^fg(%s) You have no new mails" % (_calmcolor)
elif newmails == 1:
title = "^fg(%s) You have 1 new mail" % (_alertcolor)
else:
title = "^fg(%s) You have %s new mails" % (_alertcolor,newmails)

# print the title with formatting
print "^tw()" +title
#clear the slave window
print "^cs()"

#then print the messages
for i in range(min(_maxmails,newmails)):

emailtitle = atom.entries[i].title
# show only first few words if title is too long
if len(emailtitle.split()) > _maxwords:
emailtitle = ' '.join(emailtitle.split()[:_maxwords])

print "^fg(%s) %s from %s" % (_calmcolor, emailtitle, atom.entries[i].author)

if __name__ == "__main__":
feed = auth()
showmail(feed)




This bash script calls the dzenGmailNotify script at specified intervals.


#!/bin/bash

#checkgmail.sh

while true ; do
python dzenGmailNotify.py
sleep 30
done


Finally, make the above executable and add this to .xsession to display in dzen.


./checkgmail.sh | dzen2 -ta r -l 5 -bg '#808080' -x 700 -w 380 &