process einsatzrapport from webdav inbox
This commit is contained in:
parent
f9b86f3c8f
commit
63482d5f2e
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -3,3 +3,4 @@ __pycache__/
|
||||||
.env
|
.env
|
||||||
pylokid/temp_test.py
|
pylokid/temp_test.py
|
||||||
test.py
|
test.py
|
||||||
|
tmp/
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
|
import re
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import logging
|
import logging
|
||||||
import asyncio
|
import asyncio
|
||||||
|
@ -11,7 +12,7 @@ import aioeasywebdav
|
||||||
|
|
||||||
|
|
||||||
class WebDav:
|
class WebDav:
|
||||||
""" WebDav Client """
|
"""WebDav Client"""
|
||||||
|
|
||||||
def __init__(self, url, username, password, webdav_basedir, tmp_dir):
|
def __init__(self, url, username, password, webdav_basedir, tmp_dir):
|
||||||
self.logger = logging.getLogger(__name__)
|
self.logger = logging.getLogger(__name__)
|
||||||
|
@ -32,7 +33,7 @@ class WebDav:
|
||||||
self.logger.info("WebDAV connection successfull")
|
self.logger.info("WebDAV connection successfull")
|
||||||
|
|
||||||
def upload(self, file_name, f_id, check_exists=True):
|
def upload(self, file_name, f_id, check_exists=True):
|
||||||
""" uploads a file to webdav - checks for existence before doing so """
|
"""uploads a file to webdav - checks for existence before doing so"""
|
||||||
|
|
||||||
# upload with webdav
|
# upload with webdav
|
||||||
if f_id == None:
|
if f_id == None:
|
||||||
|
@ -63,8 +64,12 @@ class WebDav:
|
||||||
)
|
)
|
||||||
self.logger.info('[%s] File "%s" uploaded', f_id, file_name)
|
self.logger.info('[%s] File "%s" uploaded', f_id, file_name)
|
||||||
|
|
||||||
|
def delete(self, file_name):
|
||||||
|
"""delete file on webdav"""
|
||||||
|
self.loop.run_until_complete(self.webdav.delete(file_name))
|
||||||
|
|
||||||
def einsatz_exists(self, f_id):
|
def einsatz_exists(self, f_id):
|
||||||
""" check if an einsatz is already created """
|
"""check if an einsatz is already created"""
|
||||||
|
|
||||||
remote_upload_dir = (
|
remote_upload_dir = (
|
||||||
self.webdav_basedir + "/" + str(datetime.now().year) + "/" + f_id
|
self.webdav_basedir + "/" + str(datetime.now().year) + "/" + f_id
|
||||||
|
@ -75,8 +80,33 @@ class WebDav:
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def einsatzrapport_inbox_check(self, tmp_dir):
|
||||||
|
"""check if an einsatzrapport with an f_id exists in the WebDav Inbox and process it"""
|
||||||
|
|
||||||
|
rapporte_to_process = []
|
||||||
|
|
||||||
|
filelist = self.loop.run_until_complete(
|
||||||
|
self.webdav.ls(f"{self.webdav_basedir}/Inbox")
|
||||||
|
)
|
||||||
|
for file in filelist:
|
||||||
|
full_path = file[0]
|
||||||
|
parsed = re.search(".*Einsatzrapport_(F[0-9].*)\.pdf", full_path)
|
||||||
|
if parsed:
|
||||||
|
f_id = parsed.group(1)
|
||||||
|
self.logger.info("[%s] Found %s - Downloading", f_id, full_path)
|
||||||
|
|
||||||
|
# Download PDF for later processing
|
||||||
|
self.loop.run_until_complete(
|
||||||
|
self.webdav.download(
|
||||||
|
full_path, f"{tmp_dir}/Einsatzrapport_{f_id}.pdf"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
rapporte_to_process.append(f_id)
|
||||||
|
|
||||||
|
return rapporte_to_process
|
||||||
|
|
||||||
def store_data(self, f_id, file_name, data):
|
def store_data(self, f_id, file_name, data):
|
||||||
""" stores data on webdav """
|
"""stores data on webdav"""
|
||||||
|
|
||||||
file_path = os.path.join(self.tmp_dir, file_name)
|
file_path = os.path.join(self.tmp_dir, file_name)
|
||||||
|
|
||||||
|
@ -88,7 +118,7 @@ class WebDav:
|
||||||
self.upload(file_name, f_id, False)
|
self.upload(file_name, f_id, False)
|
||||||
|
|
||||||
def get_lodur_data(self, f_id, filetype="_lodur.json"):
|
def get_lodur_data(self, f_id, filetype="_lodur.json"):
|
||||||
""" gets lodur data if it exists """
|
"""gets lodur data if it exists"""
|
||||||
|
|
||||||
file_name = f_id + filetype
|
file_name = f_id + filetype
|
||||||
file_path = os.path.join(self.tmp_dir, file_name)
|
file_path = os.path.join(self.tmp_dir, file_name)
|
||||||
|
|
|
@ -260,7 +260,38 @@ def main():
|
||||||
else:
|
else:
|
||||||
logger.error("[%s] Unknown type: %s", f_id, f_type)
|
logger.error("[%s] Unknown type: %s", f_id, f_type)
|
||||||
|
|
||||||
# TODO check webdav for Einsatzrapport PDF with f_id in filename and then process it
|
logger.info("Checking WebDav Inbox folder for Einsatzrapporte to process")
|
||||||
|
rapporte_to_process = webdav_client.einsatzrapport_inbox_check(TMP_DIR)
|
||||||
|
|
||||||
|
if rapporte_to_process:
|
||||||
|
for f_id_rapport in rapporte_to_process:
|
||||||
|
filename = f"Einsatzrapport_{f_id_rapport}.pdf"
|
||||||
|
local_file = os.path.join(TMP_DIR, filename)
|
||||||
|
# Upload to f_id folder
|
||||||
|
webdav_client.upload(filename, f_id_rapport)
|
||||||
|
|
||||||
|
# Process it for Lodur
|
||||||
|
lodur_id = webdav_client.get_lodur_data(f_id_rapport)["event_id"]
|
||||||
|
# Retrieve Lodur data again and store it in Webdav
|
||||||
|
lodur_data = lodur_client.retrieve_form_data(lodur_id)
|
||||||
|
webdav_client.store_data(
|
||||||
|
f_id_rapport, f_id_rapport + "_lodur.json", lodur_data
|
||||||
|
)
|
||||||
|
lodur_client.einsatzrapport_scan(
|
||||||
|
f_id_rapport,
|
||||||
|
lodur_data,
|
||||||
|
local_file,
|
||||||
|
webdav_client,
|
||||||
|
)
|
||||||
|
# Delete processed Einsatzrapport from Inbox and local temp dir
|
||||||
|
logger.info("Einsatzrapport processed - deleting file in Inbox")
|
||||||
|
webdav_client.delete(f"{WEBDAV_BASEDIR}/Inbox/{filename}")
|
||||||
|
os.remove(local_file)
|
||||||
|
|
||||||
|
pushover.send_message(
|
||||||
|
f"Einsatzrapport {f_id_rapport} wurde bearbeitet.",
|
||||||
|
title=f"Feuerwehr Einsatzrapport bearbeitet - {f_id_rapport}",
|
||||||
|
)
|
||||||
|
|
||||||
# send heartbeat
|
# send heartbeat
|
||||||
requests.get(HEARTBEAT_URL)
|
requests.get(HEARTBEAT_URL)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[tool.poetry]
|
[tool.poetry]
|
||||||
name = "pylokid"
|
name = "pylokid"
|
||||||
version = "3.1.5"
|
version = "3.2.0"
|
||||||
description = ""
|
description = ""
|
||||||
authors = ["Tobias Brunner <tobias@tobru.ch>"]
|
authors = ["Tobias Brunner <tobias@tobru.ch>"]
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
Loading…
Reference in a new issue