#!/usr/bin/env python # -*- coding: utf-8 -*- import sys import os import mailbox import email.header import email.message from parser.Bahn import Bahn from parser.ICSC import ICSC if 2 != len(sys.argv): print("Error. Please povide your mailbox file (mbox) as parameter with the script!") exit() mboxfile = sys.argv[1] ''' Email MIME based content converting https://stackoverflow.com/a/36716138 ''' def decode_mime_words(s): return u''.join( word.decode(encoding or 'utf8') if isinstance(word, bytes) else word for word, encoding in email.header.decode_header(s)) ''' Get content of email https://stackoverflow.com/a/31489271 ''' def getbody(message): #getting plain text 'email body' body = None if message.is_multipart(): for part in message.walk(): if part.is_multipart(): for subpart in part.walk(): if subpart.get_content_type() == 'text/plain': body = subpart.get_payload(decode=True) elif part.get_content_type() == 'text/plain': body = part.get_payload(decode=True) elif message.get_content_type() == 'text/plain': body = message.get_payload(decode=True) return body mbox = mailbox.mbox(mboxfile) for message in mbox: mFrom = decode_mime_words(message['from']) mSubject = decode_mime_words(message['subject']) mContent = getbody(message) #Hier muss noch das Date ergänzt werden # #Dieser Block lädt den Bahn Parser und dient als Dummy. Er wird immer weiter kopiert und um zusätzliche Sender ergänzt # if mFrom == 'BahnCard-Service ': # events = Bahn.getEvents(mContent) # print(events) #der folgende Block lädt den Parser ICSC if mFrom == 'International Civil Society Centre ': events = ICSC.getEvents(mContent) print(events)