@@ -112,36 +112,40 @@ def get(self):
112112 self .debug ("no change in emails" )
113113 continue
114114
115- # new message.. .
115+ # New message. Reverse so we look at the newest one first .
116116 old_ids = self ._old_ids [0 ].split ()
117- for msg_id in self ._new_ids [0 ].split ():
117+ msg_ids = self ._new_ids [0 ].split ()
118+ msg_ids .reverse ()
119+ for msg_id in msg_ids :
118120
119- # seen it?
121+ # Seen it?
120122 if msg_id in old_ids :
121123 continue
122124
123125 # New message. Look at all the parts and try to grab the code, if we catch an exception
124126 # just move onto the next part.
125127 self .debug ("new-msg={}" .format (msg_id ))
126- res , msg = self ._imap .fetch (msg_id , "(BODY.PEEK[])" )
127- if isinstance (msg [0 ][1 ], bytes ):
128- for part in email .message_from_bytes (msg [0 ][1 ]).walk ():
129- if part .get_content_type () != "text/html" :
130- continue
131- try :
132- for line in part .get_payload (decode = True ).splitlines ():
133- # match code in email, this might need some work if the email changes
134- code = re .match (r"^\W+(\d{6})\W*$" , line .decode ())
135- if code is not None :
136- self .debug (
137- "code={}" .format (code .group (1 ))
138- )
139- return code .group (1 )
140- except :
141- self .debug ("trying next part" )
142-
143- # update old so we don't keep trying new
144- self ._old_ids = self ._new_ids
128+ res , parts = self ._imap .fetch (msg_id , "(BODY.PEEK[])" )
129+ # res, parts = self._imap.fetch(msg_id, "(RFC822)")
130+
131+ for msg in parts :
132+ try :
133+ if isinstance (msg [1 ], bytes ):
134+ for part in email .message_from_bytes (msg [1 ]).walk ():
135+ if part .get_content_type () != "text/html" :
136+ continue
137+ for line in part .get_payload (decode = True ).splitlines ():
138+ # match code in email, this might need some work if the email changes
139+ code = re .match (r"^\W+(\d{6})\W*$" , line .decode ())
140+ if code is not None :
141+ self .debug (f"code={ code .group (1 )} " )
142+ return code .group (1 )
143+ except Exception as e :
144+ self .debug (f"trying next part { str (e )} " )
145+
146+ # Update old so we don't keep trying new.
147+ # Yahoo can lose ids so we extend the old list.
148+ self ._old_ids .extend (new_id for new_id in self ._new_ids if new_id not in self ._old_ids )
145149
146150 # problem parsing the message, force a fail
147151 except Exception as e :
0 commit comments