mailparser, makes directories and parses MIME to files!

This commit is contained in:
Al-P 2019-08-03 19:26:30 +02:00
parent e52e010f75
commit 57a3c87979
2 changed files with 72 additions and 2 deletions

View File

@ -12,6 +12,7 @@ A parser to get events of ICSC out of their e-mails
'''
class ICSC:
'''
@param input must be string with html
@return all events as list
@ -20,8 +21,10 @@ class ICSC:
def getEvents(html):
events = []
soup = BeautifulSoup(html, 'lxml')
event = soup
print(soup).text
event = soup.find("body").text
event = event.split("Upcoming Events")[1]
event = event.split("International Civil Society Centre Agricolastraße 26, 10555 Berlin, Germany +49 (0) 30 20 62 46 97 11 www.icscentre.org")[0]
print(event)
#Am Ende soll alles auf der Variable events liegen
print(events)

View File

@ -0,0 +1,67 @@
#!/usr/bin/env python3
"""Unpack a MIME message into a directory of files."""
import os
import email
import mimetypes
from email.policy import default
from argparse import ArgumentParser
def main():
# parser = ArgumentParser(description="""\
# Unpack a MIME message into a directory of files.
# """)
# parser.add_argument('-d', 'directory', required=True,
# help="""Unpack the MIME message into the named
# directory, which will be created if it doesn't already
# exist.""")
# parser.add_argument('msgfile')
# args = parser.parse_args()
''' All of the above is commented out because not necessary in this case'''
with open("mysourcedirectory/mymailfilename", 'rb') as fp:
#Insert here your sourcedirectory and the file name of the file to parse
msg = email.message_from_binary_file(fp, policy=default)
try:
os.mkdir("mybeautifulmaildirectory")
#insert here name of output path
except FileExistsError:
pass
# with open(args.msgfile, 'rb') as fp:
# msg = email.message_from_binary_file(fp, policy=default)
# try:
# os.mkdir(args.directory)
# except FileExistsError:
# pass
counter = 1
for part in msg.walk():
# multipart/* are just containers
if part.get_content_maintype() == 'multipart':
continue
# Applications should really sanitize the given filename so that an
# email message can't be used to overwrite important files
filename = part.get_filename()
if not filename:
ext = mimetypes.guess_extension(part.get_content_type())
if not ext:
# Use a generic bag-of-bits extension
ext = '.bin'
filename = 'part-%03d%s' % (counter, ext)
counter += 1
with open(os.path.join("/mybeautifulcleanmaildirectory", filename), 'wb') as fp:
#insert output path above
fp.write(part.get_payload(decode=True))
if __name__ == '__main__':
main()