mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-03-31 13:35:08 -05:00
Page:
Post Consume Script Examples
Pages
Affiliated Projects
Backend Ideas List
Email OAuth App Setup
Home
Platform‐Specific Troubleshooting
Post Consume Script Examples
Pre Consume Script Examples
Related Projects
Scanner & Software Recommendations
Using Security Tools with Paperless ngx
Using a Reverse Proxy with Paperless ngx
Using and Generating ASN Barcodes
Clone
9
Post Consume Script Examples
WebWorkingMan edited this page 2024-08-30 13:31:28 +02:00
Table of Contents
This wiki page is a repository of example post-consume scripts contributed by the community. As always, you should exercise caution when using a script and make sure you understand the code before using a script from the internet.
Python + API
A more advanced post consume script which uses Python and the API to assign some new values to a document which has just been consumed. Make sure the script has execute permissions.
#!/usr/bin/env python3
import os
import json
# TODO: The user can use anything in the standard library, installed for paperless
# or use the custom startup scripts to install additional libraries via pip
import requests
def _set_auth_tokens(paperless_url: str, timeout: float, session: requests.Session):
# TODO: You fill these in or otherwise provide them
credentials = {"username": "test", "password": "test"}
response = session.get(paperless_url, timeout=timeout)
response.raise_for_status()
csrf_token = response.cookies["csrftoken"]
response = session.post(
paperless_url + "/api/token/",
data=json.dumps(credentials),
headers={
"X-CSRFToken": csrf_token,
"Content-Type": "application/json"
},
timeout=timeout,
)
response.raise_for_status()
api_token = response.json()["token"]
session.headers.update(
{"Authorization": f"Token {api_token}", f"X-CSRFToken": csrf_token}
)
if __name__ == "__main__":
# Running inside the Docker container
# TODO: Update this as needed
paperless_url = "http://localhost:8000"
timeout = 5.0
with requests.Session() as sess:
# Set tokens for the appropriate header auth
_set_auth_tokens(paperless_url, timeout, sess)
# Get the PK as provided via post-consume
doc_pk = int(os.environ["DOCUMENT_ID"])
# Query the API for the document info
doc_info_resp = sess.get(
paperless_url + f"/api/documents/{doc_pk}/", timeout=timeout
)
doc_info_resp.raise_for_status()
doc_info = doc_info_resp.json()
# Extract the currently assigned values
correspondent = doc_info["correspondent"]
doc_type = doc_info["document_type"]
doc_original_name = doc_info["original_file_name"]
# etc...
# Parse, set, otherwise choose new values
# TODO: Up to the user to decide how these new values should be set
# Use regex, etc to get the primary key of the new values
new_correspondent = 1
new_doc_type = 1
# Update the document
resp = sess.patch(
paperless_url + f"/api/documents/{doc_pk}/",
headers={"Content-Type": "application/json"},
data=json.dumps({"correspondent": new_correspondent, "document_type": new_doc_type}),
timeout=timeout,
)
resp.raise_for_status()
Send new document message via ntfy + Paperparrot
#!/usr/bin/env python3
import os, requests, json
DOCUMENT_ID = os.getenv('DOCUMENT_ID')
USER = os.getenv('DOCUMENT_OWNER')
PAPERPARROT_URL = f"paperparrot://documents/{DOCUMENT_ID}"
NTFY_URL = "https://ntfy.example.com/"
requests.post(NTFY_URL, data=json.dumps({
"topic": USER,
"message": f"Hi, {USER}, a Document has been added",
"actions": [{"action": "view", "label": "Open", "url": PAPERPARROT_URL}]
}), headers={"Click": PAPERPARROT_URL})
Feel free to contribute to the wiki pages - enhance and extend the content!
Also browse Discussions & connect in Matrix chat.