|
| 1 | +import json |
| 2 | +import requests |
| 3 | +import secrets |
| 4 | +import time |
| 5 | +import csv |
| 6 | +from datetime import datetime |
| 7 | + |
| 8 | +def addUriLink (key, valueSource): |
| 9 | + uri = '/repositories/3/resources/'+row['ResourceUri'] |
| 10 | + value = row[valueSource] |
| 11 | + print value |
| 12 | + asRecord = requests.get(baseURL+uri, headers=headers).json() |
| 13 | + updatedRecord = asRecord |
| 14 | + if key == 'subjects': |
| 15 | + subjects = updatedRecord['subjects'] |
| 16 | + originalSubjects = updatedRecord['subjects'] |
| 17 | + subject = {} |
| 18 | + subject['ref'] = value |
| 19 | + if subject not in subjects: |
| 20 | + subjects.append(subject) |
| 21 | + updatedRecord['subjects'] = subjects |
| 22 | + print updatedRecord['subjects'] |
| 23 | + updatedRecord = json.dumps(updatedRecord) |
| 24 | + print baseURL + uri |
| 25 | + post = requests.post(baseURL + uri, headers=headers, data=updatedRecord).json() |
| 26 | + print post |
| 27 | + f.writerow([uri]+[subjects]+[post]) |
| 28 | + else: |
| 29 | + print 'no update' |
| 30 | + f.writerow([uri]+['no update']+[]) |
| 31 | + elif key == 'linked_agents': |
| 32 | + agents = updatedRecord['linked_agents'] |
| 33 | + print 'originalAgents' |
| 34 | + print agents |
| 35 | + originalAgents = updatedRecord['linked_agents'] |
| 36 | + agent = {} |
| 37 | + agent['terms'] = [] |
| 38 | + agent['ref'] = value |
| 39 | + if row['tag'].startswith('1'): |
| 40 | + agent['role'] = 'creator' |
| 41 | + elif row['tag'].startswith('7'): |
| 42 | + agent['role'] = 'creator' |
| 43 | + elif row['tag'].startswith('6'): |
| 44 | + agent['role'] = 'subject' |
| 45 | + else: |
| 46 | + 'print error' |
| 47 | + f.writerow([uri]+['tag error']+[]) |
| 48 | + if agent not in agents: |
| 49 | + agents.append(agent) |
| 50 | + print 'updatedAgents' |
| 51 | + print agents |
| 52 | + updatedRecord['linked_agents'] = agents |
| 53 | + updatedRecord = json.dumps(updatedRecord) |
| 54 | + print baseURL + uri |
| 55 | + post = requests.post(baseURL + uri, headers=headers, data=updatedRecord).json() |
| 56 | + print post |
| 57 | + f.writerow([uri]+[agents]+[post]) |
| 58 | + else: |
| 59 | + print 'no update' |
| 60 | + print agent |
| 61 | + f.writerow([uri]+['no update']+[]) |
| 62 | + else: |
| 63 | + 'print error' |
| 64 | + f.writerow([uri]+['error']+[]) |
| 65 | + |
| 66 | +startTime = time.time() |
| 67 | + |
| 68 | +baseURL = secrets.baseURL |
| 69 | +user = secrets.user |
| 70 | +password = secrets.password |
| 71 | + |
| 72 | +auth = requests.post(baseURL + '/users/'+user+'/login?password='+password).json() |
| 73 | +session = auth["session"] |
| 74 | +headers = {'X-ArchivesSpace-Session':session, 'Content_Type':'application/json'} |
| 75 | + |
| 76 | +filename = raw_input('Enter filename (including \'.csv\'): ') |
| 77 | + |
| 78 | +f=csv.writer(open(filename+'Post'+datetime.now().strftime('%Y-%m-%d %H.%M.%S')+'.csv', 'wb')) |
| 79 | +f.writerow(['uri']+['links']+['post']) |
| 80 | + |
| 81 | +with open(filename) as csvfile: |
| 82 | + reader = csv.DictReader(csvfile) |
| 83 | + for row in reader: |
| 84 | + addUriLink ('linked_agents', 'agentUri') |
| 85 | + #addUriLink ('subjects', 'SubjectUri') |
| 86 | +elapsedTime = time.time() - startTime |
| 87 | +m, s = divmod(elapsedTime, 60) |
| 88 | +h, m = divmod(m, 60) |
| 89 | +print 'Total script run time: ', '%d:%02d:%02d' % (h, m, s) |
0 commit comments