Refactor after feedback:

- rename PAPERLESS_TIKA to PAPERLESS_TIKA_ENABLED
- all other env params now start with PAPERLESS_TIKA
- convert_to_pdf as class instance method
- smaller details

Signed-off-by: Jo Vandeginste <Jo.Vandeginste@kuleuven.be>
This commit is contained in:
Jo Vandeginste
2020-12-31 14:41:47 +01:00
parent b8e8bf3dd4
commit 5236f4e58d
7 changed files with 46 additions and 49 deletions

View File

@@ -9,6 +9,6 @@ class PaperlessTikaConfig(AppConfig):
def ready(self):
from documents.signals import document_consumer_declaration
if settings.PAPERLESS_TIKA:
if settings.PAPERLESS_TIKA_ENABLED:
document_consumer_declaration.connect(tika_consumer_declaration)
AppConfig.ready(self)

View File

@@ -70,49 +70,46 @@ class TikaDocumentParser(DocumentParser):
def parse(self, document_path, mime_type):
self.log("info", f"[TIKA_PARSE] Sending {document_path} to Tika server")
tika_server = settings.PAPERLESS_TIKA_ENDPOINT
try:
parsed = parser.from_file(document_path)
parsed = parser.from_file(document_path, tika_server)
except requests.exceptions.HTTPError as err:
raise ParseError(f"Could not parse {document_path} with tika server: {err}")
try:
content = parsed["content"].strip()
except:
content = ""
try:
creation_date = dateutil.parser.isoparse(
parsed["metadata"]["Creation-Date"]
raise ParseError(
f"Could not parse {document_path} with tika server at {tika_server}: {err}"
)
try:
self.text = parsed["content"].strip()
except:
creation_date = None
pass
try:
self.date = dateutil.parser.isoparse(parsed["metadata"]["Creation-Date"])
except:
pass
archive_path = os.path.join(self.tempdir, "convert.pdf")
convert_to_pdf(self, document_path, archive_path)
convert_to_pdf(document_path, archive_path)
self.archive_path = archive_path
self.date = creation_date
self.text = content
def convert_to_pdf(document_path, pdf_path):
pdf_path = os.path.join(self.tempdir, "convert.pdf")
gotenberg_server = settings.PAPERLESS_TIKA_GOTENBERG_ENDPOINT
url = gotenberg_server + "/convert/office"
def convert_to_pdf(self, document_path, pdf_path):
pdf_path = os.path.join(self.tempdir, "convert.pdf")
gotenberg_server = settings.GOTENBERG_SERVER_ENDPOINT
url = gotenberg_server + "/convert/office"
self.log("info", f"[TIKA] Converting {document_path} to PDF as {pdf_path}")
files = {"files": open(document_path, "rb")}
headers = {}
self.log("info", f"[TIKA] Converting {document_path} to PDF as {pdf_path}")
files = {"files": open(document_path, "rb")}
headers = {}
try:
response = requests.post(url, files=files, headers=headers)
response.raise_for_status() # ensure we notice bad responses
except requests.exceptions.HTTPError as err:
raise ParseError(
f"Could not contact gotenberg server at {gotenberg_server}: {err}"
)
try:
response = requests.post(url, files=files, headers=headers)
response.raise_for_status() # ensure we notice bad responses
except requests.exceptions.HTTPError as err:
raise ParseError(
f"Could not contact gotenberg server at {gotenberg_server}: {err}"
)
file = open(pdf_path, "wb")
file.write(response.content)
file.close()
file = open(pdf_path, "wb")
file.write(response.content)
file.close()

View File

@@ -1,3 +0,0 @@
import magic
m = magic.from_file("/nfsstorage/jo/syncthing/Documenten/20R-309.153.052.pdf")
print(m)