This repository has been archived by the owner on Feb 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
replaceKeyValuePairOnItemIdCSV.py
77 lines (67 loc) · 3.31 KB
/
replaceKeyValuePairOnItemIdCSV.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
import json
import requests
import secrets
import time
import csv
from datetime import datetime
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
secretsVersion = input('To edit production server, enter the name of the secrets file: ')
if secretsVersion != '':
try:
secrets = __import__(secretsVersion)
print('Editing Production')
except ImportError:
print('Editing Stage')
else:
print('Editing Stage')
baseURL = secrets.baseURL
email = secrets.email
password = secrets.password
filePath = secrets.filePath
verify = secrets.verify
skippedCollections = secrets.skippedCollections
startTime = time.time()
data = {'email':email,'password':password}
header = {'content-type':'application/json','accept':'application/json'}
session = requests.post(baseURL+'/rest/login', headers=header, verify=verify, params=data).cookies['JSESSIONID']
cookies = {'JSESSIONID': session}
headerFileUpload = {'accept':'application/json'}
status = requests.get(baseURL+'/rest/status', headers=header, cookies=cookies, verify=verify).json()
print('authenticated')
fileName = filePath+input('Enter fileName (including \'.csv\'): ')
replacedKey = input('Enter key: ')
replacementKey = replacedKey
f=csv.writer(open(filePath+'replacedKeyValuePair'+datetime.now().strftime('%Y-%m-%d %H.%M.%S')+'.csv', 'w'))
f.writerow(['itemID']+['replacedKey']+['replacedValue']+['replacementValue']+['delete']+['post'])
with open(fileName) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
itemMetadataProcessed = []
itemID = row['itemID']
replacedValue = row['replacedValue']
replacementValue = row['replacementValue']
itemMetadata = requests.get(baseURL+'/rest/items/'+str(itemID)+'/metadata', headers=header, cookies=cookies, verify=verify).json()
for element in itemMetadata:
languageValue = element['language']
if element['key'] == replacedKey and element['value'] == replacedValue:
updatedMetadataElement = {}
updatedMetadataElement['key'] = replacementKey
updatedMetadataElement['value'] = replacementValue
updatedMetadataElement['language'] = languageValue
itemMetadataProcessed.append(updatedMetadataElement)
provNote = '\''+replacedKey+': '+replacedValue+'\' was replaced by \''+replacementKey+': '+replacementValue+'\' through a batch process on '+datetime.now().strftime('%Y-%m-%d %H:%M:%S')+'.'
provNoteElement = {}
provNoteElement['key'] = 'dc.description.provenance'
provNoteElement['value'] = provNote
provNoteElement['language'] = 'en_US'
itemMetadataProcessed.append(provNoteElement)
else:
itemMetadataProcessed.append(element)
print(itemMetadata)
itemMetadataProcessed = json.dumps(itemMetadataProcessed)
delete = requests.delete(baseURL+'/rest/items/'+str(itemID)+'/metadata', headers=header, cookies=cookies, verify=verify)
print(delete)
post = requests.put(baseURL+'/rest/items/'+str(itemID)+'/metadata', headers=header, cookies=cookies, verify=verify, data=itemMetadataProcessed)
print(post)
f.writerow([itemID]+[replacedKey]+[replacedValue]+[replacementValue]+[delete]+[post])