nog.infio.mirror/src/Mail/thunderbird.py

64 lines
1.9 KiB
Python
Raw Normal View History

2019-07-30 14:32:09 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import mailbox
import email.header
import email.message
2019-07-30 15:52:35 +02:00
from parser.Bahn import Bahn
2019-07-30 19:53:39 +02:00
from parser.ICSC import ICSC
2019-07-30 15:52:35 +02:00
2019-07-30 14:32:09 +02:00
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'])
2019-07-30 15:52:35 +02:00
mContent = getbody(message)
2019-07-30 17:15:16 +02:00
#Hier muss noch das Date ergänzt werden
2019-07-30 15:52:35 +02:00
2019-07-30 19:53:39 +02:00
#Dieser Block lädt den Bahn Parser und dient als Dummy. Er wird immer weiter kopiert und um zusätzliche Sender ergänzt
2019-07-30 15:52:35 +02:00
if mFrom == 'BahnCard-Service <noreply.bahncard-service@bahn.de>':
events = Bahn.getEvents(mContent)
2019-07-30 19:53:39 +02:00
print(events)
#der folgende Block ist auf das ICSC abgestimmt
if mFrom == 'International Civil Society Centre <communications@icscentre.org>':
events = ICSC.getEvents(mContent)
2019-07-30 17:15:16 +02:00
print(events)