Skip to content

Commit ab7bd7f

Browse files
committed
Support re-running
1 parent 86b10d0 commit ab7bd7f

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

getBitstreams.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import csv
55
import urllib3
66
import argparse
7+
import os
8+
import re
79

810

911
def main():
@@ -139,8 +141,12 @@ def main():
139141
if args.verbose: print(dsObject['type'])
140142

141143
itemHandle = dsObject['handle']
144+
handleID = re.sub(r'.*\/', '', itemHandle)
145+
itemPath = filePath + '/' + handleID + '/'
146+
if not os.path.exists(itemPath):
147+
os.makedirs(itemPath)
142148

143-
f = csv.writer(open(filePath+itemHandle.replace('/', '-')+'_bitstreams.csv', 'wb'))
149+
f = csv.writer(open(itemPath + handleID + '_bitstreams.csv', 'wb'))
144150
f.writerow(['sequenceId']+['name']+['format']+['bundleName'])
145151

146152
bitstreamCount = len(dsObject['bitstreams'])
@@ -171,11 +177,12 @@ def main():
171177
bitstreamCount -= limit
172178

173179
for dlBitstream in dlBitstreams:
174-
response = requests.get(baseURL + str(dlBitstream['retrieveLink']), headers=header, cookies=cookies, verify=verify, timeout=response_timeout)
175-
response.raise_for_status() # ensure we notice bad responses
176-
file = open(filePath + dlBitstream['name'], 'wb')
177-
file.write(response.content)
178-
file.close()
180+
if not os.path.isfile(itemPath + dlBitstream['name']):
181+
response = requests.get(baseURL + str(dlBitstream['retrieveLink']), headers=header, cookies=cookies, verify=verify, timeout=response_timeout)
182+
response.raise_for_status() # ensure we notice bad responses
183+
file = open(itemPath + dlBitstream['name'], 'wb')
184+
file.write(response.content)
185+
file.close()
179186
else:
180187
print('object is of an invalid type for this script ({}). please enter the handle of an item or a collection.').format(dsObject['type'])
181188

0 commit comments

Comments
 (0)