-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdelete_unknown_containers.py
89 lines (86 loc) · 4.36 KB
/
delete_unknown_containers.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
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
# This script checks archival objects for specific resources in ArchivesSpace and if they have an 'unknown_container' as
# the value of their indicator, then it deletes that object.
from secrets import *
from asnake.aspace import ASpace
from asnake.client import ASnakeClient
aspace = ASpace(baseurl=as_api, username=as_un, password=as_pw)
client = ASnakeClient(baseurl=as_api, username=as_un, password=as_pw)
client.authorize()
resource_ids = ["/repositories/4/resources/4103", "/repositories/4/resources/4064", "/repositories/4/resources/2798",
"/repositories/4/resources/1001", "/repositories/4/resources/4048", "/repositories/2/resources/633",
"/repositories/2/resources/723", "/repositories/2/resources/748", "/repositories/2/resources/414"]
# "/repositories/5/resources/5071" - UA collection - Steve to check with Kat
for resource_id in resource_ids:
unknown_count = 0
uri_breakup = resource_id.split("/")
res_id = uri_breakup[4]
repo_id = uri_breakup[2]
try:
rl_repo = aspace.repositories(repo_id)
resource_record = rl_repo.resources(res_id).tree
resource_tree = resource_record.walk
print(rl_repo.resources(res_id).json()["title"])
for node in resource_tree:
ao_json = client.get(node.uri).json()
for instance in ao_json["instances"]:
if "sub_container" in instance.keys():
indicators = []
types = []
for key, value in instance["sub_container"].items():
if "indicator_" in key:
if "unknown container" == value:
child_type = "type_" + str(key[-1])
indicators.append(key)
types.append(child_type)
unknown_count += 1
for indicator in indicators:
try:
del instance["sub_container"][indicator]
except Exception as e:
print("There was an error when deleting the unknown indicator: {}".format(e))
print(instance)
for child_type in types:
try:
del instance["sub_container"][child_type]
except Exception as e:
print("There was an error when deleting the unknown child/grandchild type: {}".format(e))
print(instance)
if indicators and types:
update_ao = client.post(node.uri, json=ao_json).json()
print(update_ao)
else:
indicators = []
types = []
for key, value in instance.items():
if "indicator_" in key:
if "unknown container" == value:
child_type = "type_" + str(key[-1])
indicators.append(key)
types.append(child_type)
unknown_count += 1
for indicator in indicators:
try:
del instance[indicator]
except Exception as e:
print("There was an error when deleting the unknown indicator: {}".format(e))
print(instance)
for child_type in types:
try:
del instance[child_type]
except Exception as e:
print("There was an error when deleting the unknown child/grandchild type: {}".format(e))
print(instance)
if indicators and types:
update_ao = client.post(node.uri, json=ao_json).json()
print(update_ao)
print("Total unknown containers = {}".format(str(unknown_count)))
print("\n")
print("-" * 100)
except Exception as e:
print("There was an error retrieving {}: {}".format(resource_id, e))
try:
print(client.get(resource_id).json())
except:
print("Could not retrieve resource")
print("\n")
print("-" * 100)