<commit_before>#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#
# Simple example showing how to parse all .eml files in the current folder
# and extract any attachments to a pre-configured folder
#

import base64
import os

import eml_parser

# where to save attachments to
outpath = '.'

for k in os.listdir('.'):
    if k.endswith('.eml'):
        print('Parsing: {}'.format(k))

        m = eml_parser.eml_parser.decode_email(k, include_attachment_data=True)

        if 'attachments' in m:
            for a_id, a in m['attachments'].items():
                if a['filename'] == '':
                    filename = a_id
                else:
                    filename = a['filename']

                filename = os.path.join(outpath, filename)

                print('\tWriting attachment: {}'.format(filename))
                with open(filename, 'wb') as a_out:
                    a_out.write(base64.b64decode(a['raw']))

            print()
<commit_msg>fix and cleanup; fixes #57<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#
# Simple example showing how to parse all .eml files in the current folder
# and extract any attachments to a pre-configured folder
#

import argparse
import base64
import datetime
import email.header
import pathlib

import eml_parser


def json_serial(obj):
    """JSON serializer for objects not serializable by default json code"""

    if isinstance(obj, datetime.datetime):
        serial = obj.isoformat()
        return serial
    elif isinstance(obj, email.header.Header):
        print(str(obj))
        raise Exception('object cannot be of type email.header.Header')
    elif isinstance(obj, bytes):
        return obj.decode('utf-8', errors='ignore')

    raise TypeError(f'Type "{str(type(obj))}" not serializable')


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('-p', dest='path',
                        help='Path to scan for EML files.', required=True)
    parser.add_argument('-o', dest='outpath', default='.',
                        help='Path where to save attachments in (default is current directory).')

    options = parser.parse_args()

    scan_path = pathlib.Path(options.path)
    out_path = pathlib.Path(options.outpath)

    if not scan_path.is_dir():
        raise SystemExit('Specified path is not accessible')

    if not out_path.is_dir():
        out_path.mkdir()

    ep = eml_parser.EmlParser(include_attachment_data=True)

    for k in scan_path.iterdir():
        if k.suffix == '.eml':
            print(f'Parsing: {str(k)}')

            m = ep.decode_email(k)

            if 'attachment' in m:
                for a in m['attachment']:
                    out_filepath = out_path / a['filename']

                    print(f'\tWriting attachment: {out_filepath}')
                    with out_filepath.open('wb') as a_out:
                        a_out.write(base64.b64decode(a['raw']))

                print()


if __name__ == '__main__':
    main()

{'fraction_non_alphanumeric': 0.12008005336891261, 'fraction_numerical': 0.0063375583722481655, 'mean_word_length': 2.3433667781493868, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5515708', 'n_tokens_mistral': 911, 'n_tokens_neox': 856, 'n_words': 242}