<commit_before>#!/usr/bin/env python3

import imaplib
import json
import getpass
from argparse import ArgumentParser

# List of arguments for ArgumentParser
arguments = [
    ('username', dict(default='<EMAIL>')),
    ('password', dict(default='<PASSWORD>')),
    ('host', dict(default='imap.gmail.com')),
    ('folder', dict(default='INBOX')),
    ('after', dict(type=str)),
    ('headers-only', dict(default=False, action='store_const', const=True)),
    ('humanize', dict(default=False, action='store_const', const=True)),
    ('message-id', dict(type=int)),
    ('list-folders', dict(default=False, action='store_const', const=True)),
    ]


def unpack_argument(args):
    ''' Convenient argument wrapper '''
    if not 'const' in args:
        if 'default' in args and not 'type' in args:
            # find type from default value
            args['type'] = type(args['default'])
    return args

# Build argument parser
parser = ArgumentParser(description='IMAP wrapper.')
for name, args in arguments:
    args = unpack_argument(args)
    parser.add_argument(
        '--%s' % name,
        dest=name.replace('-', '_'),
        **args)


def fetch_mail(M, msg_id, msg_part):
    ''' Fetch mail content '''
    typ, data = M.fetch(str(int(msg_id)), '(BODY.PEEK[' + msg_part + '])')
    data2 = data[0][1].decode()
    return {"mail-id": str(int(msg_id)), "content": data2}


def fetch_result(M, args):
    ''' Fetch result as specified in args '''
    M.select(args.folder, "true")
    typ, data = M.search(None, 'ALL')
    #typ, data = M.search(None, 'AFTER', '"01-Jan-2010"')

    if args.list_folders:
        folders = M.list()
        if folders[0] == 'OK':
            return [folder.decode() for folder in folders[1]]

    if args.message_id is not None:
        return fetch_mail(M, args.message_id, args.message_part)
    else:
        mail_list = []
        for num in data[0].split():
            mail_list.append(fetch_mail(M, num, args.message_part))
        return mail_list


def to_json(result, args):
    ''' Convert result to JSON '''
    if args.humanize:
        json_args = dict(indent=True)
    else:
        json_args = dict(separators=(',', ':'))
    return json.dumps(result, **json_args)


def main():
    args = parser.parse_args()
    # Set message part
    message_part = 'TEXT'
    if args.headers_only:
        message_part = 'HEADER'
    setattr(args, 'message_part', message_part)

    # Init mailbox
    M = imaplib.IMAP4_SSL(args.host)
    # Get username and password
    usernm = args.username
    usernm = (usernm == '-' and getpass.getpass('Username: ')) or usernm
    passwd = args.password
    passwd = (passwd == '-' and getpass.getpass()) or passwd
    M.login(usernm, passwd)<commit_msg>added support for --after<commit_after>#!/usr/bin/env python3

import imaplib
import json
import getpass
from argparse import ArgumentParser

# List of arguments for ArgumentParser
arguments = [
    ('username', dict(default='<EMAIL>')),
    ('password', dict(default='<PASSWORD>')),
    ('host', dict(default='imap.gmail.com')),
    ('folder', dict(default='INBOX')),
    ('after', dict(type=str)), # 01-Nov-2010
    ('headers-only', dict(default=False, action='store_const', const=True)),
    ('humanize', dict(default=False, action='store_const', const=True)),
    ('message-id', dict(type=int)),
    ('list-folders', dict(default=False, action='store_const', const=True)),
    ]


def unpack_argument(args):
    ''' Convenient argument wrapper '''
    if not 'const' in args:
        if 'default' in args and not 'type' in args:
            # find type from default value
            args['type'] = type(args['default'])
    return args

# Build argument parser
parser = ArgumentParser(description='IMAP wrapper.')
for name, args in arguments:
    args = unpack_argument(args)
    parser.add_argument(
        '--%s' % name,
        dest=name.replace('-', '_'),
        **args)


def fetch_mail(M, msg_id, msg_part):
    ''' Fetch mail content '''
    typ, data = M.fetch(str(int(msg_id)), '(BODY.PEEK[' + msg_part + '])')
    data2 = data[0][1].decode()
    return {"mail-id": str(int(msg_id)), "content": data2}


def fetch_result(M, args):
    ''' Fetch result as specified in args '''
    M.select(args.folder, "true")
    typ, data = M.search(None, args.message_since)

    if args.list_folders:
        folders = M.list()
        if folders[0] == 'OK':
            return [folder.decode() for folder in folders[1]]

    if args.message_id is not None:
        return fetch_mail(M, args.message_id, args.message_part)
    else:
        mail_list = []
        for num in data[0].split():
            mail_list.append(fetch_mail(M, num, args.message_part))
        return mail_list


def to_json(result, args):
    ''' Convert result to JSON '''
    if args.humanize:
        json_args = dict(indent=True)
    else:
        json_args = dict(separators=(',', ':'))
    return json.dumps(result, **json_args)


def main():
    args = parser.parse_args()
    # Set message part
    message_part = 'TEXT'
    if args.headers_only:
        message_part = 'HEADER'
    setattr(args, 'message_part', message_part)

    message_since = 'ALL'
    if args.after:
        message_since = '(SINCE "' + args.after  + '")'
    setattr(args, 'message_since', message_since)

    # Init mailbox
    M = imaplib.IMAP4_SSL(args.host)
    # Get username and password
    usernm = args.username
    usernm = (usernm == '-' and getpass.getpass('Username: ')) or usernm
    passwd = args.password
    passwd = (passwd == '-' and getpass.getpass()) or passwd
    M.login(usernm, passwd)
{'fraction_non_alphanumeric': 0.1560549313358302, 'fraction_numerical': 0.005350454788657036, 'mean_word_length': 3.1726190476190474, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3042387', 'n_tokens_mistral': 1861, 'n_tokens_neox': 1828, 'n_words': 446}