|
| 1 | +import json |
| 2 | +import requests |
| 3 | +import secrets |
| 4 | +import time |
| 5 | +import csv |
| 6 | + |
| 7 | +secretsVersion = raw_input('To edit production server, enter the name of the secrets file: ') |
| 8 | +if secretsVersion != '': |
| 9 | + try: |
| 10 | + secrets = __import__(secretsVersion) |
| 11 | + print 'Editing Production' |
| 12 | + except ImportError: |
| 13 | + print 'Editing Stage' |
| 14 | +else: |
| 15 | + print 'Editing Stage' |
| 16 | + |
| 17 | +baseURL = secrets.baseURL |
| 18 | +email = secrets.email |
| 19 | +password = secrets.password |
| 20 | +filePath = secrets.filePath |
| 21 | +verify = secrets.verify |
| 22 | + |
| 23 | +requests.packages.urllib3.disable_warnings() |
| 24 | + |
| 25 | +handle = raw_input('Enter handle: ') |
| 26 | + |
| 27 | +data = json.dumps({'email':email,'password':password}) |
| 28 | +header = {'content-type':'application/json','accept':'application/json'} |
| 29 | +session = requests.post(baseURL+'/rest/login', headers=header, verify=verify, data=data).content |
| 30 | +headerAuth = {'content-type':'application/json','accept':'application/json', 'rest-dspace-token':session} |
| 31 | +print 'authenticated' |
| 32 | +startTime = time.time() |
| 33 | + |
| 34 | +endpoint = baseURL+'/rest/handle/'+handle |
| 35 | +collection = requests.get(endpoint, headers=headerAuth, verify=verify).json() |
| 36 | +collectionID = collection['id'] |
| 37 | +collectionTitle = requests.get(endpoint, headers=headerAuth, verify=verify).json() |
| 38 | +endpoint = baseURL+'/rest/collections/'+str(collectionID)+'/items?limit=5000' |
| 39 | +itemList = requests.get(endpoint, headers=headerAuth, verify=verify).json() |
| 40 | + |
| 41 | +f=csv.writer(open(filePath+'handlesAndBitstreams.csv', 'wb')) |
| 42 | +f.writerow(['bitstream']+['handle']) |
| 43 | + |
| 44 | +for item in itemList: |
| 45 | + itemHandle = item['handle'] |
| 46 | + itemID = str(item['link']) |
| 47 | + |
| 48 | + bitstreams = requests.get(baseURL+itemID+'/bitstreams', headers=headerAuth, verify=verify).json() |
| 49 | + for bitstream in bitstreams: |
| 50 | + fileName = bitstream['name'] |
| 51 | + fileName.replace('.pdf','') |
| 52 | + f.writerow([fileName]+[itemHandle]) |
| 53 | + |
| 54 | +logout = requests.post(baseURL+'/rest/logout', headers=headerAuth, verify=verify) |
| 55 | + |
| 56 | +elapsedTime = time.time() - startTime |
| 57 | +m, s = divmod(elapsedTime, 60) |
| 58 | +h, m = divmod(m, 60) |
| 59 | +print 'Total script run time: ','%d:%02d:%02d' % (h, m, s) |
0 commit comments