-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassetKeywords.py
116 lines (94 loc) · 4.14 KB
/
assetKeywords.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
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
# !/usr/local/bin/python3.4.3
# ----Copyright (c) 2019 Carnegie Hall | The MIT License (MIT)----
# run script with 4 arguments:
# argument 0 is the script name
# argument 1 is the filepath to the csv of asset IDs with folder IDs
# argument 2 is the filepath to the csv of event IDs and event keyword strings
# argument 3 is the filepath to the csv of work performance folder IDs and keyword strings
# argument 4 is destination directory for output CSV of asset IDs + all keywords
import csv
import os
from os.path import join
import sys
filePath1 = str(sys.argv[1])
filePath2 = str(sys.argv[2])
filePath3 = str(sys.argv[3])
filePath4 = str(sys.argv[4])
assetDict = {}
dupeDict = {}
with open(filePath1, newline = '', encoding='utf-8') as f:
assetData = csv.reader(f, dialect='excel', delimiter=',', quotechar='"')
next(assetData, None) #skip headers
idChecklist = []
for row in assetData:
assetID = row[0]
eventFolderID = row[1]
assetDict[str(eventFolderID)] = {}
assetDict[str(eventFolderID)]['Asset ID'] = assetID
assetDict[str(eventFolderID)]['Keywords'] = ''
if eventFolderID not in idChecklist:
idChecklist.append(eventFolderID)
else:
print(eventFolderID, '\t', assetID)
with open(filePath2, newline = '', encoding='utf-8') as h:
eventData = csv.reader(h, dialect='excel', delimiter=',', quotechar='"')
next(eventData, None)
for row in eventData:
eventFolderID = row[0]
eventPerformers = row[1]
eventPresenters = row[2]
if eventPerformers:
keywordString = f'{eventPerformers}{eventPresenters}'
else:
keywordString = f'{eventPresenters}'
try:
assetDict[str(eventFolderID)]['Keywords'] = keywordString
except KeyError:
pass
with open(filePath3, newline = '', encoding='utf-8') as j:
workPerfData = csv.reader(j, dialect='excel', delimiter=',', quotechar='"')
next(workPerfData, None)
idChecklist = []
keywordList = []
workPerfDict = {}
for row in workPerfData:
workPerfFolderID = row[0]
eventFolderID = row[1]
workPerfEntities = row[2]
if workPerfEntities:
workPerfKeywords = workPerfEntities.split('|')
if eventFolderID not in idChecklist:
idChecklist.append(eventFolderID)
keywordList = []
for keyword in workPerfKeywords:
if keyword:
keywordList.append(keyword)
workPerfDict[str(eventFolderID)] = keywordList
else:
keywordList = workPerfDict[str(eventFolderID)]
for keyword in workPerfKeywords:
if keyword:
if keyword not in keywordList:
keywordList.append(keyword)
workPerfDict[str(eventFolderID)] = keywordList
for key in workPerfDict:
keywords = workPerfDict[key]
s = '|'
keywordString = s.join(keywords)
workPerfDict[key] = keywordString
for key in assetDict:
assetKeywords = assetDict[key]['Keywords']
try:
workPerfKeywords = workPerfDict[key]
keywordString = f'{assetKeywords}{workPerfKeywords}'
assetDict[key]['Keywords'] = keywordString
except KeyError:
pass
outputPath = ''.join([str(filePath4), '/assetKeywords.csv'])
fields = ['Asset ID', 'Keywords']
with open(outputPath, 'w', newline='') as csvfile:
w = csv.DictWriter(csvfile, fields)
w.writeheader()
for k in assetDict:
w.writerow({field: assetDict[k].get(field) for field in fields})
print('Done assembling asset keywords')