Advertisement
Guest User

Gmail feed parser in Python

a guest
Mar 8th, 2012
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. """
  4. Checks Gmail for unread mails by parsing the unread mail feed.
  5. Requires python-feedparser. Set _USER and _PASSWD before using.
  6. """
  7.  
  8. import urllib
  9. import feedparser
  10.  
  11. _URL = 'https://mail.google.com/mail/feed/atom/'   # Gmail feed url
  12. _USER = ''                           # Username
  13. _PASSWD = ''       # Password
  14.  
  15. # Colors
  16. _GREEN = '\033[1;32m'
  17. _YELLOW = '\033[1;33m'
  18. _RESET = '\033[0;0m'
  19.  
  20. class GmailOpener(urllib.FancyURLopener):
  21.     """
  22.    Child class of urllib.FancyURLopener. Overrides the default
  23.    prompt_user_passwd() method and returns (_USER, _PASSWD) instead.
  24.    """
  25.  
  26.     def prompt_user_passwd(self, host, realm):
  27.         return _USER, _PASSWD
  28.  
  29. def readMail():
  30.     """
  31.    Function to authenticate and read mail.
  32.    """
  33.  
  34.     opener = GmailOpener()
  35.     f = opener.open(_URL)
  36.  
  37.     feed = feedparser.parse(f)
  38.     unread = feed['feed']['fullcount']
  39.  
  40.     print ('You have' + _GREEN + ' (' + _YELLOW + unread + _GREEN + ') ' +
  41.              _RESET + 'unread mails.\n')
  42.  
  43.     if unread != '0':
  44.         for mail in feed.entries:
  45.             print '* ' + mail.author + ' : ' + mail.title + '\n'
  46.  
  47. if __name__ == '__main__':
  48.     readMail()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement