The Free Geek

Ramblings of a Free Software Geek

Check GMail the Python way Wed, 04 Feb 2009 21:05:21 +0000

Filed under: Google,Programming,Python — Bon Goose @ 21:05:21

Swaroop posted a nifty Perl script to check GMail. The script basically parses an Atom feed of the latest 20 mails provided by Google. Since a Python hacker like Swaroop is dabbling in Perl, I thought it was my duty as a Python evangelist (or is it Pythangelist?) to show the people that the same thing can be achieved using Python with equal ease 🙂 The main code is around 50% of the total code. A large portion of the code is used for the pretty printing. Here it is —

## check-gmail.py -- A command line util to check GMail -*- Python -*-

# ======================================================================
# Copyright (C) 2006 Baishampayan Ghose <b.ghose@ubuntu.com>
# Time-stamp: Mon Jul 31, 2006 20:45+0530
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
# ======================================================================

import urllib             # For BasicHTTPAuthentication
import feedparser         # For parsing the feed
from textwrap import wrap # For pretty printing assistance

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

def auth():
    '''The method to do HTTPBasicAuthentication'''
    opener = urllib.FancyURLopener()
    f = opener.open(_URL)
    feed = f.read()
    return feed

def fill(text, width):
    '''A custom method to assist in pretty printing'''
    if len(text) < width:
        return text + ' '*(width-len(text))
    else:
        return text

def readmail(feed):
    '''Parse the Atom feed and print a summary'''
    atom = feedparser.parse(feed)
    print ""
    print atom.feed.title
    print "You have %s new mails" % len(atom.entries)
    # Mostly pretty printing magic
    print "+"+("-"*84)+"+"
    print "| Sl.|"+" Subject"+' '*48+"|"+" Author"+' '*15+"|"
    print "+"+("-"*84)+"+"
    for i in xrange(len(atom.entries)):
        print "| %s| %s| %s|" % (
            fill(str(i), 3),
            fill(wrap(atom.entries[i].title, 50)[0]+"[...]", 55),
            fill(wrap(atom.entries[i].author, 15)[0]+"[...]", 21))
    print "+"+("-"*84)+"+"

if __name__ == "__main__":
    f = auth()  # Do auth and then get the feed
    readmail(f) # Let the feed be chewed by feedparser

And here is a sample output —

ghoseb@trinka:~$ python check-gmail.py
Enter username for New mail feed at mail.google.com: foo.bar
Enter password for foo.bar in New mail feed at mail.google.com:

