def read(self): if self.address == "none": message= self.name + " is not a valid address." self.log.info(message) self.say(message) else: freshNews = feedparser.parse(self.address).entries if len(self.oldNews) == 0: toRead = freshNews[0:4] else: toRead = [i for i in freshNews if i not in self.oldNews] self.oldNews = freshNews greetings="su "+self.name+"." self.say(greetings) for news in toRead: digest = alviUtils.str_to_hlf(news.get('title')) # print self.address + ': '+digest self.log.info(self.address + ': '+digest) self.say(digest)
def _readImap(self): import imaplib try: if self.ssl: conn= imaplib.IMAP4_SSL(self.address) else: conn= imaplib.IMAP4(self.address) conn.login(self.username, self.passwd) except: message = "I am unable to connect to " +self.name self.log.warning(message) self.say(message) raise RuntimeError, "Cannot connect to server" code = conn.select('INBOX')[0] if code != 'OK': message="ERROR: Mailbox not found!!" self.say(message) self.log.warning(message) raise RuntimeError, "Failed to select inbox" code, data= conn.search(None, 'ALL') if code == 'OK': msgid_list= data[0].split() else: message="ERROR: messages DDI not found" self.say(message) self.log.warning(message) raise RuntimeError, "Failed to get message IDs" if not self.lastid in msgid_list: lastidx = len(msgid_list)-3 else: lastidx = msgid_list.index(self.lastid)+1 newids=msgid_list[lastidx:] newids.reverse() if len(newids) > 0: self.lastid = newids[0] greetings="su "+self.name+"." self.say(greetings) for id in newids: code, data = conn.fetch(id, '(RFC822.HEADER)') if code == 'OK': mail = email.message_from_string(data[0][1]) From_ = mail.get('From').split(' ') if(len(From_)>1): # nice-formed From. i.e.: sender <*****@*****.**> From = '' for s in From_[:-1]: From += s # too ugly ? #From = From.replace('"', '') From = alviUtils.str_to_hlf(From) else: # old-formed From. i.e.: <*****@*****.**> From = alviUtils.str_to_hlf(From_[0]) digest = From + " writes you: " + alviUtils.str_to_hlf(mail.get('Subject')) self.log.info(self.name+': '+digest) self.say(digest) else: message="ERROR: could not retrieve msg" self.log.warning(message) say(message) raise RuntimeError, "could not retrieve msg" conn.close() conn.logout() return self.lastid