Added healtcheck "down" requests, send empty draeger api request if data not parseable
This commit is contained in:
parent
4ea63d5623
commit
f36f327bf1
80
main.py
80
main.py
@ -29,50 +29,62 @@ def check_email():
|
||||
|
||||
status, messages = mail.search(None, 'UNSEEN SUBJECT "Nachricht von DLRG Stuttgart"')
|
||||
|
||||
mail_ids = messages[0].split()
|
||||
|
||||
for mail_id in mail_ids:
|
||||
status, msg_data = mail.fetch(mail_id, '(RFC822)')
|
||||
try:
|
||||
mail_ids = messages[0].split()
|
||||
|
||||
for response_part in msg_data:
|
||||
if isinstance(response_part, tuple):
|
||||
msg = email.message_from_bytes(response_part[1])
|
||||
subject, encoding = decode_header(msg["Subject"])[0]
|
||||
|
||||
if isinstance(subject, bytes):
|
||||
subject = subject.decode(encoding if encoding else 'utf-8')
|
||||
|
||||
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
|
||||
for mail_id in mail_ids:
|
||||
status, msg_data = mail.fetch(mail_id, '(RFC822)')
|
||||
|
||||
for response_part in msg_data:
|
||||
if isinstance(response_part, tuple):
|
||||
msg = email.message_from_bytes(response_part[1])
|
||||
subject, encoding = decode_header(msg["Subject"])[0]
|
||||
|
||||
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())
|
||||
if isinstance(subject, bytes):
|
||||
subject = subject.decode(encoding if encoding else 'utf-8')
|
||||
|
||||
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 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()
|
||||
except Exception as 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__":
|
||||
while True:
|
||||
check_email()
|
||||
|
||||
r = requests.get(HEALTHCHECK_URL)
|
||||
r = requests.get(HEALTHCHECK_URL + "?status=up&msg=OK&ping=")
|
||||
print(r.status_code)
|
||||
|
||||
time.sleep(CHECK_INTERVAL)
|
||||
|
Loading…
Reference in New Issue
Block a user