[email] Add MDA script to setup.py

This commit is contained in:
maike 2020-07-05 23:42:07 +02:00 committed by dl6tom
parent a9c9f4f1c0
commit e24a0f8738
2 changed files with 30 additions and 29 deletions

View file

@ -10,37 +10,37 @@ from logging import getLogger
from kibicara.platforms.email.model import Email from kibicara.platforms.email.model import Email
logger = getLogger(__name__) def main():
logger = getLogger(__name__)
# read mail from STDIN
mailbytes = bytes(sys.stdin.read())
# read mail from STDIN # parse plaintext to email.EmailMessage object
mailbytes = bytes(sys.stdin.read()) myparser = email.parser.BytesParser()
mail = myparser.parsebytes(mailbytes)
# parse plaintext to email.EmailMessage object # extract relevant data from mail
myparser = email.parser.BytesParser() for part in mail.walk():
mail = myparser.parsebytes(mailbytes) try:
text = part.get_body(('plain',))
# extract relevant data from mail if not text:
for part in mail.walk(): text = re.sub(r'<[^>]*>', '', part.get_body(('html',)))
try: if not text:
text = part.get_body(('plain',)) logger.error('No suitable message body')
if not text: exit(0)
text = re.sub(r'<[^>]*>', '', part.get_body(('html',))) except Exception:
if not text: logger.info("No Body in this message part", exc_info=True)
logger.error('No suitable message body')
exit(0) exit(0)
except Exception: body = {
logger.info("No Body in this message part", exc_info=True) 'text': text,
exit(0) 'to': mail['To'].lower(),
body = { 'author': mail['From'].lower(),
'text': text, 'secret': Email.secret
'to': mail['To'].lower(), }
'author': mail['From'].lower(),
'secret': Email.secret
}
# POST request mit API-key und JSON-body an /api/email/messages/: # POST request mit API-key und JSON-body an /api/email/messages/:
print("curl " print("curl "
"-X POST http://localhost/api/email/messages/ " "-X POST http://localhost/api/email/messages/ "
"-H 'Content-Type: application/json' " "-H 'Content-Type: application/json' "
"-d " + json.dumps(body)) "-d " + json.dumps(body))

View file

@ -10,6 +10,7 @@ setup(
entry_points={ entry_points={
'console_scripts': [ 'console_scripts': [
'kibicara=kibicara.kibicara:Main', 'kibicara=kibicara.kibicara:Main',
'kibicara_mda=kibicara.kibicara_mda:main',
] ]
}, },
install_requires=[ install_requires=[