<s> import imaplib <EOL> import email <EOL> import re <EOL> from dateutil import parser <EOL> WORDS = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> def getSender ( email ) : <EOL> """<STR_LIT>""" <EOL> sender = email [ '<STR_LIT>' ] <EOL> m = re . match ( r'<STR_LIT>' , sender ) <EOL> if m : <EOL> return m . group ( <NUM_LIT:1> ) <EOL> return sender <EOL> def getDate ( email ) : <EOL> return parser . parse ( email . get ( '<STR_LIT:date>' ) ) <EOL> def getMostRecentDate ( emails ) : <EOL> """<STR_LIT>""" <EOL> dates = [ getDate ( e ) for e in emails ] <EOL> dates . sort ( reverse = True ) <EOL> if dates : <EOL> return dates [ <NUM_LIT:0> ] <EOL> return None <EOL> def fetchUnreadEmails ( profile , since = None , markRead = False , limit = None ) : <EOL> """<STR_LIT>""" <EOL> conn = imaplib . IMAP4_SSL ( '<STR_LIT>' ) <EOL> conn . debug = <NUM_LIT:0> <EOL> conn . login ( profile [ '<STR_LIT>' ] , profile [ '<STR_LIT>' ] ) <EOL> conn . select ( readonly = ( not markRead ) ) <EOL> msgs = [ ] <EOL> ( retcode , messages ) = conn . search ( None , '<STR_LIT>' ) <EOL> if retcode == '<STR_LIT:OK>' and messages != [ '<STR_LIT>' ] : <EOL> numUnread = len ( messages [ <NUM_LIT:0> ] . split ( '<STR_LIT:U+0020>' ) ) <EOL> if limit and numUnread > limit : <EOL> return numUnread <EOL> for num in messages [ <NUM_LIT:0> ] . split ( '<STR_LIT:U+0020>' ) : <EOL> ret , data = conn . fetch ( num , '<STR_LIT>' ) <EOL> msg = email . message_from_string ( data [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) <EOL> if not since or getDate ( msg ) > since : <EOL> msgs . append ( msg ) <EOL> conn . close ( ) <EOL> conn . logout ( ) <EOL> return msgs <EOL> def handle ( text , mic , profile ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> msgs = fetchUnreadEmails ( profile , limit = <NUM_LIT:5> ) <EOL> if isinstance ( msgs , int ) : <EOL> response = "<STR_LIT>" % msgs <EOL> mic . say ( response ) <EOL> return <EOL> senders = [ getSender ( e ) for e in msgs ] <EOL> except imaplib . IMAP4 . error : <EOL> mic . say ( <EOL> "<STR_LIT>" ) <EOL> return <EOL> if not senders : <EOL> mic . say ( "<STR_LIT>" ) <EOL> elif len ( senders ) == <NUM_LIT:1> : <EOL> mic . say ( "<STR_LIT>" + senders [ <NUM_LIT:0> ] + "<STR_LIT:.>" ) <EOL> else : <EOL> response = "<STR_LIT>" % len ( <EOL> senders ) <EOL> unique_senders = list ( set ( senders ) ) <EOL> if len ( unique_senders ) > <NUM_LIT:1> : <EOL> unique_senders [ - <NUM_LIT:1> ] = '<STR_LIT>' + unique_senders [ - <NUM_LIT:1> ] <EOL> response += "<STR_LIT>" <EOL> response += '<STR_LIT>' . join ( senders ) <EOL> else : <EOL> response += "<STR_LIT>" + unique_senders [ <NUM_LIT:0> ] <EOL> mic . say ( response ) <EOL> def isValid ( text ) : <EOL> """<STR_LIT>""" <EOL> return bool ( re . search ( r'<STR_LIT>' , text , re . IGNORECASE ) ) </s>
