forked from ehanson8/dspace-data-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadataOverview.py
134 lines (122 loc) · 5.45 KB
/
metadataOverview.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import requests
import secret
import time
import csv
from collections import Counter
secretVersion = input('To edit production server, enter the name of the secret file: ')
if secretVersion != '':
try:
secret = __import__(secretVersion)
print('Using Production')
except ImportError:
print('Using Stage')
else:
print('Using Stage')
# login info kept in secret.py file
baseURL = secret.baseURL
email = secret.email
password = secret.password
filePath = secret.filePath
skippedCollections = secret.skippedCollections
# authentication
startTime = time.time()
data = {'email': email, 'password': password}
header = {'content-type': 'application/json', 'accept': 'application/json'}
session = requests.post(baseURL+'/rest/login', headers=header, params=data).cookies['JSESSIONID']
cookies = {'JSESSIONID': session}
headerFileUpload = {'accept': 'application/json'}
cookiesFileUpload = cookies
status = requests.get(baseURL+'/rest/status', headers=header, cookies=cookies).json()
userFullName = status['fullname']
print('authenticated')
f = csv.writer(open(filePath+'collectionStats.csv', 'w'))
f.writerow(['Name']+['collectionID']+['collectionHandle']+['numberOfItems'])
itemList = []
endpoint = baseURL+'/rest/communities'
communities = requests.get(endpoint, headers=header, cookies=cookies, verify=verify).json()
for i in range(0, len(communities)):
communityID = communities[i]['uuid']
communityName = communities[i]['name']
collections = requests.get(baseURL+'/rest/communities/'+str(communityID)+'/collections', headers=header, cookies=cookies, verify=verify).json()
for j in range(0, len(collections)):
collectionID = collections[j]['uuid']
collectionID = collections[j]['uuid']
numberItems = collections[j]['numberItems']
collectionName = collections[j]['name']
collectionHandle = collections[j]['handle']
fullName = communityName+' - '+collectionName
print(collectionID)
if collectionID not in skippedCollections:
offset = 0
items = ''
while items != []:
f.writerow([fullName]+[collectionID]+[collectionHandle]+[str(numberItems).zfill(6)])
items = requests.get(baseURL+'/rest/collections/'+str(collectionID)+'/items?limit=200&offset='+str(offset), headers=header, cookies=cookies, verify=verify)
while items.status_code != 200:
time.sleep(5)
items = requests.get(baseURL+'/rest/collections/'+str(collectionID)+'/items?limit=200&offset='+str(offset), headers=header, cookies=cookies, verify=verify)
items = items.json()
for k in range(0, len(items)):
itemID = items[k]['uuid']
concat = str(communityID)+':'+str(collectionID)+'|'+str(itemID)
itemList.append(concat)
offset = offset + 200
print(offset)
elapsedTime = time.time() - startTime
m, s = divmod(elapsedTime, 60)
h, m = divmod(m, 60)
print('Item list creation time: ', '%d:%02d:%02d' % (h, m, s))
# retrieve metadata from all items
keyList = []
dcTypeList = []
keyCount = []
f = csv.writer(open(filePath+'dspaceIDs.csv', 'w'))
f.writerow(['communityID']+['collectionID']+['itemID'])
for concat in itemList:
communityID = concat[:concat.find(':')]
collectionID = concat[concat.find(':')+1:concat.find('|')]
itemID = concat[concat.find('|')+1:]
f.writerow([communityID]+[collectionID]+[itemID])
concat = concat[:concat.find('|')]
print(itemID)
metadata = requests.get(baseURL+'/rest/items/'+str(itemID)+'/metadata', headers=header, cookies=cookies).json()
for i in range(0, len(metadata)):
key = metadata[i]['key']
keyCount.append(key)
keyConcat = concat+'|' + metadata[i]['key']
if keyConcat not in keyList:
keyList.append(keyConcat)
if metadata[i]['key'] == 'dc.type':
dcType = metadata[i]['value']
if dcType not in dcTypeList:
dcTypeList.append(dcType)
print('writing types')
f = csv.writer(open(filePath+'dspaceTypes.csv', 'w'))
f.writerow(['type'])
for dcType in dcTypeList:
f.writerow([dcType])
print('writing global key counts')
f = csv.writer(open(filePath+'keyCount.csv', 'w'))
f.writerow(['key']+['count'])
countDict = Counter(keyCount)
for key, value in countDict.items():
f.writerow([key]+[str(value).zfill(6)])
print('writing collection metadata keys')
f = csv.writer(open(filePath+'collectionMetadataKeys.csv', 'w'))
f.writerow(['fullName']+['collectionID']+['collectionHandle']+['key'])
for concat in keyList:
communityID = concat[:concat.find(':')]
collectionID = concat[concat.find(':')+1:concat.find('|')]
key = concat[concat.rfind('|')+1:]
additionalDataCommunity = requests.get(baseURL+'/rest/communities/'+str(communityID), headers=header, cookies=cookies).json()
communityName = additionalDataCommunity['name']
additionalDataCollection = requests.get(baseURL+'/rest/collections/'+str(collectionID), headers=header, cookies=cookies).json()
collectionName = additionalDataCollection['name']
collectionHandle = additionalDataCollection['handle']
fullName = communityName+' - '+collectionName
f.writerow([fullName]+[collectionID]+[collectionHandle]+[key])
elapsedTime = time.time() - startTime
m, s = divmod(elapsedTime, 60)
h, m = divmod(m, 60)
print("%d:%02d:%02d" % (h, m, s))
logout = requests.post(baseURL+'/rest/logout', headers=header, cookies=cookies)