-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlcm.py
More file actions
137 lines (98 loc) · 4.1 KB
/
lcm.py
File metadata and controls
137 lines (98 loc) · 4.1 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from configparser import ConfigParser
from dateutil.relativedelta import relativedelta
import datetime
import requests
import json
def read_config_file():
cfg = ConfigParser()
cfg.read(['config.ini',
'/config/config.ini'])
return cfg
def main():
print("Starting document LCM script...\n")
config = read_config_file()
global host
host = config.get('paperless', 'HOST')
global auth
auth = requests.auth.HTTPBasicAuth(
config.get('paperless', 'USER'),
config.get('paperless', 'PASSWORD'),
)
global lcm_prefix
lcm_prefix = config.get('paperless', 'LCM_PREFIX')
global auto_delete
auto_delete = config.getboolean('paperless', 'AUTO_DELETE')
if not auto_delete:
removal_tag = config.get('paperless', 'REMOVAL_TAG')
global removal_tag_id
removal_tag_id = get_removal_tag_id(removal_tag)
lcm_documents = get_lcm_documents()
for document in lcm_documents:
check_document(document)
def get_removal_tag_id(removal_tag):
response = api_request(f"tags/?name__iexact={removal_tag}")
return response['results'][0]['id']
def check_document(document):
print(document['title'])
for tag in document['tags']:
tag_name = tag_id_to_name(tag)
if tag_name.startswith(lcm_prefix):
document_creation_date = datetime.datetime.strptime(
document['created'].split('T')[0], '%Y-%m-%d').date()
lcm_policy = {
'value': int(tag_name[len(lcm_prefix):-1]),
'units': tag_name[-1]
}
print(f"\tFound LCM tag: {tag_name}")
print(f"\tDocument creation date: {document_creation_date}")
today = datetime.date.today()
if(lcm_policy['units'] == 'd'):
if today >= document_creation_date + relativedelta(days=lcm_policy['value']):
add_removal_tag_or_delete(document['id'])
else:
print(
f"\t{((document_creation_date + relativedelta(days=lcm_policy['value'])) - today).days } days to go. ")
if(tag_name.lower().endswith('m')):
if today >= document_creation_date + relativedelta(months=lcm_policy['value']):
add_removal_tag_or_delete(document['id'])
else:
print(
f"\t{ ((document_creation_date + relativedelta(months=lcm_policy['value'])) - today).days } days to go. ")
if(tag_name.lower().endswith('y')):
if today >= document_creation_date + relativedelta(years=lcm_policy['value']):
add_removal_tag_or_delete(document['id'])
else:
print(
f"\t{ ((document_creation_date + relativedelta(years=lcm_policy['value'])) - today).days } days to go. ")
break
def tag_id_to_name(tag_id):
tag = api_request(f"tags/{tag_id}/")
return tag['name']
def get_lcm_documents():
documents = api_request(f"documents/?tags__name__istartswith={lcm_prefix}")
return documents['results']
def api_request(url, method='get', body=''):
if method == 'get':
response = requests.get(host + '/api/' + url, auth=auth)
if method == 'put':
headers = {'Content-type': 'application/json'}
response = requests.put(host + '/api/' + url,
auth=auth, data=body, headers=headers)
if method == 'delete':
response = requests.delete(host + '/api/' + url, auth=auth)
try:
return response.json()
except json.JSONDecodeError:
pass
def add_removal_tag_or_delete(document_id):
if auto_delete:
api_request(f"documents/{document_id}/", 'delete')
print("\t**Document REMOVED**")
else:
document = api_request(f"documents/{document_id}/", 'get')
document['tags'].append(removal_tag_id)
body = document
api_request(f"documents/{document_id}/", 'put', json.dumps(document))
print("\t**Document marked for removal**")
if __name__ == "__main__":
main()