#!/bin/env python

import requests
import json
from geopy.geocoders import Nominatim

#__init__(format_string='%s', view_box=(-180, -90, 180, 90), country_bias=None, timeout=1, proxies=None, domain='nominatim.openstreetmap.org', scheme='https')

# http://wiki.openstreetmap.org/wiki/Nominatim

# http://nominatim.openstreetmap.org/search?<params>
# http://nominatim.openstreetmap.org/search/<query>?<params>

#http://nominatim.openstreetmap.org/search?q=135+pilkington+avenue,+birmingham&format=xml&polygon=1&addressdetails=1
url = 'http://nominatim.openstreetmap.org/'

params = {'q': '157+High+Street,+Prestatyn,+Conwy', 'format': 'json', 'addressdetails': 1, 'limit': 1}
#params = {'q': 'Stockbridge+Village,+Liverpool', 'format': 'json', 'addressdetails': 1, 'limit': 1}
#params = {'q': '3 cobden mews,The broadway, Wimbledon', 'format': 'json', 'addressdetails': 1, 'limit': 1}
#print type(requests.get(url, params=params).json()[0])
print type(requests.get(url, params=params).json())
print len(requests.get(url, params=params).json())
print requests.get(url, params=params).json()
#requests.get(url, params=params).json()[0]

json_response = requests.get(url, params=params, headers={'Accept-Language': 'en'}).json()[0]

print(json.dumps(json_response, sort_keys=False, indent=4, separators=(',', ': ')))

#http://nominatim.openstreetmap.org/reverse?format=xml&lat=53.33252&lon=-3.400008&zoom=18&addressdetails=1
url = 'http://nominatim.openstreetmap.org/reverse'

#params = {'q':,'format': 'json', 'addressdetails': 1, 'limit': 1, 'lat': '52.54877605', 'lon': '-1.81627023283164'}


#requests.get(url, params=params).json()[0]

#json_response = requests.get(url, params=params, headers={'Accept-Language': 'en'}).json()[0]

#from geopy.geocoders import GoogleV3
#geolocator = GoogleV3()
##address, (latitude, longitude) = geolocator.reverse("40.752067, -73.977578")
#address = geolocator.reverse("40.752067, -73.977578")
##print address, latitude, longitude
##print latitude, longitude
#print address
#
#address = geolocator.geocode("175 5th Avenue NYC")
#print address
#address, (latitude, longitude) = geolocator.geocode("175 5th Avenue NYC")
#print address, latitude, longitude

from geopy.geocoders import Nominatim
geolocator = Nominatim()



#address = geolocator.reverse("40.752067, -73.977578")
#print address

address, (latitude, longitude) = geolocator.reverse("40.752067, -73.977578")
print address, latitude, longitude

params = {'q': '157+High+Street,+Prestatyn,+Conwy', 'format': 'json', 'addressdetails': 1, 'limit': 1}

streetname='157 High Street'
cityname='Prestatyn'
statename='Conwy'
countryname='UK'
postcode='LL19 9AP'

#geocode(query, exactly_one=True, timeout=None, addressdetails=False, language=False, geometry=None)
#address, (latitude, longitude) = geolocator.geocode(['street' : streetname, 'city' : cityname, 'state' : statename, 'country' : countryname, 'postalcode' : postcode ])
#address, (latitude, longitude) = geolocator.geocode({'street' : '157 High Street', 'postalcode' : 'LL19 9AP'})
address, (latitude, longitude) = geolocator.geocode({'street' : streetname, 'postalcode' : postcode})

print address, latitude, longitude


def osm_address_lookup(dict, geolocator):
	#print type(dict)
	#print dict
	#print dict['name']
	#print dict['address']
	#streetname = "+".join(dict['address'][0:2])

	if 'address' in dict:
		streetname = dict['address'][0]
		print type(streetname)
		print streetname
		postcode = dict['address'][-2]
		print type(postcode)
		print postcode
		#address, (latitude, longitude) = geolocator.geocode({'street' : streetname, 'postalcode' : postcode})
		#address, (latitude, longitude) = geolocator.geocode({'postalcode' : postcode})
		#address, (latitude, longitude) = geolocator.geocode(streetname)
		address, (latitude, longitude) = geolocator.geocode({'street' : streetname, 'postalcode' : postcode})
	else:
		print "No address."
	#print address, latitude, longitude

	lat = dict['location']['lat']
	lon = dict["location"]['lon']
	latlon = dict["location"]['lat'] + "," + dict["location"]['lon']
	print type(latlon)
	print latlon
	if 'location' in dict:
		 print geolocator.reverse(latlon)
	else:
		pass
	#return address, latitude, longitude

address = [
        "61 Coldharbour Lane",
        "Hayes",
        "Middlesex",
        "UB3 3EE",
        "England"
    ]

location = {
	"lat" : "51.50828",
	"lon" : "-0.4145179"
	}

address = [
        "The Croft",
        "Stockbridge Village",
        "Liverpool",
        "Merseyside",
        "L28 1NR",
        "England"
    ]

location = {
	"lat" : "53.43529",
	"lon" : "-2.867382"
	}

#address = [
#        "3 Cobden Mews",
#        "The Broadway",
#        "Wimbledon",
#        "London",
#        "SW19 1RH",
#        "England"
#    ]
#
#location = {
#        "lat": "51.41904",
#        "lon": "-0.1993326"
#    }

dict = {'address' : address, 'location' : location}
geolocator = Nominatim()
osm_address_lookup(dict, geolocator)

dict = {'location' : location}
geolocator = Nominatim()
osm_address_lookup(dict, geolocator)
