forked from ehanson8/dspace-data-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetBitstreamMetadataByHandleAndName.py
73 lines (63 loc) · 2.38 KB
/
getBitstreamMetadataByHandleAndName.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
import requests
from datetime import datetime
import secret
import time
import csv
import argparse
import pandas as pd
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file')
args = parser.parse_args()
if args.file:
filename = args.file
else:
filename = input('Enter the file: ')
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
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')
bit_dict = []
with open(filename) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
itemHandle = row['handle']
bitstreams_names = row['pres_bits']
endpoint = baseURL+'/rest/handle/'+itemHandle
item = requests.get(endpoint, headers=header, cookies=cookies).json()
link = item['link']
bitsLink = baseURL+link+'/bitstreams?expand=all&limit=1000'
bitstreams = requests.get(bitsLink, headers=header, cookies=cookies).json()
for bitstream in bitstreams:
bit_uuid = bitstream['uuid']
fileName = bitstream['name']
size = bitstream['sizeBytes']
if fileName in bitstreams_names:
bit_dict.append({'handle': itemHandle, 'bit_uuid': bit_uuid, 'bit_name': fileName, 'size': size})
logout = requests.post(baseURL+'/rest/logout', headers=header, cookies=cookies)
df = pd.DataFrame.from_dict(bit_dict)
print(df.head(15))
dt = datetime.now().strftime('%Y-%m-%d %H.%M.%S')
df.to_csv(path_or_buf='bitstreamsSizes_'+dt+'.csv', header='column_names', index=False)
elapsedTime = time.time() - startTime
m, s = divmod(elapsedTime, 60)
h, m = divmod(m, 60)
print('Total script run time: ', '%d:%02d:%02d' % (h, m, s))