-
-
Save ab/9f113b0dd4740623bf51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import imaplib | |
import os | |
import logging | |
import optparse | |
# LABEL = 'support' | |
# QUERY = 'before:2015-06-01 -label:to-delete' | |
# DEST = 'to-delete' | |
LABEL = 'to-delete' | |
QUERY = None | |
DEST = '[Gmail]/Trash' | |
logging.basicConfig(level=logging.INFO, | |
format="[%(asctime)s] %(levelname)s %(message)s") | |
mail = imaplib.IMAP4_SSL('imap.gmail.com') | |
mail.login('[email protected]', os.environ['GMAIL_PASSWORD']) | |
logging.info("Logged in.") | |
status, n = mail.select(LABEL) | |
logging.info("SELECTED %s, found %s messages...", LABEL, n[0]) | |
if QUERY: | |
search_args = ('X-GM-RAW', QUERY) | |
else: | |
search_args = ('ALL',) | |
status, ids = mail.uid('search', None, *search_args) | |
if not ids[0]: | |
logging.info("search returned no UIDs") | |
os.exit(0) | |
ids = ids[0].split(' ') | |
logging.info("Fetched %s UIDs...", len(ids)) | |
def flush(): | |
global did_delete | |
mail.uid('copy', ','.join(batch), DEST) | |
did_delete += len(batch) | |
del batch[:] | |
logging.info("flagged %d messages...", did_delete) | |
did_delete = 0 | |
batch = [] | |
for i in ids: | |
batch.append(i) | |
if len(batch) >= 100: | |
flush() | |
flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment