-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_kdnr.py
63 lines (53 loc) · 1.69 KB
/
post_kdnr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
import os
import httpx
import json
import re
#Define Variables
auth_token = "xxxxxxxxxxx"
url_base = "https://paperless.local"
custom_field_nr = "8"
search_pattern = ["Kundennummer",
"Kunden-Nr",
"Kd.Nr",
"Mitglieds-Nr",
"Unser Zeichen"
]
def paperless_send(doc):
#Define API URL
url_api_doc = url_base + "/api/documents/"
#Define Token
auth_header = ({
"Authorization":"Token "+ auth_token,
"Content-Type":"application/json"
})
#Debug Auth Header
print(auth_header)
api_url = url_api_doc + doc + "/"
#Debug API URL
print(api_url)
response = httpx.get(api_url, headers=auth_header)
result = response.json()
#Debug Doc ID
print(result["id"])
def adding_cf():
custom_fields = result["custom_fields"]
add_custom_field = {"value":post_custom_kdnr, "field":custom_field_nr}
custom_fields.append(add_custom_field)
print(custom_fields)
post_custom_fields = ({
"custom_fields": custom_fields
})
httpx.patch(api_url, headers=auth_header, json=post_custom_fields)
for search_word in search_pattern:
try:
print(f"Suchwort: {search_word}")
json_lookahead = re.search(f"(?<={search_word})(.*)",result["content"])
post_custom_kdnr = json_lookahead.group(0)
post_custom_kdnr = re.sub("[^a-zA-Z0-9]","",post_custom_kdnr)
# modify the custom fields
adding_cf()
except:
print(f"Keinen Treffer für {search_word}")
doc = os.environ.get('DOCUMENT_ID')
paperless_send(doc)