import csv

import GeoIP

gi6 = GeoIP.open("/usr/share/GeoIP/GeoIPv6.dat",GeoIP.GEOIP_STANDARD)

countries = {}
with open('mapData/countries.csv') as csvfile:
	reader = csv.DictReader(csvfile, delimiter=',', quotechar='"')
	rows = [r for r in reader]
	countries = {row['Alpha-2 code']:{'lon':row['Longitude (average)'],'lat': row['Latitude (average)']}
		for row in rows}
	countries.update({row['Alpha-3 code']:{'lon':row['Longitude (average)'],'lat': row['Latitude (average)']}
		for row in rows})

def ip62coord(ipv6):
	res = gi6.country_code_by_addr_v6(ipv6)
	if res is not None:
		if res in countries:
			coord = countries[res]
			return float(coord['lon']), float(coord['lat'])
	return None

