-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindUnusedAssets.py
More file actions
78 lines (66 loc) · 2.72 KB
/
Copy pathfindUnusedAssets.py
File metadata and controls
78 lines (66 loc) · 2.72 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
'''
Finds assets that are not referenced in any entries.
oskar.eiriksson@contentstack.com
2020-10-10
'''
import time
import cma
import config
'''
Environmental Variables needed:
- CS_REGION = EU/NA (Europe/North-America)
- CS_MANAGEMENTOKEN (Stack Management Token)
- CS_APIKEY (Stack API Key)
What to do with empty references?
- 1. Just print them out?
- See Method 1
- 2. Do we want to delete unused assets?
- See Method 2
- Delete asset(s) that are not referred within any entries
- 3. Do we want to create a CSV report with them?
- See Method 3
- 4. Do we want to update them with a tag?
- See Method 4
'''
assets = cma.getAllAssets() # Getting all assets from Stack
'''
Method 1 - Prints them out
'''
for asset in assets['assets']:
assetReferences = cma.getAssetReferences(asset['uid'])
if not assetReferences['references']:
config.logging.info('{bold}{uid}{end} not referenced in any entries. Title: {bold}{title}{end}, URL: {underline}{url}{end}'.format(uid=asset['uid'], title=asset['title'], url=asset['url'], bold=config.BOLD, end=config.END, underline=config.UNDERLINE))
'''
Method 2 - Delete asset - !!!Be careful here!!!
'''
# for asset in assets['assets']:
# assetReferences = cma.getAssetReferences(asset['uid'])
# if not assetReferences['references']:
# cma.deleteAsset(asset['uid'])
'''
Method 3 - Create a CSV Report with a timestamp - Possible to open report with e.g. Excel.
'''
# timeStr = time.strftime("%Y%m%d-%H%M%S") # For report file
# filename = timeStr + '-report.csv'
# reportFile = open(filename, 'a') # report file
# reportFile.write('uid;filename;url\n')
# config.logging.info('Creating a report file: {}'.format(filename))
# for asset in assets['assets']:
# assetReferences = cma.getAssetReferences(asset['uid'])
# if not assetReferences['references']:
# config.logging.info('Unused asset - Adding to CSV Report: {}'.format(asset['title']))
# reportFile.write(asset['uid'] + ';' + asset['title'] + ';' + asset['url'] + '\n')
# reportFile.close()
'''
Method 4 - Tag unused assets
Updating the asset with the tag "unused" - That tag can then be used to query the asset, e.g. using advanced search in web application
'''
# tag = 'unused' # To update assets with
# for asset in assets['assets']:
# assetReferences = cma.getAssetReferences(asset['uid'])
# if not assetReferences['references']: # No References - So we know that no entries reference that asset.
# if tag not in asset['tags']:
# asset['tags'].append(tag)
# cma.updateAsset(asset['uid'], asset) # Updating the asset with the new tag
# else:
# config.logging.info('Asset already tagged with unused tag')