Skip to content

Commit 8a3a04e

Browse files
committed
updates
1 parent a862bf1 commit 8a3a04e

File tree

4 files changed

+106
-89
lines changed

4 files changed

+106
-89
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ No collections skipped:
3636
#### [compareTwoKeysInCommunity.py](compareTwoKeysInCommunity.py)
3737
Based on user input, extracts the values of two specified keys from a specified community to a CSV file for comparison.
3838

39+
#### [exportSelectedRecordMetadataToCSV.py](exportSelectedRecordMetadataToCSV.py)
40+
Based a CSV of item handles, extracts all metadata (except 'dc.description.provenance' values) from the selected items to a CSV file.
41+
3942
#### [findBogusUris.py](findBogusUris.py)
4043
Extracts the item ID and the value of the key 'dc.identifier.uri' to a CSV file when the value does not begin with the handlePrefix specified in the secrets.py file.
4144

@@ -72,9 +75,6 @@ Based on user input, extracts the ID and URI for all items in the specified coll
7275
#### [getRecordsWithKeyAndValue.py](getRecordsWithKeyAndValue.py)
7376
Based on user input, extracts the ID and URI for all items in the repository with the specified key-value pair to a CSV file.
7477

75-
#### [identifyDuplicateKeyValuePairsFromItemsDiffLangTags.py](identifyDuplicateKeyValuePairsFromItemsDiffLangTags.py)
76-
Extracts all duplicate key-value pairs in an item that only differ in their assigned language tag.
77-
7878
#### [identifyItemsMissingKeyInCommunity.py](identifyItemsMissingKeyInCommunity.py)
7979
Based on user input, extracts the IDs of items from a specified community that do not have the specified key.
8080

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import json
2+
import requests
3+
import secrets
4+
import time
5+
import csv
6+
from collections import Counter
7+
import urllib3
8+
import argparse
9+
10+
#login info kept in secrets.py file
11+
baseURL = secrets.baseURL
12+
email = secrets.email
13+
password = secrets.password
14+
filePath = secrets.filePath
15+
verify = secrets.verify
16+
17+
parser = argparse.ArgumentParser()
18+
parser.add_argument('-f', '--fileName', help='the CSV file of record handles. optional - if not provided, the script will ask for input')
19+
args = parser.parse_args()
20+
21+
if args.fileName:
22+
fileName = filePath+args.fileName
23+
else:
24+
fileName = filePath+raw_input('Enter the CSV file of record handles (including \'.csv\'): ')
25+
26+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
27+
28+
secretsVersion = raw_input('To edit production server, enter the name of the secrets file: ')
29+
if secretsVersion != '':
30+
try:
31+
secrets = __import__(secretsVersion)
32+
print 'Editing Production'
33+
except ImportError:
34+
print 'Editing Stage'
35+
else:
36+
print 'Editing Stage'
37+
38+
#authentication
39+
startTime = time.time()
40+
data = {'email':email,'password':password}
41+
header = {'content-type':'application/json','accept':'application/json'}
42+
session = requests.post(baseURL+'/rest/login', headers=header, verify=verify, params=data).cookies['JSESSIONID']
43+
cookies = {'JSESSIONID': session}
44+
headerFileUpload = {'accept':'application/json'}
45+
cookiesFileUpload = cookies
46+
status = requests.get(baseURL+'/rest/status', headers=header, cookies=cookies, verify=verify).json()
47+
userFullName = status['fullname']
48+
print 'authenticated'
49+
50+
51+
handles = []
52+
with open(fileName) as csvfile:
53+
reader = csv.DictReader(csvfile)
54+
for row in reader:
55+
handles.append(row['handle'])
56+
57+
itemList = []
58+
for handle in handles:
59+
endpoint = baseURL+'/rest/handle/'+handle
60+
item = requests.get(endpoint, headers=header, cookies=cookies, verify=verify).json()
61+
itemID = item['uuid']
62+
itemList.append(itemID)
63+
64+
keyList = []
65+
for itemID in itemList:
66+
metadata = requests.get(baseURL+'/rest/items/'+str(itemID)+'/metadata', headers=header, cookies=cookies, verify=verify).json()
67+
for metadataElement in metadata:
68+
key = metadataElement['key']
69+
if key not in keyList and key != 'dc.description.provenance':
70+
keyList.append(key)
71+
print itemID, key
72+
73+
keyListHeader = ['itemID']
74+
keyListHeader = keyListHeader + keyList
75+
print keyListHeader
76+
f=csv.writer(open(filePath+'selectedRecordMetadata.csv', 'wb'))
77+
f.writerow(keyListHeader)
78+
79+
itemRows = []
80+
for itemID in itemList:
81+
#itemRow = {}
82+
itemRow = dict.fromkeys(keyListHeader, '')
83+
itemRow['itemID'] = itemID
84+
print itemRow
85+
metadata = requests.get(baseURL+'/rest/items/'+str(itemID)+'/metadata', headers=header, cookies=cookies, verify=verify).json()
86+
for metadataElement in metadata:
87+
for key in keyListHeader:
88+
if metadataElement['key'] == key:
89+
value = metadataElement['value'].encode('utf-8')+'|'
90+
try:
91+
itemRow[key] = itemRow[key] + value
92+
except:
93+
itemRow[key] = value
94+
print itemRow
95+
itemList = []
96+
for key in keyListHeader:
97+
itemList.append(itemRow[key][:len(itemRow[key])-1])
98+
#itemRows.append(itemRow)
99+
f.writerow(itemList)

getCompleteAndUniqueValuesForAllKeysInCommunity.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@
7878
for l in range (0, len (metadata)):
7979
if metadata[l]['key'] != 'dc.description.provenance':
8080
key = metadata[l]['key']
81-
value = metadata[l]['value'].encode('utf-8')
81+
try:
82+
value = metadata[l]['value'].encode('utf-8')
83+
except:
84+
value = ''
8285
if os.path.isfile(filePathComplete+key+'ValuesComplete.csv') == False:
8386
f=csv.writer(open(filePathComplete+key+'ValuesComplete.csv', 'wb'))
8487
f.writerow(['itemID']+['value'])

identifyDuplicateKeyValuePairsFromItemsDiffLangTags.py

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)