forked from ehanson8/dspace-data-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetCollectionDescriptionsForCommunity.py
69 lines (58 loc) · 2.09 KB
/
getCollectionDescriptionsForCommunity.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
import requests
import secret
import csv
import time
import urllib3
import argparse
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')
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--handle', help='community or sub-community handle')
args = parser.parse_args()
if args.handle:
handle = args.handle
else:
handle = input('Enter the handle: ')
baseURL = secret.baseURL
email = secret.email
password = secret.password
filePath = secret.filePath
skippedCollections = secret.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 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('collectionDescriptions'+handle.replace('/', '-')+'.csv', 'w'))
f.writerow(['handle']+['name']+['intro'])
endpoint = baseURL+'/rest/handle/'+handle
community = requests.get(endpoint, headers=header, cookies=cookies).json()
communityID = community['uuid']
endpoint = baseURL+'/rest/communities/'+str(communityID)+'/collections'
output = requests.get(endpoint, headers=header, cookies=cookies).json()
for collection in output:
for item in collection:
if item == 'introductoryText':
intro = collection['introductoryText']
print(intro)
elif item == 'name':
name = collection['name']
print(name)
elif item == 'handle':
handle = collection['handle']
handle = 'http://jhir.library.jhu.edu/handle/'+handle
else:
pass
f.writerow([handle]+[name]+[intro])