# -*- coding: utf-8 -*-
__author__ = 'fremcode@gmail.com'
import os
import sys
import email
import errno
import mimetypes
PATH = os.path.abspath(os.path.dirname(sys.argv[0]))
ITEM_PATH = PATH + "\item"
PATH_DATA = os.path.abspath(os.path.dirname(sys.argv[0]))+"\data"
def item(eml_file,eml_name):
fp = open(eml_file)
html_name = eml_name.split('.')
html = html_name[0]+html_name[1]
msg = email.message_from_file(fp)
fp.close()
counter = 1
for part in msg.walk():
if part.get_content_maintype() == 'multipart':
continue
filename = part.get_filename()
#print filename
if not filename:
ext = mimetypes.guess_extension(part.get_content_type())
if not ext:
ext = '.bin'
filename = html+'part-%03d%s' % (counter, ext)
counter += 1
try:
fp = open(os.path.join(ITEM_PATH, filename), 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
except IOError:
pass
except TypeError:
pass
def main():
for root,dirs,files in os.walk(PATH_DATA):
for _dir in dirs:
if os.path.isdir("item"):
pass
else:
os.mkdir("_dir\html")
for _file in files:
eml_file = os.path.join(root, _file)
item(eml_file, _file)
if __name__ == '__main__':
main()