Gmail - Inbox for foo.bar@gmail.com
You have 20 new mails
+------------------------------------------------------------------------------------+
| Sl.| Subject                                                | Author               |
+------------------------------------------------------------------------------------+
| 0  | Strip Whitespace Middleware[...]                       | Will McCutchen ([...]|
| 1  | [FOSS Nepal] list of free alternatives to windows[...] | Manish Regmi (r[...] |
| 2  | json serialization[...]                                | Gábor Farkas (g[...] |
| 3  | editable=False and "Could not find Formfield or[...]   | Corey (coordt@e[...] |
| 4  | IronPython 1.0 release candidate[...]                  | Jeremy Dunck (j[...] |
| 5  | django server tree organization[...]                   | Kenneth[...]         |
| 6  | Project when using multiple sites[...]                 | Jay Parlar (par[...] |
| 7  | [FOSS Nepal] Neprog (nepali version pogrammer for[...] | ujwal (ujwal2@g[...] |
| 8  | Bug#379789: wrong keymap on Intel MacBook Pro[...]     | Frans Pop (elen[...] |
| 9  | debconf is Level 1?[...]                               | Clytie Siddall ([...]|
| 10 | Weird slowdown with dev server behind nat[...]         | Akatemik (tpiev[...] |
| 11 | Database API question: I am not able to return a[...]  | DavidA (david.a[...] |
| 12 | Bug#379120: lspci present on i386, verify on[...]      | Eddy Petrişor ([...] |
| 13 | New levels of D-I[...]                                 | Eddy Petrişor ([...] |
| 14 | Installed Apps in settings.py[...]                     | limodou (limodo[...] |
| 15 | where u at man ... where can i call you ??????[...]    | Sanjeev[...]         |
| 16 | unable to runser ?[...]                                | Geert[...]           |
| 17 | Bug#380585: debian 3.1 install FD[...]                 | as_hojoe (as_ho[...] |
| 18 | Re: Translated packages descriptions progress[...]     | Michael Bramer ([...]|
| 19 | Loading an url takes 60 sec.[...]                      | and_ltsk (andre[...] |
+------------------------------------------------------------------------------------+
ghoseb@trinka:~$

Well, the code is obviously pretty rough. It’s just for showing newbies how to use feedparser and urllib, two very powerful Python libraries. Improvements, patches are welcome 🙂

The text source can also be downloaded.

Advertisements
 

GNU Emacs with XFT goodness Mon, 06 Nov 2006 17:56:48 +0000

Filed under: GNU/Linux,Programming,Ubuntu — Bon Goose @ 17:56:48

Personally I have been using the emacs-unicode-2 CVS branch of GNU Emacs for a long time now. This branch will become Emacs 23 in the future and it contains better Unicode handling code in addition to some cool features like antialiased fonts (courtesy XFT and FreeType), copy to clipboard, etc.

GNU Emacs with XFT

I usually build a custom version of the emacs-snapshot package with source from the emacs-unicode-2 branch and some simple customisation to enable the antialiased fonts. Some of my friends are using those packages and I thought it’d be good if share those packages with a larger audience.

So I have set up an APT repository with those packages which you can install on your Ubuntu Dapper machine. To do that, first add the following line to your /etc/apt/sources.list

deb http://people.ubuntu-in.org/~ghoseb/apt/ dapper main
deb-src http://people.ubuntu-in.org/~ghoseb/apt/ dapper main # for source

Then do sudo apt-get update to fetch the package list and then sudo apt-get install emacs-snapshot-gtk to install the customised GNU Emacs package.

If you already have the vanilla emacs-snapshot installed then only sudo apt-get upgrade will upgrade your current installation to the new one. And yes, this package can co-exist peacefully with your emacs21 package.

Now to enable antialiased fonts, add the following line to your ~/.emacs

(set-default-font "Bitstream Vera Sans Mono-8")

Now launch GNU Emacs as emacs-snapshot-gtk --enable-font-backend and enjoy the awesomeness 🙂

Leave a comment if any of you need Ubuntu Edgy packages, I will build them and add to the repository.

Update: I have added Edgy packages too ! Just add the following lines to your /etc/apt/sources.list and the rest is identical to the instructions for Dapper.

deb http://people.ubuntu-in.org/~ghoseb/apt-edgy/ edgy main
deb-src http://people.ubuntu-in.org/~ghoseb/apt-edgy/ edgy main # for source

 

A first look at ayttm Fri, 25 Nov 2005 19:35:08 +0000

Filed under: Free Software,GNU/Linux,Open Source Software,Programming — Bon Goose @ 19:35:08

I have never used ayttm myself, but I know Philip S. Tellis, its primary author quite well. It appears that the project is in a great need of active developers as Philip is quite busy with his work and others are I guess absconding. For those who don't know what ayttm is, it's a multi-protocol and cross platform messaging client, a bit like GAIM. ayttm is quite novel in its own right as it seems to support Yahoo! Messenger webcams, though I haven't tested it myself. There are a few nagging issues wrt ayttm at the moment like the GUI is still using GTk+ 1.x while the whole world has moved to GTk+ 2.6.x upwards. So I thought it was worthwhile to check it out and see if I could help in some way. I checked out the source from the CVS repo and tried building it. And I hit a bug straightaway. There was this small bug which would cause an error when compiled with gcc 4.x. So I whipped up a small patch to fix it and submitted it to the list. And no, it was not a one-liner (it was a two-liner ;)). I just checked that the patch was committed by Philip. Cool! I guess I have to devote some time hacking ayttm after my freakin' exams are over. And yes, if you are a C programmer, and want to contribute to Free Software, ayytm needs you. Join the lists today!