|
13 | 13 | headerAuth = {'content-type':'application/json','accept':'application/json', 'rest-dspace-token':session} |
14 | 14 | headerAuthFileUpload = {'accept':'application/json', 'rest-dspace-token':session} |
15 | 15 | print 'authenticated' |
16 | | - |
| 16 | +# |
17 | 17 | #Post community |
18 | 18 | communityName = 'Test Community' |
19 | 19 | community = json.dumps({'name': communityName}) |
20 | 20 | post = requests.post(baseURL+'/rest/communities', headers=headerAuth, data=community).json() |
21 | | -print post |
| 21 | +print json.dumps(post) |
22 | 22 | communityID = post['link'] |
23 | 23 | print communityID |
24 | 24 |
|
25 | | -# #Post collection |
| 25 | +#Post collection |
26 | 26 | collectionName = 'Test Collection' |
27 | 27 | collection = json.dumps({'name': collectionName}) |
28 | 28 | post = requests.post(baseURL+communityID+'/collections', headers=headerAuth, data=collection).json() |
29 | | -print post |
| 29 | +print json.dumps(post) |
30 | 30 | collectionID = post['link'] |
31 | 31 |
|
32 | 32 | #Post item |
33 | 33 | item = json.dumps({'metadata': [{'key': 'dc.title', 'language': 'en_US', 'value': 'testing123'}]}) |
34 | 34 | post = requests.post(baseURL+collectionID+'/items', headers=headerAuth, data=item).json() |
35 | | -print post |
| 35 | +print json.dumps(post) |
36 | 36 | itemID = post['link'] |
37 | 37 |
|
38 | 38 | #Post bitstream |
39 | 39 | #bitstream = filePath+'test.txt' |
40 | 40 | bitstream = filePath+'testImage.jpg' |
| 41 | +#bitstream = filePath+'A.pdf' |
| 42 | +#bitstream = filePath+'test.pdf' |
| 43 | + |
41 | 44 | fileName = bitstream[bitstream.rfind('/')+1:] |
42 | 45 | files = {'file': open(bitstream, 'rb')} |
43 | 46 | data = json.dumps({'name': fileName, 'sequenceId': 1}) |
44 | | -post = requests.post(baseURL+itemID+'/bitstreams', headers=headerAuthFileUpload, files=files).json() |
45 | | -print post |
46 | | -bitstreamID = '/rest/bitstreams/'+str(post['id']) |
47 | | -post = requests.put(baseURL+bitstreamID, headers=headerAuth, data=data) |
48 | | -print post |
| 47 | +post = requests.post(baseURL+itemID+'/bitstreams?name='+fileName, headers=headerAuthFileUpload, files=files).json() |
| 48 | +print json.dumps(post) |
| 49 | +metadata = requests.get(baseURL+itemID+'/metadata', headers=headerAuth).json() |
49 | 50 |
|
50 | | -# print baseURL+itemID+'/metadata' |
51 | | -# item = json.dumps([{'key': 'dc.title', 'language': 'en_US', 'value': 'testing123'}]) |
52 | | -# post = requests.post(baseURL+itemID+'/metadata', headers=headerAuth, data=item).json() |
53 | | -# print post |
| 51 | +updatedMetadata = [] |
| 52 | +for metadatum in metadata: |
| 53 | + if metadatum['key'] != 'dc.description.provenance': |
| 54 | + print 'yay' |
| 55 | + updatedMetadata.append(metadatum) |
| 56 | + else: |
| 57 | + value = metadatum['value'] |
| 58 | + time = value[value.index('DSpace on ')+10:value.index(' (GMT)')] |
| 59 | + print time |
| 60 | + print 'nay' |
| 61 | + provNote = {} |
| 62 | + provNote['key'] = 'dc.description.provenance' |
| 63 | + provNote['language'] = 'en_US' |
| 64 | + bitstreams = requests.get(baseURL+itemID+'/bitstreams', headers=headerAuth).json() |
| 65 | + bitstreamCount = len(bitstreams) |
| 66 | + provNoteValue = 'Made available in DSpace on '+time+' (GMT). No. of bitstreams: '+str(bitstreamCount) |
| 67 | + for bitstream in bitstreams: |
| 68 | + fileName = bitstream['name'] |
| 69 | + size = str(bitstream['sizeBytes']) |
| 70 | + checksum = bitstream['checkSum']['value'] |
| 71 | + print checksum |
| 72 | + algorithm = bitstream ['checkSum']['checkSumAlgorithm'] |
| 73 | + print algorithm |
| 74 | + provNoteValue = provNoteValue+' '+fileName+': '+size+' bytes, checkSum: '+checksum+' ('+algorithm+')' |
| 75 | + print provNoteValue |
| 76 | + provNote['value'] = provNoteValue |
| 77 | + print provNote |
| 78 | + updatedMetadata.append(provNote) |
| 79 | +updatedMetadata = json.dumps(updatedMetadata) |
| 80 | + |
| 81 | +delete = requests.delete(baseURL+itemID+'/metadata', headers=headerAuth) |
| 82 | +print delete |
| 83 | +post = requests.put(baseURL+itemID+'/metadata', headers=headerAuth, data=updatedMetadata) |
| 84 | +print post |
54 | 85 |
|
55 | 86 | logout = requests.post(baseURL+'/rest/logout', headers=headerAuth) |
0 commit comments