I want to watch a directory on my local machine for PDFs, and then upload them to Paperless NGX whenever they appear. That way, I can scan directly to a folder with NAPS2, and the uploader will do its thing.
I want to watch a directory on my local machine for PDFs, and then upload them to [Paperless NGX](https://github.com/paperless-ngx/paperless-ngx) whenever they appear. That way, I can scan directly to a folder with [NAPS2](https://www.naps2.com/), and the uploader will do its thing.
I wrote this little script. You need to set
* `PAPERLESS_API_TOKEN`
* `PAPERLESS_API_HOST`
in the environment. Paperless will let you get an API token from your profile, and the host should be just the hostname (e.g. `paperless.example.com`).
```python
import requests
import os
from time import sleep
import re
import shutil
from http import HTTPStatus
PAPERLESS = f"https://{os.getenv('PAPERLESS_API_HOST')}/api/documents/post_document/"
TOKEN = os.getenv("PAPERLESS_API_TOKEN")
while True:
files = os.listdir()
for file in files:
m = re.match(r".*\.pdf$", file)
if m:
pdf = m.group(0)
# https://stackoverflow.com/questions/22567306/how-to-upload-file-with-python-requests
resp = requests.post(
PAPERLESS,
headers={"Authorization": f"Token {TOKEN}"},
files={"document": open(pdf, "rb")},
)
print(f"{pdf}: {resp.status_code} {resp.text}")
if resp.status_code == HTTPStatus.OK:
shutil.move(pdf, os.path.join("success", pdf))
else:
shutil.move(pdf, os.path.join("failure", pdf))
sleep(5)
```
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
I want to watch a directory on my local machine for PDFs, and then upload them to Paperless NGX whenever they appear. That way, I can scan directly to a folder with NAPS2, and the uploader will do its thing.
I wrote this little script. You need to set
PAPERLESS_API_TOKENPAPERLESS_API_HOSTin the environment. Paperless will let you get an API token from your profile, and the host should be just the hostname (e.g.
paperless.example.com).