# -*- coding: utf-8 -*-

import sys
import csv
import requests

def read_csv(filename):
	""" Converts CSV file to list of dictionaries. """
	
	lst = []
	with open(filename, 'r') as f:
		csvfile = csv.reader(f)
		for row in csvfile:
			person = {}
			person['name'] = row[0]
			person['domain'] = row[1]
			lst.append(person)
	return lst

def post_norbert(payload):
	""" Posts to the voilanorbert.com api using the payload as parameters. """
	
	URL = "https://www.voilanorbert.com/api/v1/"
	
	# read the API key from a file that's not available on public GitHub repo :-)
	with open('Data/apikey.txt', 'r') as f:
		key = f.read()
	
	# query the API
	payload['token'] = key
	r = requests.post(URL, data=payload)
	print "status code:", r.status_code
	print "headers:", r.headers
	print "test:", r.text
	print "json:", r.json()
	print
	
	return



if __name__ == '__main__':
	bulk = []
	# parse args and error handling
	
	# load csv file with names to match
	bulk = read_csv('Data/test1.csv')
	for person in bulk:
		print person
	
	# send names norbert api one at a time
	for person in bulk:
		print "Payload sent: ", person
		post_norbert(person)
	
	# write results back to a csv
	
	# terminate with stats msg
	
	
	sys.exit()
@@ -355,16 +355,21 @@
 (payload
+, key
 ):%0A%09%22%22%22 
@@ -492,431 +492,921 @@
 /%22%0A%09
-%0A%09# read the API key from a file that's not available on public GitHub repo :-)%0A%09with open('Data/apikey.txt', 'r') as f:%0A%09%09key = f.read()%0A%09%0A%09# query the API%0A%09payload%5B'token'%5D = key%0A%09r = requests.post(URL, data=payload)%0A%09print %22status code:%22, r.status_code%0A%09print %22headers:%22, r.headers%0A%09print %22test:%22, r.text%0A%09print %22json:%22, r.json()%0A%09print%0A%09%0A%09return%0A%0A%0A%0Aif __name__ == '__main__':%0A%09bulk = %5B%5D%0A%09# parse args and error handling
+add_credits = False%0A%09payload%5B'token'%5D = key%0A%09%0A%09# query the API%0A%09try:%0A%09%09r = requests.post(URL, data=payload)%0A%09%09if r.status_code == 200:%0A%09%09%09payload%5B'success'%5D = r.json()%5B'success'%5D%0A%09%09%09payload%5B'emails'%5D = r.json()%5B'emails'%5D%0A%09%09%09payload%5B'error'%5D = None%0A%09%09elif r.status_code == 410:%0A%09%09%09payload%5B'success'%5D = r.json()%5B'success'%5D%0A%09%09%09payload%5B'emails'%5D = None%0A%09%09%09payload%5B'error'%5D = r.json()%5B'error'%5D%0A%09%09%09add_credits = True%0A%09%09else:%0A%09%09%09payload%5B'success'%5D = r.json()%5B'success'%5D%0A%09%09%09payload%5B'emails'%5D = None%0A%09%09%09payload%5B'error'%5D = r.json()%5B'error'%5D%0A%09except:%0A%09%09print %22%25s occurred processing %25s.%22 %25 (sys.exc_info()%5B0%5D.__name__, payload%5B'name'%5D)%0A%09%09payload%5B'success'%5D = None%0A%09%09payload%5B'emails'%5D = None%0A%09%09payload%5B'error'%5D = sys.exc_info()%5B0%5D.__name__%0A%09%0A%09return payload, add_credits%0A%0A%0A%0Aif __name__ == '__main__':%0A%09bulk = %5B%5D%0A%09# parse args and error handling%0A%09%0A%09# fetch the api key%0A%09with open('Data/apikey.txt', 'r') as f:%0A%09%09key = f.read()
 %0A%09%0A%09
@@ -1472,52 +1472,21 @@
 test
-1
+_error
 .csv')%0A
-%09for person in bulk:%0A%09%09print person%0A
 %09%0A%09#
@@ -1583,28 +1583,117 @@
 n%0A%09%09
-post_norbert(person)
+result, buy_credits = post_norbert(person, key)%0A%09%09print %22result:%22, result%0A%09%09print %22buy_credits:%22, buy_credits
 %0A%09%0A%09

Add error checking to API query.