import requests
import json
import csv

print "Welcome to Place parsing App!"

with open('result.csv', 'wb') as ff:
	writer = csv.writer(ff, delimiter=";", quoting=csv.QUOTE_ALL)

	with open('monumentos.csv', 'rb') as f:
	    reader = csv.reader(f, delimiter=";", quoting=csv.QUOTE_NONE)
	    for row in reader:
			payload = {'placeid': ""+row[0], 'key': 'AIzaSyBkXoRT-QTxMIaat_FvARPh_G_hN4TbE9Q'}
			response = requests.get("https://maps.googleapis.com/maps/api/place/details/json", params=payload)
			print response.url
			j = json.loads(response.text);

			lst = []
			lst.append(row[0])
			
			try:
				lst.append(j['result']['name'])
			except:
				lst.append("")

			try:
				lst.append(j['result']['geometry']['location']['lat'])
			except:
				lst.append("")

			try:
				lst.append(j['result']['geometry']['location']['lng'])
			except:
				lst.append("")

			try:
				lst.append(j['result']['formatted_address'])
			except:
				lst.append("")

			try:
				lst.append(j['result']['international_phone_number'])
			except:
				lst.append("")

			try:
				lst.append(j['result']['rating'])
			except:
				lst.append("")

			try:
				lst.append(j['result']['website'])
			except:
				lst.append("")

			writer.writerow([unicode(s).encode("utf-8") for s in lst])

print "END"
