95 lines
2.3 KiB
Python
Executable File
95 lines
2.3 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import re
|
|
from datetime import datetime
|
|
from helper import Config
|
|
from helper import PaperlessAPI
|
|
|
|
|
|
|
|
|
|
# need to have this manually as setlocale() won't work in Paperless docker setup
|
|
MONTH_NAMES = [
|
|
"0-Index",
|
|
"Januar",
|
|
"Februar",
|
|
"März",
|
|
"April",
|
|
"Mai",
|
|
"Juni",
|
|
"Juli",
|
|
"August",
|
|
"September",
|
|
"Oktober",
|
|
"November",
|
|
"Dezember"
|
|
]
|
|
|
|
#""
|
|
# expand the string by the provided parts, but only if the input string doesn't contain those parts yet.
|
|
#
|
|
def expand_by_non_existing_parts(title, parts=[]) -> str:
|
|
if len(parts) == 0:
|
|
return title
|
|
|
|
for part in parts:
|
|
if str(part) not in title:
|
|
title += " " + str(part)
|
|
|
|
return title
|
|
|
|
|
|
if __name__ == "__main__":
|
|
cfg = Config()
|
|
|
|
print("START POST CONSUME")
|
|
print(cfg)
|
|
|
|
api = PaperlessAPI(cfg.get_paperless_api_url(), cfg.get_auth_token())
|
|
|
|
document_id = os.environ["DOCUMENT_ID"]
|
|
document = api.get_document_by_id(document_id)
|
|
orig_filename = document["original_file_name"]
|
|
|
|
title = document["title"]
|
|
|
|
pattern = re.compile(r'^(\d{8})(\s\W\s|\s)(.*)')
|
|
findings = pattern.match(title)
|
|
if findings:
|
|
extracted_new_title = findings.group(3)
|
|
print(f'Extracted title="{extracted_new_title}"')
|
|
|
|
# should add Month Year suffix to title?
|
|
if cfg.expand_title_by_date():
|
|
print("add suffix")
|
|
date_string = findings.group(1)
|
|
print(f"title date string={date_string}")
|
|
d=datetime.strptime(date_string, "%Y%m%d")
|
|
|
|
print(f"parsed date={d}")
|
|
suffix = f'{MONTH_NAMES[d.month]} {d.year}'
|
|
print(f"created suffix={suffix}")
|
|
extracted_new_title = expand_by_non_existing_parts(extracted_new_title, [MONTH_NAMES[d.month], d.year])
|
|
|
|
tag_id = api.get_tag_id_by_name(cfg.get_notify_tag())
|
|
print(tag_id)
|
|
if tag_id is None:
|
|
data = {"title": extracted_new_title}
|
|
else:
|
|
document["tags"].append(tag_id)
|
|
data = {"title": extracted_new_title, "tags": document["tags"]}
|
|
|
|
response = api.patch_document(document_id, data)
|
|
|
|
print(f'Status Code = {response.status_code}')
|
|
response.raise_for_status()
|
|
else:
|
|
print("Nothing changed")
|
|
|
|
|
|
print("END POST CONSUME")
|
|
|
|
|
|
|