Added healtcheck "down" requests, send empty draeger api request if data not parseable

This commit is contained in:
Florian Kaiser 2024-07-15 12:37:00 +02:00
parent 4ea63d5623
commit f36f327bf1

80
main.py
View File

@ -29,50 +29,62 @@ def check_email():
status, messages = mail.search(None, 'UNSEEN SUBJECT "Nachricht von DLRG Stuttgart"') status, messages = mail.search(None, 'UNSEEN SUBJECT "Nachricht von DLRG Stuttgart"')
mail_ids = messages[0].split() try:
mail_ids = messages[0].split()
for mail_id in mail_ids:
status, msg_data = mail.fetch(mail_id, '(RFC822)')
for response_part in msg_data: for mail_id in mail_ids:
if isinstance(response_part, tuple): status, msg_data = mail.fetch(mail_id, '(RFC822)')
msg = email.message_from_bytes(response_part[1])
subject, encoding = decode_header(msg["Subject"])[0] for response_part in msg_data:
if isinstance(response_part, tuple):
if isinstance(subject, bytes): msg = email.message_from_bytes(response_part[1])
subject = subject.decode(encoding if encoding else 'utf-8') subject, encoding = decode_header(msg["Subject"])[0]
from_ = msg.get("From")
print(f'Neue E-Mail von {from_} mit Betreff: {subject}')
for part in msg.walk():
if part.get_content_maintype() == 'multipart':
continue
if part.get('Content-Disposition') is None:
continue
filename = part.get_filename() if isinstance(subject, bytes):
if filename: subject = subject.decode(encoding if encoding else 'utf-8')
filepath = os.path.join(SAVE_FOLDER, filename)
with open(filepath, 'wb') as f: from_ = msg.get("From")
f.write(part.get_payload(decode=True)) print(f'Neue E-Mail von {from_} mit Betreff: {subject}')
if filename.endswith('.pdf'): for part in msg.walk():
pdf_text = read_pdf(filepath) if part.get_content_maintype() == 'multipart':
continue
r = requests.post('https://einsatzmeldesystem.de/ems/inbound/deployment/universal/', json=get_draeger_json(pdf_text)) if part.get('Content-Disposition') is None:
continue
print(r.status_code)
print(r.json()) filename = part.get_filename()
if filename:
filepath = os.path.join(SAVE_FOLDER, filename)
with open(filepath, 'wb') as f:
f.write(part.get_payload(decode=True))
if filename.endswith('.pdf'):
pdf_text = read_pdf(filepath)
r = requests.post('https://einsatzmeldesystem.de/ems/inbound/deployment/universal/', json=get_draeger_json(pdf_text))
print(r.status_code)
print(r.json())
except Exception as e:
data = {}
data["apiKey"] = DRAEGER_API_KEY
r = requests.post('https://einsatzmeldesystem.de/ems/inbound/deployment/universal/', json=data)
r = requests.get(HEALTHCHECK_URL + "?status=down&msg=Can not parse data. Alarming with empty data&ping=")
print(r.status_code)
mail.logout() mail.logout()
except Exception as e: except Exception as e:
print(f'Fehler: {e}') print(f'Fehler: {e}')
r = requests.get(HEALTHCHECK_URL + "?status=down&msg=Can not connect to mail server&ping=")
print(r.status_code)
if __name__ == "__main__": if __name__ == "__main__":
while True: while True:
check_email() check_email()
r = requests.get(HEALTHCHECK_URL) r = requests.get(HEALTHCHECK_URL + "?status=up&msg=OK&ping=")
print(r.status_code) print(r.status_code)
time.sleep(CHECK_INTERVAL) time.sleep(CHECK_INTERVAL)