Bläddra i källkod

imap-Schleife für mehr als 1000 Mails

o365: überflüssige Kontakte werden automatisch gelöscht
robert 2 år sedan
förälder
incheckning
bbdc7e3ca1
2 ändrade filer med 19 tillägg och 16 borttagningar
  1. 10 3
      contacts/o365_copy.py
  2. 9 13
      mailserver/imap.py

+ 10 - 3
contacts/o365_copy.py

@@ -1,5 +1,5 @@
 from O365 import Account
-from O365.address_book import Contact, Recipients
+from O365.address_book import Contact
 
 
 client_id = '925f74dc-f96a-4718-9ca7-d6cc3fa43e1e'
@@ -50,6 +50,7 @@ def sync_contacts():
     account.authenticate()
     shared = account.address_book(resource='adressbuch@global-cube.net', address_book='personal')
     shared_contacts = {p.display_name: p for p in shared.get_contacts(limit=None)}
+    delete_candidates = set()
 
     for mailbox in mailboxes:
         print(mailbox)
@@ -57,6 +58,10 @@ def sync_contacts():
         personal_contacts = {p.display_name: p for p in personal.get_contacts(limit=None)}
         extra_contacts = set(personal_contacts.keys()).difference(shared_contacts.keys())
         print(extra_contacts)
+        if len(delete_candidates) == 0:
+            delete_candidates = extra_contacts
+        else:
+            delete_candidates = delete_candidates.intersection(extra_contacts)
         # p.delete()
 
         for p_shared in shared_contacts.values():
@@ -67,6 +72,7 @@ def sync_contacts():
                 p_existing = personal_contacts[p_shared.display_name]
                 if p_existing.to_api_data() != p_shared.to_api_data():
                     copy_contact(p_shared, p_existing)
+    return delete_candidates
 
 
 def delete_contacts(delete_candidates):
@@ -111,6 +117,7 @@ def cleanup_contacts():
 
 
 if __name__ == '__main__':
-    sync_contacts()
-    # delete_contacts({'Matios Yigit', 'Martin Petz', 'Herr Steinraths', 'Herr Fischer'})
+    contacts = sync_contacts()
+    if len(contacts) > 0:
+        delete_contacts(contacts)
     # cleanup_contacts()

+ 9 - 13
mailserver/imap.py

@@ -30,13 +30,13 @@ class Imap:
                 if msg_count < msg_limit:
                     continue
                 mb.folder.set(folder)
-                messages = mb.fetch(
-                    criteria=AND(date_lt=date_criteria),
-                    mark_seen=True,
-                    headers_only=True,
-                )
-                uids = [msg.uid for msg in messages]
-                mb.delete(uids[0 : (msg_count - msg_limit)])
+                uids = [0] * 101
+                while len(uids) > 100:
+                    messages = mb.fetch(
+                        criteria=AND(date_lt=date_criteria), mark_seen=True, limit=1000, headers_only=True
+                    )
+                    uids = [msg.uid for msg in messages]
+                    mb.delete(uids[0 : (msg_count - msg_limit)])
 
     def move(self):
         with MailBox("mail.global-cube.com").login("archiv", "Gc01am64!") as mb:
@@ -147,12 +147,8 @@ class Imap:
 
                             selected.append(";".join([mail_to, text]))
                             if len(msg.attachments) > 0:
-                                msg2 = MailMessage.from_bytes(
-                                    msg.attachments[0].payload
-                                )
-                                selected[-1] += ";" + ";".join(
-                                    [msg2.from_, msg2.subject, msg2.date_str]
-                                )
+                                msg2 = MailMessage.from_bytes(msg.attachments[0].payload)
+                                selected[-1] += ";" + ";".join([msg2.from_, msg2.subject, msg2.date_str])
             with open(os.path.dirname(__file__) + "/undelivered.csv", "w") as fwh:
                 fwh.write("\n".join(selected))