Skip to content

Instantly share code, notes, and snippets.

@Maffsie
Created January 4, 2010 00:04
Show Gist options
  • Save Maffsie/268198 to your computer and use it in GitHub Desktop.
Save Maffsie/268198 to your computer and use it in GitHub Desktop.
import sys
import urllib # For BasicHTTPAuthentication
import feedparser # For parsing the feed
from textwrap import wrap
_URL = "https://mail.google.com/gmail/feed/atom"
users = [['usernames'],['passwords'],['name for each account']]
def auth(i):
'''The method to do HTTPBasicAuthentication'''
urllib.FancyURLopener.prompt_user_passwd = lambda self, host, realm: (users[0][i], users[1][i])
opener = urllib.FancyURLopener()
f = opener.open(_URL)
feed = f.read()
return feed
def readmail(feed, i):
'''Parse the Atom feed and print a summary'''
atom = feedparser.parse(feed)
if(len(atom.entries) > 0):
rtrn = '%s: %s' % (users[2][i], len(atom.entries))
cnt = len(atom.entries)
return rtrn, cnt
else:
return 0, 0
if __name__ == "__main__":
count = 0
out = ""
for item in range(len(users[0])):
f = auth(item) # Do auth and then get the feed
m, cnt = readmail(f, item) # Let the feed be chewed by feedparser
count = count + cnt
if(m != 0):
if(item != 0 and len(out) > 0):
out += ", "
out += m
if(count == 0):
print "No new mail"
else:
if(count != 1):
print `count` + " new emails (" + out + ")"
else:
print `count` + " new email (" + out + ")"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment