import json, requests, csv, time, secrets
from datetime import datetime

# import secrets
secretsVersion = raw_input('To edit production server, enter the name of the secrets file: ')
if secretsVersion != '':
    try:
        secrets = __import__(secretsVersion)
        print 'Editing Production'
    except ImportError:
        print 'Editing Development'
else:
    print 'Editing Development'

startTime = time.time()

baseURL = secrets.baseURL
user = secrets.user
password = secrets.password
repository = secrets.repository

# test for successful connection
def test_connection():
	try:
		requests.get(baseURL)
		print 'Connected!'
		return True

	except requests.exceptions.ConnectionError:
		print 'Connection error. Please confirm ArchivesSpace is running.  Trying again in 10 seconds.'

is_connected = test_connection()

while not is_connected:
	time.sleep(10)
	is_connected = test_connection()

#authenticate
auth = requests.post(baseURL + '/users/'+user+'/login?password='+password).json()
session = auth["session"]
headers = {'X-ArchivesSpace-Session':session, 'Content_Type':'application/json'}

# User supplied variables
do_csv = raw_input('Enter csv filename: ')

# Open csv, create new csv
date = datetime.now().strftime('%Y-%m-%d %H.%M.%S')
csv_dict = csv.DictReader(open(do_csv))
f=csv.writer(open(date + 'new_' + do_csv, 'wb'))
f.writerow(['title']+['digital_object_id']+['uri'])

# Construct JSON to post from csv
doList = []
for row in csv_dict:
	file_uri = row['fileuri']
	title = row['title']
	digital_object_id = row['objectid']
	date_expression = row['dateExpression']
	date_begin = row['dateBegin']
	doRecord = {'title': title, 'digital_object_id': digital_object_id, 'publish': False}
	doRecord['file_versions'] = [{'file_uri': file_uri, 'publish': False, 'file_format_name': 'dng'}]
	doRecord['dates'] = [{'expression': date_expression, 'begin': date_begin, 'date_type': 'single', 'label': 'creation'}]
	doRecord = json.dumps(doRecord)
	post = requests.post(baseURL + '/repositories/'+ repository + '/digital_objects', headers=headers, data=doRecord).json()
	print post
	# Save uri to new csv file
	uri = post['uri']
	f.writerow([title]+[digital_object_id]+[uri])

# Feedback to user
print 'New .csv saved to working directory.'

# show script runtime
elapsedTime = time.time() - startTime
m, s = divmod(elapsedTime, 60)
h, m = divmod(m, 60)
print 'Post complete.  Total script run time: ', '%d:%02d:%02d' % (h, m, s)
