Added function to analyse mails and extracted helper functions, calculate city from coordinates
This commit is contained in:
parent
e3e6dc43d0
commit
381ccaf46e
56
helpers.py
Normal file
56
helpers.py
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
REGEX = r"SDS_ILS_BEGINN(#D#Leitstelle:\s+)(?P<number>\d*)(?P<sonderrechte>m|o|i)?(?P<fzg>RTW|NAW|KTW)? (?P<keyword>Trauma Gesicht/|Atemnot Akut|Trauma Extremit|Abdomen Akut|Person droht zu spr|Hilflose Person|\S*|) (?P<address>\D*)(.*) \$GPSN(.*)E(.*)SDS_ILS_ENDE"
|
||||||
|
|
||||||
|
import os
|
||||||
|
import PyPDF2
|
||||||
|
import re
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
from geopy.geocoders import Nominatim
|
||||||
|
|
||||||
|
load_dotenv(override=True)
|
||||||
|
|
||||||
|
DRAEGER_API_KEY = os.environ.get("DRAEGER_API_KEY", default=None)
|
||||||
|
|
||||||
|
def get_city(lat, lon):
|
||||||
|
app = Nominatim(user_agent="Test")
|
||||||
|
location = app.reverse(query="N" + lat[:2] + '.' + lat[2:] + ", E" + lon[:2] + '.' + lon[2:], namedetails=True)
|
||||||
|
|
||||||
|
return [location.raw.get("address").get("postcode"), location.raw.get("address").get("city"), location.raw.get("address").get("city_district")]
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
def get_draeger_json(text):
|
||||||
|
match = re.findall(REGEX, text.replace("\n", ""))
|
||||||
|
|
||||||
|
if (len(match) != 0):
|
||||||
|
data = {}
|
||||||
|
data["apiKey"] = DRAEGER_API_KEY
|
||||||
|
|
||||||
|
if(match[0][1].startswith("9")): # 9 => RD (First Rsponder)
|
||||||
|
data["alertingKeyword"] = "Wachalarm"
|
||||||
|
else:
|
||||||
|
data["alertingKeyword"] = match[0][4]
|
||||||
|
data["alertingKeywordText"] = match[0][1] + " " + match[0][4]
|
||||||
|
data["street"] = match[0][5]
|
||||||
|
data["houseNumber"] = match[0][6]
|
||||||
|
|
||||||
|
try:
|
||||||
|
city = get_city(match[0][7], match[0][8])
|
||||||
|
data["city"] = city[0] + " " + city[1]
|
||||||
|
data["cityDistrict"] = city[2]
|
||||||
|
except Exception as e:
|
||||||
|
print(f'Fehler: {e}')
|
||||||
|
|
||||||
|
data["lat"] = match[0][7][:2] + '.' + match[0][7][2:]
|
||||||
|
data["lon"] = match[0][8][:2] + '.' + match[0][8][2:]
|
||||||
|
|
||||||
|
print(data)
|
||||||
|
return data
|
||||||
|
else:
|
||||||
|
return None
|
62
mail_analyzer.py
Normal file
62
mail_analyzer.py
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
from dotenv import load_dotenv
|
||||||
|
import imaplib
|
||||||
|
import email
|
||||||
|
from email.header import decode_header
|
||||||
|
import os
|
||||||
|
|
||||||
|
from helpers import get_draeger_json, read_pdf
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
def check_email():
|
||||||
|
try:
|
||||||
|
mail = imaplib.IMAP4_SSL(IMAP_SERVER)
|
||||||
|
mail.login(EMAIL_ACCOUNT, EMAIL_PASSWORD)
|
||||||
|
mail.select(MAILBOX)
|
||||||
|
|
||||||
|
status, messages = mail.search(None, '(SUBJECT "Nachricht von DLRG Stuttgart")')
|
||||||
|
|
||||||
|
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]
|
||||||
|
|
||||||
|
if isinstance(subject, bytes):
|
||||||
|
subject = subject.decode(encoding if encoding else 'utf-8')
|
||||||
|
|
||||||
|
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).replace("\n", "")
|
||||||
|
|
||||||
|
print(get_draeger_json(pdf_text))
|
||||||
|
|
||||||
|
mail.logout()
|
||||||
|
except Exception as e:
|
||||||
|
print(f'Fehler: {e}')
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
check_email()
|
77
main.py
77
main.py
@ -1,14 +1,13 @@
|
|||||||
REGEX = r'SDS_ILS_BEGINN(#D#Leitstelle:\s+)(.*)(m|o|i)(RTW|NAW|KTW) (.*) \$GPSN(.*)E(.*)SDS_ILS_ENDE'
|
REGEX = r'SDS_ILS_BEGINN(#D#Leitstelle:\s+)(.*)(m|o|i)(RTW|NAW|KTW) (.*) \$GPSN(.*)E(.*)SDS_ILS_ENDE'
|
||||||
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
import re
|
|
||||||
import requests
|
import requests
|
||||||
import imaplib
|
import imaplib
|
||||||
import email
|
import email
|
||||||
from email.header import decode_header
|
from email.header import decode_header
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
import PyPDF2
|
from helpers import get_draeger_json, read_pdf
|
||||||
|
|
||||||
load_dotenv(override=True)
|
load_dotenv(override=True)
|
||||||
|
|
||||||
@ -20,28 +19,6 @@ MAILBOX = os.environ.get("MAILBOX", default=None)
|
|||||||
CHECK_INTERVAL = int(os.environ.get("CHECK_INTERVAL", default=None)) # in Sekunden
|
CHECK_INTERVAL = int(os.environ.get("CHECK_INTERVAL", default=None)) # in Sekunden
|
||||||
SAVE_FOLDER = os.environ.get("SAVE_FOLDER", default=None)
|
SAVE_FOLDER = os.environ.get("SAVE_FOLDER", default=None)
|
||||||
DRAEGER_API_KEY = os.environ.get("DRAEGER_API_KEY", default=None)
|
DRAEGER_API_KEY = os.environ.get("DRAEGER_API_KEY", default=None)
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
def get_draeger_json(text):
|
|
||||||
match = re.findall(REGEX, text.replace("\n", ""))
|
|
||||||
|
|
||||||
data = {}
|
|
||||||
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:]
|
|
||||||
|
|
||||||
print(data)
|
|
||||||
return data
|
|
||||||
|
|
||||||
def check_email():
|
def check_email():
|
||||||
try:
|
try:
|
||||||
@ -49,7 +26,7 @@ def check_email():
|
|||||||
mail.login(EMAIL_ACCOUNT, EMAIL_PASSWORD)
|
mail.login(EMAIL_ACCOUNT, EMAIL_PASSWORD)
|
||||||
mail.select(MAILBOX)
|
mail.select(MAILBOX)
|
||||||
|
|
||||||
status, messages = mail.search(None, 'UNSEEN')
|
status, messages = mail.search(None, 'UNSEEN SUBJECT "Nachricht von DLRG Stuttgart"')
|
||||||
|
|
||||||
mail_ids = messages[0].split()
|
mail_ids = messages[0].split()
|
||||||
|
|
||||||
@ -61,33 +38,31 @@ def check_email():
|
|||||||
msg = email.message_from_bytes(response_part[1])
|
msg = email.message_from_bytes(response_part[1])
|
||||||
subject, encoding = decode_header(msg["Subject"])[0]
|
subject, encoding = decode_header(msg["Subject"])[0]
|
||||||
|
|
||||||
print(subject)
|
if isinstance(subject, bytes):
|
||||||
if (subject == "Nachricht von DLRG Stuttgart"): # Only Mails from Handyalarm
|
subject = subject.decode(encoding if encoding else 'utf-8')
|
||||||
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
|
||||||
|
|
||||||
from_ = msg.get("From")
|
filename = part.get_filename()
|
||||||
print(f'Neue E-Mail von {from_} mit Betreff: {subject}')
|
if filename:
|
||||||
|
filepath = os.path.join(SAVE_FOLDER, filename)
|
||||||
for part in msg.walk():
|
with open(filepath, 'wb') as f:
|
||||||
if part.get_content_maintype() == 'multipart':
|
f.write(part.get_payload(decode=True))
|
||||||
continue
|
|
||||||
if part.get('Content-Disposition') is None:
|
if filename.endswith('.pdf'):
|
||||||
continue
|
pdf_text = read_pdf(filepath)
|
||||||
|
|
||||||
filename = part.get_filename()
|
r = requests.post('https://einsatzmeldesystem.de/ems/inbound/deployment/universal/', json=get_draeger_json(pdf_text))
|
||||||
if filename:
|
|
||||||
filepath = os.path.join(SAVE_FOLDER, filename)
|
print(r.status_code)
|
||||||
with open(filepath, 'wb') as f:
|
print(r.json())
|
||||||
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())
|
|
||||||
mail.logout()
|
mail.logout()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f'Fehler: {e}')
|
print(f'Fehler: {e}')
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
python-dotenv
|
python-dotenv
|
||||||
PyPDF2
|
PyPDF2
|
||||||
requests
|
requests
|
||||||
|
geopy
|
Loading…
Reference in New Issue
Block a user