Catch the exception if decoding failed.
[pymailheaders.git] / feedprl.py
blobe78642e20936aac45189e6e58afd3792bf6bb22a
1 #!/usr/bin/env python
3 # feedprl.py
4 # Copyright 2007 Neil Shi <zeegeek@gmail.com>
6 # Feed Protocol
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.
19 import feedparser
20 import re
22 from exception import *
24 class feed:
25 """feed class
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:
33 __server
34 __mbox
35 __uname
36 __pass
37 __ssl
38 __url
39 __feed
40 """
42 __server = ''
43 __mbox = ''
44 __uname = ''
45 __pass = ''
46 __ssl = False
47 __url = ''
48 __feed = {}
50 def __init__(self, server, uname, password, ssl, h, mbox):
51 """Constructor
53 @type server: string
54 @param server: feed URL
55 @type uname: string
56 @param uname: username
57 @type password: string
58 @param password: password
59 @type ssl: bool
60 @param ssl: dummy variable for compatibility
61 @type h: int
62 @param h: dummy variable
63 @type mbox: string
64 @param mbox: Gmail label
65 """
67 if server == 'gmail':
68 self.__server = 'mail.google.com/gmail/feed/atom'
69 self.__ssl = True
70 else:
71 # get rid of 'http[s]://'
72 self.__server = re.sub('^[^/]*:/*', '', server)
73 if ssl != None:
74 self.__ssl = ssl
75 if mbox != 'INBOX':
76 self.__mbox = mbox
77 # replace @ to html code
78 if uname and password:
79 self.__uname = uname.replace('@', '%40')
80 self.__pass = password
82 def connect(self):
83 """Form URL.
84 """
86 # assemble URL
87 if self.__ssl:
88 self.__url = 'https://'
89 else:
90 self.__url = 'http://'
91 if self.__uname and self.__pass:
92 self.__url += self.__uname + ':' + self.__pass + '@'
93 self.__url += self.__server
95 def get_mail(self):
96 """Parse feed.
98 @rtype: list
99 @return: List of tuples of flag, sender addresses and subjects.
100 Oldest message on top.
103 # get feed
104 try:
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)
115 except:
116 raise
118 # parse sender addresses and subjects
119 def a(x):
120 sender = ''
122 if x.has_key('author_detail'):
123 author = x.author_detail
124 if author.has_key('name'):
125 sender = author.name
126 elif author.has_key('email'):
127 sender = author.email
128 elif x.has_key('author'):
129 sender = x.author
130 return (True, sender, x.title)
132 return map(a, self.__feed.entries)