1
# -*- coding: utf-8 -*-
6
# 1) Create an instance of 'GmailAtom' class. The two arguments
7
# its constructor take are the username (including '@gmail.com')
9
# 2) To retrieve the account status call 'refreshInfo()'.
10
# 3) To get the unread messages count call 'getUnreadMsgCount()'.
11
# You MUST call 'refreshInfo()' at least one time before using
12
# this method or it will return zero.
13
# 4) To get specific information about an unread email you must
14
# call the corresponding getter method passing to it the number
15
# of the message. The number zero represents the newest one.
16
# You MUST call 'refreshInfo()' at least one time before using any
17
# getter method or they will return an empty string.
18
# The getter methods are:
20
# getMsgSummary(index)
21
# getMsgAuthorName(index)
22
# getMsgAuthorEmail(index)
25
# juan.grande@gmail.com
27
from xml.sax.handler import ContentHandler
39
class MailHandler(ContentHandler):
43
TAG_FULLCOUNT = "fullcount"
46
TAG_SUMMARY = "summary"
51
# Path the information
52
PATH_FULLCOUNT = [ TAG_FEED, TAG_FULLCOUNT ]
53
PATH_TITLE = [ TAG_FEED, TAG_ENTRY, TAG_TITLE ]
54
PATH_SUMMARY = [ TAG_FEED, TAG_ENTRY, TAG_SUMMARY ]
55
PATH_AUTHOR_NAME = [ TAG_FEED, TAG_ENTRY, TAG_AUTHOR, TAG_NAME ]
56
PATH_AUTHOR_EMAIL = [ TAG_FEED, TAG_ENTRY, TAG_AUTHOR, TAG_EMAIL ]
61
def startDocument(self):
66
def startElement( self, name, attrs):
68
self.actual.append(name)
70
# add a new email to the list
73
self.entries.append(m)
75
def endElement( self, name):
79
def characters( self, content):
81
if (self.actual==self.PATH_FULLCOUNT):
82
self.mail_count = self.mail_count+content
85
if (self.actual==self.PATH_TITLE):
86
temp_mail=self.entries.pop()
87
temp_mail.title=temp_mail.title+content
88
self.entries.append(temp_mail)
91
if (self.actual==self.PATH_SUMMARY):
92
temp_mail=self.entries.pop()
93
temp_mail.summary=temp_mail.summary+content
94
self.entries.append(temp_mail)
97
if (self.actual==self.PATH_AUTHOR_NAME):
98
temp_mail=self.entries.pop()
99
temp_mail.author_name=temp_mail.author_name+content
100
self.entries.append(temp_mail)
102
# Message author email
103
if (self.actual==self.PATH_AUTHOR_EMAIL):
104
temp_mail=self.entries.pop()
105
temp_mail.author_addr=temp_mail.author_addr+content
106
self.entries.append(temp_mail)
108
def getUnreadMsgCount(self):
109
return int(self.mail_count)
114
realm = "New mail feed"
115
host = "https://mail.google.com"
116
url = host + "/mail/feed/atom"
118
def __init__(self, user, pswd):
119
self.m = MailHandler()
120
# initialize authorization handler
121
auth_handler = urllib2.HTTPBasicAuthHandler()
122
auth_handler.add_password( self.realm, self.host, user, pswd)
123
opener = urllib2.build_opener(auth_handler)
124
urllib2.install_opener(opener)
126
def sendRequest(self):
127
return urllib2.urlopen(self.url)
129
def refreshInfo(self):
130
# get the page and parse it
131
p = sax.parseString( self.sendRequest().read(), self.m)
133
def getUnreadMsgCount(self):
134
return self.m.getUnreadMsgCount()
136
def getMsgTitle(self, index):
137
return self.m.entries[index].title
139
def getMsgSummary(self, index):
140
return self.m.entries[index].summary
142
def getMsgAuthorName(self, index):
143
return self.m.entries[index].author_name
145
def getMsgAuthorEmail(self, index):
146
return self.m.entries[index].author_email