Skip to content

Instantly share code, notes, and snippets.

@Dobby233Liu
Forked from urschrei/parseml.py
Last active September 15, 2023 13:01
Show Gist options
  • Save Dobby233Liu/98574b5eb5e927e0cbef652557162004 to your computer and use it in GitHub Desktop.
Save Dobby233Liu/98574b5eb5e927e0cbef652557162004 to your computer and use it in GitHub Desktop.
(Attempt to) extract (everything) from EML files (specified in command args), and write them to (a well-named) output subdir
#!/usr/bin/env python
"""
2020 update:
- More iterators, fewer lists
- Python 3 compatible
- Processes files in parallel
(one thread per CPU, but that's not really how it works)
hacked to dump everything
"""
import os
import email
from multiprocessing import Pool
import mimetypes
import sys
import traceback
def printe(*args, **kwargs):
if not kwargs.get("file"):
kwargs["file"] = sys.stderr
return print(*args, **kwargs)
def extract(filename):
if not os.path.exists(filename):
printe(f"File {filename} does not exist!")
return 0, 0
# ensure that an output dir exists
od = "output_" + os.path.splitext(os.path.basename(filename))[0]
os.path.exists(od) or os.makedirs(od)
output_count = 0
with open(filename, "r") as f:
msg = email.message_from_file(f)
for attachment in msg.walk():
if attachment.is_multipart(): continue
try:
output_filename = attachment.get_filename() or hex(hash(attachment))
if not output_filename:
printe("Couldn't make a name for a part of %s. Skipping." % filename)
continue
output_filename = output_filename.replace("/", "_")
if os.path.splitext(output_filename)[1] == "":
ext = mimetypes.guess_extension(attachment.get_content_type())
if ext:
output_filename = output_filename + ext
except Exception:
printe("Couldn't make a name for a part of %s. Skipping." % filename)
traceback.print_exc()
continue
with open(os.path.join(od, output_filename), "wb") as of:
payload = attachment.get_payload(decode=True)
if not payload:
payload = attachment.get_content().encode(attachment.get_content_charset())
if not payload:
printe("Couldn't get payload for %s" % output_filename)
continue
of.write(payload)
output_count += 1
if output_count > 0:
print(f"{filename} -> {od}")
else:
printe("No attachment found for file %s!" % filename)
return output_count > 0 and 1 or 0, output_count
if __name__ == "__main__":
files = sys.argv[1:]
if len(files) == 0:
printe("No input files specified")
exit(1)
pool = Pool()
res = pool.map(extract, files)
pool.close()
pool.join()
done_count, out_file_count = (sum(i) for i in zip(*res))
print("Done: Processed {} files with {} attachments.".format(done_count, out_file_count))
if done_count < len(files): exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment