|
| 1 | +import json |
| 2 | +import requests |
| 3 | +import secrets |
| 4 | +import time |
| 5 | +import csv |
| 6 | + |
| 7 | +def firstLevelUpdateFromCSV (key, valueSource): |
| 8 | + uri = row['uri'] |
| 9 | + value = row[valueSource] |
| 10 | + if value != '': |
| 11 | + asRecord = requests.get(baseURL+uri, headers=headers).json() |
| 12 | + asRecord[key] = value |
| 13 | + asRecord = json.dumps(asRecord) |
| 14 | + post = requests.post(baseURL + uri, headers=headers, data=asRecord).json() |
| 15 | + print post |
| 16 | + else: |
| 17 | + pass |
| 18 | + |
| 19 | +def secondLevelUpdateFromCSV (key, valueSource, firstLevel): |
| 20 | + uri = row['uri'] |
| 21 | + value = row[valueSource] |
| 22 | + if value != '': |
| 23 | + asRecord = requests.get(baseURL+uri, headers=headers).json() |
| 24 | + try: |
| 25 | + asRecord[firstLevel][key] = value |
| 26 | + except: |
| 27 | + asRecord[firstLevel]= {} |
| 28 | + asRecord[firstLevel][key] = value |
| 29 | + asRecord = json.dumps(asRecord) |
| 30 | + post = requests.post(baseURL + uri, headers=headers, data=asRecord).json() |
| 31 | + print post |
| 32 | + else: |
| 33 | + pass |
| 34 | + |
| 35 | +startTime = time.time() |
| 36 | + |
| 37 | +baseURL = secrets.baseURL |
| 38 | +user = secrets.user |
| 39 | +password = secrets.password |
| 40 | + |
| 41 | +auth = requests.post(baseURL + '/users/'+user+'/login?password='+password).json() |
| 42 | +session = auth["session"] |
| 43 | +headers = {'X-ArchivesSpace-Session':session, 'Content_Type':'application/json'} |
| 44 | + |
| 45 | +filename = raw_input('Enter filename (including \'.csv\'): ') |
| 46 | +filename = 'bibNumbers.csv' |
| 47 | + |
| 48 | +with open(filename) as csvfile: |
| 49 | + reader = csv.DictReader(csvfile) |
| 50 | + for row in reader: |
| 51 | + secondLevelUpdateFromCSV('real_1', 'bib', 'user_defined') |
| 52 | + |
| 53 | +elapsedTime = time.time() - startTime |
| 54 | +m, s = divmod(elapsedTime, 60) |
| 55 | +h, m = divmod(m, 60) |
| 56 | +print 'Total script run time: ', '%d:%02d:%02d' % (h, m, s) |
0 commit comments