can read only one mail

This commit is contained in:
2026-04-02 23:22:33 +02:00
parent 40d479b2fc
commit 75002d677f
3 changed files with 57 additions and 49 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ def create_message_from_item(item: ReserveResultPojo):
def main(): def main():
# initialize discord # initialize discord
print("init discord done") print("init discord done")
_accepted_appointments = read_mails_and_find_confirmation_contacts(mode='gmx_only') _accepted_appointments = read_mails_and_find_confirmation_contacts(mode='default')
# for item in _accepted_appointments: # for item in _accepted_appointments:
# send_message(create_message_from_item(item)) # send_message(create_message_from_item(item))
+19
View File
@@ -391,6 +391,25 @@ class MongoDbManager:
def list_collection_names(self): def list_collection_names(self):
return self.db.list_collection_names() return self.db.list_collection_names()
def get_unused_yahoo_emails(self) -> list:
"""
比较 DESTINATION_EMAIL_LIST 中的 Yahoo 邮箱与 CONTACT_LIST_TO_BOOK 中的联系人邮箱,
返回未被任何联系人使用的 Yahoo 邮箱列表。
"""
destination_emails: list = self.get_destination_emails()
contacts: list = self.get_all_contacts_to_book()
contact_mail_set = {contact.mail.lower() for contact in contacts}
unused_yahoo_emails = [
mail_address
for mail_address in destination_emails
if "yahoo" in mail_address.mail.lower()
and mail_address.mail.lower() not in contact_mail_set
]
return unused_yahoo_emails
MONGO_STORE_MANAGER = MongoDbManager() MONGO_STORE_MANAGER = MongoDbManager()
+29 -40
View File
@@ -441,77 +441,66 @@ class ProxyMailReader:
) -> List[MailResult]: ) -> List[MailResult]:
results: List[MailResult] = [] results: List[MailResult] = []
since = since or datetime.datetime.today() since = since or datetime.datetime.today()
# 用于去重:同一主题+发信人只读第一封
seen_subject_from: set = set()
try: try:
client.select_folder(folder, readonly=True) client.select_folder(folder)
except Exception as exc: except Exception as exc:
logger.warning("[%s] Impossible d'ouvrir '%s' : %s", logger.warning("[%s] Impossible d'ouvrir '%s' : %s",
self.account.login, folder, exc) self.account.login, folder, exc)
return results return results
try: try:
# Utilise les sujets injectés dans client pour filtrer dès la requête IMAP messages = client.search(['SINCE', since])
uids = client.search_by_subjects(since=since)
except Exception as exc: except Exception as exc:
logger.warning("[%s] Recherche échouée dans '%s' : %s", logger.warning("[%s] Recherche échouée dans '%s' : %s",
self.account.login, folder, exc) self.account.login, folder, exc)
return results return results
if not uids: if not messages:
return results return results
print("uids {}".format(uids)) print("uids {}".format(messages))
logger.info("[%s] %d message(s) dans '%s'", logger.info("[%s] %d message(s) dans '%s'",
self.account.login, len(uids), folder) self.account.login, len(messages), folder)
for uid, msg_data in client.fetch(uids, "RFC822").items(): for uid, msg_data in client.fetch(messages, 'RFC822').items():
try: try:
raw = msg_data.get(b"RFC822") or msg_data.get("RFC822") raw = msg_data.get(b'RFC822') or msg_data.get('RFC822')
if raw is None: if raw is None:
continue continue
em = email.message_from_bytes(raw) em = email.message_from_bytes(raw)
subject = em.get("Subject", "") from_address = em.get('FROM', '')
from_addr = em.get("From", "") subject = em.get('subject', '')
to_addr = em.get("To", self.account.login) to_addr = em.get('To', self.account.login)
message_id = em.get("Message-ID", "").strip() message_id = em.get('Message-ID', '').strip()
print("subject {}".format(subject)) body = ""
print("message_id {}".format(message_id))
# 去重:同一主题+发信人只读第一封 for part in em.walk():
dedup_key = (subject, from_addr) print(part.get_content_type())
if dedup_key in seen_subject_from: if part.get_content_type() == "text/html":
logger.debug( payload = part.get_payload(decode=True)
"[%s] Doublon ignoré (même sujet et expéditeur) dans '%s': %s", if payload:
self.account.login, folder, subject[:50] body = body + payload.decode("utf-8", errors="ignore")
) elif part.get_content_type() == "text/plain":
continue body = body + str(part.get_payload())
seen_subject_from.add(dedup_key)
# Filtrer : on ne garde que les emails correspondant aux sujets/expéditeurs configurés logger.info("mail is {} and subject is {}, body is {}".format(
is_validation = ( self.account.login, subject, body))
any(s in subject for s in self._subjects)
)
if not is_validation:
continue
body = extract_body(em)
# Filtrer selon les sujets configurés
if not self._subjects or any(s in subject for s in self._subjects):
result = MailResult( result = MailResult(
account=self.account.login, account=self.account.login,
subject=subject, subject=subject,
from_address=from_addr, from_address=from_address,
to_address=to_addr, to_address=to_addr,
body=body, body=body,
message_id=message_id message_id=message_id,
) )
results.append(result) results.append(result)
except Exception as exc: except Exception as error:
logger.warning( print(error)
"[%s] Erreur traitement uid=%s : %s", print("error trying to read email_message for {}".format(self.account.login))
self.account.login, uid, exc,
)
return results return results