Draeger-Handyalarm-Bridge/main.py

101 lines
3.8 KiB
Python
Raw Normal View History

2024-07-07 08:02:01 +00:00
REGEX = r'SDS_ILS_BEGINN(#D#Leitstelle:\s+)(.*)(m|o|i)(RTW|NAW|KTW) (.*) \$GPSN(.*)E(.*)SDS_ILS_ENDE'
from dotenv import load_dotenv
import re
2024-07-07 08:22:18 +00:00
import requests
2024-07-07 08:02:01 +00:00
import imaplib
import email
from email.header import decode_header
import os
import time
import PyPDF2
load_dotenv(override=True)
# Konfigurationsparameter
IMAP_SERVER = os.environ.get("IMAP_SERVER", default=None)
EMAIL_ACCOUNT = os.environ.get("EMAIL_ACCOUNT", default=None)
EMAIL_PASSWORD = os.environ.get("EMAIL_PASSWORD", default=None)
MAILBOX = os.environ.get("MAILBOX", default=None)
CHECK_INTERVAL = int(os.environ.get("CHECK_INTERVAL", default=None)) # in Sekunden
SAVE_FOLDER = os.environ.get("SAVE_FOLDER", default=None)
2024-07-07 08:22:18 +00:00
DRAEGER_API_KEY = os.environ.get("DRAEGER_API_KEY", default=None)
2024-07-07 08:02:01 +00:00
def read_pdf(file_path):
with open(file_path, 'rb') as file:
reader = PyPDF2.PdfReader(file)
text = ''
for page in reader.pages:
text += page.extract_text()
return text
2024-07-07 08:22:18 +00:00
def get_draeger_json(text):
2024-07-07 08:02:01 +00:00
match = re.findall(REGEX, text.replace("\n", ""))
data = {}
2024-07-07 08:22:18 +00:00
data["apiKey"] = DRAEGER_API_KEY
data["alertingKeyword"] = "Wachalarm"
data["alertingKeywordText"] = match[0][1] + " " + match[0][4]
data["lat"] = match[0][5][:2] + '.' + match[0][5][2:]
data["lon"] = match[0][6][:2] + '.' + match[0][6][2:]
2024-07-07 08:02:01 +00:00
2024-07-07 08:22:18 +00:00
print(data)
return data
2024-07-07 08:02:01 +00:00
def check_email():
try:
mail = imaplib.IMAP4_SSL(IMAP_SERVER)
mail.login(EMAIL_ACCOUNT, EMAIL_PASSWORD)
mail.select(MAILBOX)
status, messages = mail.search(None, 'UNSEEN')
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:
if isinstance(response_part, tuple):
msg = email.message_from_bytes(response_part[1])
subject, encoding = decode_header(msg["Subject"])[0]
print(subject)
if (subject == "Nachricht von DLRG Stuttgart"): # Only Mails from Handyalarm
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)
2024-07-07 08:22:18 +00:00
r = requests.post('https://einsatzmeldesystem.de/ems/inbound/deployment/universal/', json=get_draeger_json(pdf_text))
print(r.status_code)
print(r.json())
2024-07-07 08:02:01 +00:00
mail.logout()
except Exception as e:
print(f'Fehler: {e}')
if __name__ == "__main__":
# send_email("Test", "Test")
while True:
print("Loop")
check_email()
time.sleep(CHECK_INTERVAL)