|
2 | 2 | import requests |
3 | 3 | import secrets |
4 | 4 | import datetime |
| 5 | +import time |
| 6 | +import os |
5 | 7 |
|
6 | 8 | secretsVersion = raw_input('To edit production server, enter the name of the secrets file: ') |
7 | 9 | if secretsVersion != '': |
|
19 | 21 |
|
20 | 22 | requests.packages.urllib3.disable_warnings() |
21 | 23 |
|
22 | | -directory = filePath+raw_input('Enter directory name: ') |
| 24 | +directory = raw_input('Enter directory name: ') |
23 | 25 | fileExtension = '.'+raw_input('Enter file extension: ') |
24 | | -communityName = raw_input('Enter community name: ') |
25 | | -collectionName = raw_input('Enter collectionn name: ') |
| 26 | +communityHandle = raw_input('Enter community handle: ') |
| 27 | +collectionName = raw_input('Enter collection name: ') |
26 | 28 |
|
| 29 | +startTime = time.time() |
27 | 30 | data = json.dumps({'email':email,'password':password}) |
28 | 31 | header = {'content-type':'application/json','accept':'application/json'} |
29 | 32 | session = requests.post(baseURL+'/rest/login', headers=header, verify=verify, data=data).content |
|
33 | 36 | userFullName = status['fullname'] |
34 | 37 | print 'authenticated' |
35 | 38 |
|
36 | | -#Post community |
37 | | -community = json.dumps({'name': communityName}) |
38 | | -post = requests.post(baseURL+'/rest/communities', headers=headerAuth, verify=verify, data=community).json() |
39 | | -communityID = post['link'] |
| 39 | +#Get community ID |
| 40 | +endpoint = baseURL+'/rest/handle/'+communityHandle |
| 41 | +community = requests.get(endpoint, headers=headerAuth, verify=verify).json() |
| 42 | +communityID = str(community['id']) |
40 | 43 |
|
41 | 44 | #Post collection |
42 | 45 | collection = json.dumps({'name': collectionName}) |
43 | | -post = requests.post(baseURL+communityID+'/collections', headers=headerAuth, verify=verify, data=collection).json() |
| 46 | +post = requests.post(baseURL+'/rest/communities/'+communityID+'/collections', headers=headerAuth, verify=verify, data=collection).json() |
44 | 47 | collectionID = post['link'] |
45 | 48 |
|
46 | 49 | #Post items |
47 | | -collectionMetadata = json.load(open(directory+'/'+'collectionMetadata.json')) |
| 50 | +fileList = {} |
| 51 | +for root, dirs, files in os.walk(directory, topdown=True): |
| 52 | + for file in files: |
| 53 | + if file.endswith(fileExtension): |
| 54 | + fileList[file.replace('.pdf','')] = os.path.join(root, file).replace('\\','/') |
| 55 | + print file |
| 56 | +elapsedTime = time.time() - startTime |
| 57 | +m, s = divmod(elapsedTime, 60) |
| 58 | +h, m = divmod(m, 60) |
| 59 | +print 'File list creation time: ','%d:%02d:%02d' % (h, m, s) |
| 60 | + |
| 61 | +collectionMetadata = json.load(open(directory+'/'+'metadata.json')) |
48 | 62 | for itemMetadata in collectionMetadata: |
49 | 63 | for element in itemMetadata['metadata']: |
50 | 64 | if element['key'] == 'dc.identifier.other': |
|
55 | 69 | itemID = post['link'] |
56 | 70 |
|
57 | 71 | #Post bitstream |
58 | | - bitstream = directory+'/'+fileIdentifier+fileExtension |
| 72 | + bitstream = fileList[fileIdentifier] |
59 | 73 | fileName = bitstream[bitstream.rfind('/')+1:] |
60 | 74 | data = open(bitstream, 'rb') |
61 | 75 | files = {'file': open(bitstream, 'rb')} |
62 | 76 | post = requests.post(baseURL+itemID+'/bitstreams?name='+fileName, headers=headerAuthFileUpload, verify=verify, data=data).json() |
63 | 77 |
|
| 78 | + |
| 79 | + # #Post bitstream |
| 80 | + # bitstream = directory+'/'+fileIdentifier+fileExtension |
| 81 | + # fileName = bitstream[bitstream.rfind('/')+1:] |
| 82 | + # data = open(bitstream, 'rb') |
| 83 | + # files = {'file': open(bitstream, 'rb')} |
| 84 | + # post = requests.post(baseURL+itemID+'/bitstreams?name='+fileName, headers=headerAuthFileUpload, verify=verify, data=data).json() |
| 85 | + |
64 | 86 | #Create provenance notes |
65 | 87 | provNote = {} |
66 | 88 | provNote['key'] = 'dc.description.provenance' |
|
97 | 119 | print post |
98 | 120 |
|
99 | 121 | logout = requests.post(baseURL+'/rest/logout', headers=headerAuth, verify=verify) |
| 122 | + |
| 123 | +elapsedTime = time.time() - startTime |
| 124 | +m, s = divmod(elapsedTime, 60) |
| 125 | +h, m = divmod(m, 60) |
| 126 | +print 'Total script run time: ','%d:%02d:%02d' % (h, m, s) |
0 commit comments