forked from ehanson8/dspace-data-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getKeyNamesForCollection.py
67 lines (60 loc) · 2.2 KB
/
getKeyNamesForCollection.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
import json
import requests
import secret
import time
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')
baseURL = secret.baseURL
email = secret.email
password = secret.password
filePath = secret.filePath
collectionHandle = input('enter collection handle please: ')
startTime = time.time()
data = json.dumps({'email': email, 'password': password})
header = {'content-type': 'application/json', 'accept': 'application/json'}
session = requests.post(baseURL+'/rest/login', headers=header, data=data).content
headerAuth = {'content-type': 'application/json', 'accept': 'application/json', 'rest-dspace-token': session}
print('authenticated')
itemList = []
endpoint = baseURL+'/rest/handle/'+collectionHandle
collection = requests.get(endpoint, headers=headerAuth).json()
collectionID = collection['uuid']
print(collectionID)
offset = 0
items = ''
while items != []:
items = requests.get(baseURL+'/rest/collections/'+str(collectionID)+'/items?limit=200&offset='+str(offset), headers=headerAuth)
while items.status_code != 200:
time.sleep(5)
items = requests.get(baseURL+'/rest/collections/'+str(collectionID)+'/items?limit=200&offset='+str(offset), headers=headerAuth)
items = items.json()
for k in range(0, len(items)):
itemID = items[k]['uuid']
itemList.append(itemID)
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))
dcElements = []
for number, itemID in enumerate(itemList):
itemsRemaining = len(itemList) - number
metadata = requests.get(baseURL+'/rest/items/'+str(itemID)+'/metadata', headers=headerAuth).json()
for l in range(0, len(metadata)):
keys = metadata[l]['key']
keys = str(keys)
if keys in dcElements:
pass
if keys not in dcElements:
dcElements.append(keys)
print(len(dcElements))
print("\'] + [\'".join(dcElements))
print((dcElements))