One of the LCDproc clients running on my wifi radio is a python script which grabs the atom feed from Gmail to find out if there are any unread messages in my inbox. LCDproc is a project that produces server software that drives those small lcd displays on a Linux/BSD machine. The Gmail notifier script is inspired by two different sources:
http://www.stefanomanni.it/arduino/2009/12/07/ardugmailnotifier-an-arduino-based-gmail-notifier/
Which depends on python-feedparser.
and also:
http://mydogsbreakfast.blogspot.com/2010/01/lcdprocpy.html
Which depends on Jingleman’s Python OOP Wrapper Library for LCDproc Telnet API (lcdproc0.02).
Here is the result: lcdgmail.py.tar.gz
* Sept. 20, 2010 – added some exception handling to toughen the script up a bit. I found that feedparser doesn’t like to read things sometimes.
* Sept. 21, 2010 – modified the script to read feed once per iteration of the main loop.
* Sept. 29, 2010 – added simple GNU all-permissive license to code.
#!/usr/bin/env python
# This is a fusion of two different scripts:
#
# 1) http://mydogsbreakfast.blogspot.com/2010/01/lcdprocpy.html
#
# requires lcdproc0.2 from http://pypi.python.org/pypi/lcdproc
# Also see http://github.com/jingleman/lcdproc/
# Based on http://github.com/jingleman/lcdproc/blob/master/examples.py
# sudo apt-get install python-setuptools
# sudo easy_install lcdproc
# 2) Python script for ArduGmail Notifier
#
# AUTHOR: Stefano Manni | http://www.stefanomanni.it/arduino | ::FREE SOFTWARE::
# ATTENTION: First install python-feedparser available in Ubuntu Repository or at http://www.feedparser.org/
# sudo apt-get install python-feedparser
# Checks the Gmail atom feed to see how many messages in inbox and displays a bit of info about
# the first few in the queue and sends the the info out as a lcdproc client (16x2 LCD)
# Copyright 2010, Peter Harding, seephar.com
# revised 20100920 - added exception handling
# revised 20100921 - modified to read feed once per iteration
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved. This file is offered as-is,
# without any warranty.
import time, commands, string, sys, feedparser, time
from time import strftime
from lcdproc.server import Server
USERNAME="gmail username" # Change to match your username!
PASSWORD="gmail password" # Change to match your password!
PROTO="https://"
SERVER="mail.google.com/gmail/feed/atom"
def main():
try:
lcd = Server("127.0.0.1", debug=False)
lcd.start_session()
except:
print "Unexpected error:", sys.exc_info()[0]
raise SystemExit("LCD didn't start, no need to continue")
screen1 = lcd.add_screen("Screen1")
screen1.set_heartbeat("off")
screen1.set_duration(12)
screen2 = lcd.add_screen("Screen2")
screen2.set_heartbeat("off")
screen2.set_duration(12)
ddow = strftime("%A")
ddate = strftime("%d %b %Y")
display_sc1_ln1 = screen1.add_string_widget("sc1_ln1", text= "Wait 90 Secs...", x=1, y=1)
display_sc1_ln2 = screen1.add_scroller_widget("sc1_ln2", text="for Network", left=1, top=2, right=16, bottom=2, speed=3)
display_sc2_ln1 = screen2.add_scroller_widget("sc2_ln1", text=ddow, left=1, top=1, right=16, bottom=1, speed=3)
display_sc2_ln2 = screen2.add_scroller_widget("sc2_ln2", text=ddate, left=1, top=2, right=16, bottom=2, speed=3)
# wait 90 seconds for networking to be fully active on startup otherwise script will break
time.sleep(90)
while True:
try:
gfeed = feedparser.parse(PROTO + USERNAME + ":" + PASSWORD + "@" + SERVER)
except:
gcounter = 0
display_sc1_ln1.set_text("Gmail: ?")
display_sc1_ln2.set_text("Feed not read!")
ddow = strftime("%A")
ddate = strftime("%d %b %Y")
display_sc2_ln1.set_text(ddow)
display_sc2_ln2.set_text(ddate)
else:
gcounter = int(gfeed.feed.fullcount)
if gcounter == 0:
display_sc1_ln1.set_text("Gmail: 0")
display_sc1_ln2.set_text("No mail!")
ddow = strftime("%A")
ddate = strftime("%d %b %Y")
display_sc2_ln1.set_text(ddow)
display_sc2_ln2.set_text(ddate)
if gcounter == 1:
display_sc1_ln1.set_text("Gmail: 1")
try:
gtitle = str(gfeed.entries[0].title)
except:
gtitle = "Couldn't read title"
if len(gtitle) > 25:
display_sc1_ln2.set_text(gtitle[0:24])
else:
display_sc1_ln2.set_text(gtitle)
try:
gsummary = str(gfeed.entries[0].summary)
except:
gsummary = "Couldn't read summary"
if len(gsummary) > 25:
display_sc2_ln1.set_text(gsummary[0:24])
else:
display_sc2_ln1.set_text(gsummary)
try:
gauthor = str(gfeed.entries[0].author)
except:
gauthor = "Couldn't read author"
if len(gtitle) > 25:
display_sc2_ln2.set_text(gauthor[0:24])
else:
display_sc2_ln2.set_text(gauthor)
if gcounter == 2:
display_sc1_ln1.set_text("Gmail: 2")
display_sc1_ln2.set_text("")
try:
gtitle = str(gfeed.entries[0].title)
except:
gtitle = "Couldn't read title"
if len(gtitle) > 25:
display_sc2_ln1.set_text(gtitle[0:24])
else:
display_sc2_ln1.set_text(gtitle)
try:
gtitle = str(gfeed.entries[1].title)
except:
gtitle = "Couldn't read title"
if len(gtitle) > 25:
display_sc2_ln2.set_text(gtitle[0:24])
else:
display_sc2_ln2.set_text(gtitle)
if gcounter > 2:
display_sc1_ln1.set_text("Gmail: " + str(gcounter))
try:
gtitle = str(gfeed.entries[0].title)
except:
gtitle = "Couldn't read title"
if len(gtitle) > 25:
display_sc1_ln2.set_text(gtitle[0:24])
else:
display_sc1_ln2.set_text(gtitle)
try:
gtitle = str(gfeed.entries[1].title)
except:
gtitle = "Couldn't read title"
if len(gtitle) > 25:
display_sc2_ln1.set_text(gtitle[0:24])
else:
display_sc2_ln1.set_text(gtitle)
try:
gtitle = str(gfeed.entries[2].title)
except:
gtitle = "Couldn't read title"
if len(gtitle) > 25:
display_sc2_ln2.set_text(gtitle[0:24])
else:
display_sc2_ln2.set_text(gtitle)
time.sleep(900) # check every 15 min should be often enough, wouldn't want to be a pest
if __name__ == "__main__":
main()
Download script here: lcdgmail.py.tar.gz