4 # Copyright 2007 Neil Shi <zeegeek@gmail.com>
7 # This is the xml feed protocol support module for pymailheaders.
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
22 from exception
import *
27 @attention: if an exception Error is thrown by any of the method, by
28 disconnecting and connecting again, the problem should be solved.
30 @warning: B{Have to call connect() method before doing anything else}
32 @note: Private member variables:
50 def __init__(self
, server
, uname
, password
, ssl
, h
, mbox
):
54 @param server: feed URL
56 @param uname: username
57 @type password: string
58 @param password: password
60 @param ssl: dummy variable for compatibility
62 @param h: dummy variable
64 @param mbox: Gmail label
68 self
.__server
= 'mail.google.com/gmail/feed/atom'
71 # get rid of 'http[s]://'
72 self
.__server
= re
.sub('^[^/]*:/*', '', server
)
77 # replace @ to html code
78 if uname
and password
:
79 self
.__uname
= uname
.replace('@', '%40')
80 self
.__pass
= password
88 self
.__url
= 'https://'
90 self
.__url
= 'http://'
91 if self
.__uname
and self
.__pass
:
92 self
.__url
+= self
.__uname
+ ':' + self
.__pass
+ '@'
93 self
.__url
+= self
.__server
99 @return: List of tuples of flag, sender addresses and subjects.
100 Oldest message on top.
105 self
.__feed
= feedparser
.parse(self
.__url
)
106 # check if it's a well formed feed
107 if self
.__feed
.bozo
== 1 and \
108 not isinstance(self
.__feed
.bozo_exception
, \
109 feedparser
.CharacterEncodingOverride
) and \
110 not isinstance(self
.__feed
.bozo_exception
, \
111 feedparser
.NonXMLContentType
):
112 a
= self
.__feed
.bozo_exception
113 raise Error('feedprl (get_mail)', \
114 hasattr(a
, 'getMessage') and a
.getMessage() or a
)
118 # parse sender addresses and subjects
122 if x
.has_key('author_detail'):
123 author
= x
.author_detail
124 if author
.has_key('name'):
126 elif author
.has_key('email'):
127 sender
= author
.email
128 elif x
.has_key('author'):
130 return (True, sender
, x
.title
)
132 return map(a
, self
.__feed
.entries
)